This question already has answers here:
Nginx and uwsgi connection refused when placed in separate docker containers
(1 answer)
Connection refused: when uwsgi and nginx in different containers
(1 answer)
Closed last year.
I'm trying to do an infra with docker. I can't use flask or Django.
Here are my files, I tried lot of uwsgi parameters but I still have this error :
connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.240.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://127.0.0.1:8080", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/"
I'm a student, I already read all the documentations, articles that I can, but I still don't know how to fix my problem.
Here are my different files.
[./default.conf]
upstream uwsgicluster {
server 127.0.0.1:8080;
}
server {
listen 80;
#root /usr/share/nginx/html;
location / {
#try_files $uri #wsgi;
uwsgi_pass uwsgicluster;
include /etc/nginx/uwsgi_params;
}
}
[python/app.ini]
[uwsgi]
wsgi-file = run.py # The file containing the callable (app)
callable = python # The callable object itself
socket = :5000 # The socket uwsgi will listen on
master = true
chmod-socket = 666
vacuum = true
process = 2
[python/Dockerfile]
FROM python:3.6.5
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
RUN apt-get update && apt-get install -y uwsgi-plugin-python3
EXPOSE 5000
COPY . .
RUN uwsgi --ini app.ini
[docker-compose.yml]
services:
mysql_db:
container_name: "mysql_db"
image: mysql:5.7
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
volumes:
- db_volume:/var/lib/mysql
environment: # Set up mysql database name and password
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: employees
MYSQL_USER: user
MYSQL_PASSWORD: password
networks:
- app-db
python3:
restart: always
build: ./python
container_name: "python3"
working_dir: "/root/"
tty: true
environment:
- APP_NAME=DOCKER-TEST
depends_on:
- mysql_db
networks:
- app-front
- app-db
volumes:
- ./python:/root
- pycache_volume:/root/.cache
links:
- mysql_db
front:
image: nginx:latest
container_name: "front"
volumes:
- ./front/default.conf:/etc/nginx/conf.d/default.conf
ports:
- 8080:80
networks:
- app-front
links:
- python3
networks:
app-front:
driver: bridge
app-db:
driver: bridge
volumes:
db_volume:
pycache_volume:
Can you help me please ?
Thank you !
Related
I am going to lose my mind because I've been stuck on this for 2 days and can't figure out why python wants to keep using 127.0.0.1 instead of the host I have specified. My docker compose snippet is:
# Use root/example as user/password credentials
version: '3.1'
services:
mongo_service:
image: mongo
#command: --default-authentication-plugin=mysql_native_password
command: mongo
restart: always
ports:
- '27017:27017'
cinemas_api:
container_name: cinemas_api
hostname: cinemas_api
build:
context: ./cinemas_api
dockerfile: Dockerfile
links:
- mongo_service
ports:
- 5000:5000
expose:
- '5000'
depends_on:
- mongo_service
booking_api:
container_name: booking_api
hostname: booking_api
build:
context: ./booking_api
dockerfile: Dockerfile
ports:
- 5050:5000
depends_on:
- mongo_service
networks:
db_net:
external: true
#docker-compose -f docker-compose.yaml up --build
then in cinemas.py, I try to connect:
client = MongoClient('mongodb://mongo_service:27017/test')
However, I get the error
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 ::
caused by :: Connection refused :
In one container there Django app running which expose to host port 8000
and another container flask app is running which expose to the host port 8001. So there is a condition where the flask app API end needs to communicate with the Django app API end.
Code
req = requests.get('http://192.168.43.66:8000/api/some')
Error
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.43.66', port=8000): Max retries exceeded with url: /api/user (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc6f96942e0>: Failed to establish a new connection: [Errno 110] Connection timed out'))
But if I change the request URL to some other API end which not running on the container it gets the response.
And each Django API end is working fine if I want to access it through some other external source like postman or browser.
Here is the docker-compose file content
Django App docker-compose.yml
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile
command: python3 manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
volumes:
- .:/app
depends_on:
- db
db:
image: mysql:5.7.22
restart: always
environment:
MYSQL_DATABASE: admin
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33066:3306
flask app docker-compose.yml
version: '3.8'
services:
backend:
build:
context: .
dockerfile: Dockerfile
network: 'host'
command: 'python3 main.py'
ports:
- 8001:5000
volumes:
- .:/app
depends_on:
- db
db:
image: mysql:5.7.22
restart: always
environment:
MYSQL_ROOT_HOST: '%'
MYSQL_DATABASE: main
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
volumes:
- .dbdata:/var/lib/mysql
ports:
- 33067:3306
Note: in the project YAML is well formatted so that's not an error.
when you expose a port from container, the host can access it but it doesn't mean other containers can access it too.
you either have to set the network mode to host (which won't work on windows, this is only possible if you're running them on linux).
OR you can run it in a docker-compose and define your own network, here is an example:
version : '3.4'
services:
UI:
container_name: django_app
image: django_image
networks:
my_net:
ipv4_address: 172.26.1.1
ports:
- "8080:8000"
api:
container_name: flask_app
image: flask_image
networks:
my_net:
ipv4_address: 172.26.1.2
networks:
my_net:
ipam:
driver: default
config:
- subnet: 172.26.0.0/16
now your django app can access your flask app on 172.26.1.2
EDIT :
now that you have added your docker-compose files too, you should not create apps in two different docker-compose (that's why you were getting the conflicting ip range error, you were building two networks with same range).
place everything in a single docker-compose, give them ip addresses and you should be fine.
you could make your apps to read ip address of services they relate on from environment variables and then pass these env variables to your container for more flexibility.
I have a flask app that I want to host on nginx in my docker compose file but when I do it gives me an Internal Server error
Here are some important files:
docker-compose.yml
version: "3.8"
services:
th3pl4gu3:
container_name: portfolix
build: ./
networks:
- portfolix_net
ports:
- 8084:8084
restart: always
server:
image: nginx:1.17.10
container_name: nginx
depends_on:
- th3pl4gu3
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
networks:
- portfolix_net
networks:
portfolix_net:
name: portfolix_network
driver: bridge
nginx.conf:
server {
listen 80;
location / {
include uwsgi_params;
uwsgi_pass th3pl4gu3:8084;
}
}
Flask Dockerfile
# Using python 3.8 in Alpine
FROM python:3.8-alpine3.11
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Dependencies for uWSGI
RUN apk add python3-dev build-base linux-headers pcre-dev && pip install -r requirements.txt && apk update
# In case bash is needed
#RUN apk add --no-cache bash
# Tell the port number the container should expose
EXPOSE 8084
# Run the command
ENTRYPOINT ["uwsgi", "app.ini"]
app.ini
[uwsgi]
module = run:app
master = true
processes = 5
http-socket = 0.0.0.0:8084
chmod-socket = 660
vacuum = true
die-on-term = true
Now when I run this docker-compose without the nginx service, it works, but i want it to run on nginx server. Any idea why i am geting the Internal Server Error?
I was able to solve it with the following docker-compose:
version: "3.8"
services:
th3pl4gu3:
container_name: portfolix
build: ./
networks:
- portfolix_net
expose:
- 8084
restart: always
server:
image: nginx:1.17.10
container_name: nginx
depends_on:
- th3pl4gu3
volumes:
- ./reverse_proxy/nginx.conf:/etc/nginx/nginx.conf
ports:
- 8084:80
networks:
- portfolix_net
networks:
portfolix_net:
name: portfolix_network
driver: bridge
The issue was with my 8084:8084
I have this docker-compose.yml file:
version: '2'
services:
nginx:
image: nginx:latest
container_name: nz01
ports:
- "8001:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: dz01
depends_on:
- db
volumes:
- ./src:/src
expose:
- "8000"
db:
image: postgres:latest
container_name: pz01
ports:
- "5433:5432"
volumes:
- postgres_database:/var/lib/postgresql/data:Z
volumes:
postgres_database:
external: true
And this dockerfile:
FROM python:3.5
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn computationalMarketing.wsgi -b 0.0.0.0:8000
The web and postgres server does not return an error log, just the success when I run docker-compose build and docker-compose up -d.
At this moment the three containers are running, but when I go to the browser and navigate to: localhost:8001 it does not work.
It shows the "connection has been restarted" error message.
Despite that, the web server still does not return any error, so I guess that I have everything properly configurated in my Django app. I really believe that the problem is related to Nginx, because when I review the Nginx log (using kinematic) it is still empty.
Why wouldn't Nginx be listening to connections?
Hint:
This error is happening in a new project. I tried to understand whether I have anything wrong, I'm running an old project and it works perfectly. I tried to copy the working project in my new folder and remove all existent containers and then try to run this old project in a new folder and there is the surprise. It does not work now, despite being an exact copy of the project that works in the other folder...
EDIT
In my repo I have a config/nginx folder with the helloworld.conf file:
upstream web {
ip_hash;
server web:8000;
}
server {
location /static/ {
autoindex on;
alias /src/static/;
}
location / {
proxy_pass http://web/;
}
listen 8001;
server_name localhost;
}
Still with the same error... I do not see any log error.
Django container log
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
[2018-11-05 13:00:09 +0000] [8] [INFO] Starting gunicorn 19.7.1
[2018-11-05 13:00:09 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
[2018-11-05 13:00:09 +0000] [8] [INFO] Using worker: sync
[2018-11-05 13:00:09 +0000] [11] [INFO] Booting worker with pid: 11
You nginx configure should be like:
upstream web {
ip_hash;
server web:8000;
}
server {
location / {
proxy_pass http://web/;
}
listen 8001;
server_name localhost;
}
Since this kind of problems usually are difficult to debug/reproduce I have created dummy example to just run Django app and serve it via Nginx. You can try to adjust it to your needs. Please forgive me if I have missed something, or done that shouldn't be, but I'm unfamiliar with Django framework.
Dockerfile for Django container:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code && \
pip install django
WORKDIR /code
ADD helloworld /code
docker-compose.yml:
version: '3'
services:
nginx:
image: nginx:latest
container_name: nginx
ports:
- "80:80"
volumes:
- ./config/nginx:/etc/nginx/conf.d
depends_on:
- web
web:
build: .
container_name: django
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./helloworld:/code
expose:
- "8000"
config/nginx/django.conf:
upstream web {
ip_hash;
server web:8000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web/;
}
}
Django app is inside helloworld folder.
For this example, traffic is simply passed. Proper way would be using Unix sockets instead of ports but again I'm unfamiliar with Django.
Playing around with flask I would like to get a real setup up and running in docker. This means flask should be served via nginx and gunicorn. I set up a sample code repository https://github.com/geoHeil/pythonServing but so far can't get nginx to work properly.
Flask is served on application:5000, docker should resolve application to its respective name.
Nginx config is as follows:
server {
listen 8080;
server_name application;
charset utf-8;
location / {
proxy_pass http://application:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
which looks good to me. So far I cant find the problem.
edit
compose file is here. Command to start was
docker-compose build
docker-compose up
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
links:
- db
expose:
- "5000"
ports:
- "5000:5000"
nginx:
restart: always
build: ./nginx
links:
- application
expose:
- 8080
ports:
- "8880:8080"
Your nginx config file is in a wrong location.
Steps to fix:
sudo docker-compose down
Delete nginx image:
sudo docker images
sudo docker rmi
REPOSITORY TAG IMAGE ID CREATED SIZE
pythonserving_nginx latest 152698f13c7a About a minute ago 54.3 MB
sudo docker rmi pythonserving_nginx
Now change the nginx Dockerfile:
FROM nginx:1.11.8-alpine
MAINTAINER geoheil
ADD sites-enabled.conf /etc/nginx/conf.d/sites-enabled.conf
Please note the location of nginx config.
Now try this docker-compose file (Using User-defined Networks):
version: '2'
services:
application:
restart: always
build: ./application
command: gunicorn -w 4 --bind :5000 wsgi:application
networks:
- testnetwork
expose:
- "5000"
ports:
- "5000:5000"
db:
restart: always
image: postgres:9.6.1-alpine
networks:
- testnetwork
ports:
- "5432:5432"
environment:
- POSTGRES_USER=d
- POSTGRES_PASSWORD=d
- POSTGRES_DB=d
volumes:
- ./postgres:/var/lib/postgresql
nginx:
restart: always
build: ./nginx
networks:
- testnetwork
expose:
- 8080
ports:
- "8880:8080"
networks:
testnetwork:
And Bring up containers:
sudo docker-compose up
Browse to http://localhost:8880
Smaple docker file
FROM python:3.5
RUN apt-get update
RUN apt-get install -y --no-install-recommends \
libatlas-base-dev gfortran nginx supervisor
RUN pip3 install uwsgi
COPY ./requirements.txt /project/requirements.txt
RUN pip3 install -r /project/requirements.txt
RUN useradd --no-create-home nginx
RUN rm /etc/nginx/sites-enabled/default
RUN rm -r /root/.cache
COPY nginx.conf /etc/nginx/
COPY flask-site-nginx.conf /etc/nginx/conf.d/
COPY uwsgi.ini /etc/uwsgi/
COPY supervisord.conf /etc/
COPY /app /project
WORKDIR /project
CMD ["/usr/bin/supervisord"]