Reputation: 301
I would like to deploy flask app on my VPS. I figured out easy how to do it without docker, but now I dockerized the app and I am running it using docker-composer.yml.
services: │
myapp: │
build: ./myapp │
container_name: myapp │
restart: always │
environment: │
- APP_NAME = myapp │
expose: │
- 8081
So I changed my nginx conf from
location / {
include uwsgi_params;
uwsgi_pass unix:/home/username/path/to/socket/mysocker.sock;
}
to
location / {
include uwsgi_params;
uwsgi_pass myapp:8081;
}
The app is running using docker composer but when I test the correct settings in nginx using nginx -t I get this message
nginx: [emerg] host not found in upstream "myapp" in /etc/nginx/sites-enabled/myapp:22 │nginx: configuration file /etc/nginx/nginx.conf test failed
I am pretty sure that means that nginx cannot find myapp running in docker and cannot communicate with it but I exposed the port and from what I understood the container name is host name so it should work. Does anyone know how to make them communicate? I didnt find any tutorials on internet that wouldnt also dockerize the nginx I DONT WANT IT.
Any help is appretiated
EDIT: This is my Dockerfile
FROM python:3.8.5-buster
WORKDIR /app
ADD . /app
RUN apt-get update -y && apt-get -y install curl && apt-get install libsasl2$
RUN pip3 install mysqlclient
RUN pip3 install blinker
RUN pip3 install pyOpenSSL
RUN pip3 install uwsgi
RUN pip3 install -r requirements.txt
CMD ["uwsgi", "myproject.ini", "--enable-threads"]
UWSGI
[uwsgi]
wsgi-file=wsgi.py
callable=app
socket=8081
module = wsgi:app
master = true
processes = 1
chmod-socket = 666
vacuum = true
harakiri = 120
die-on-term = true
The solution
Upvotes: 0
Views: 338
Reputation: 20176
Docker has internal DNS server working on 127.0.0.11
inside container. If your nginx is not in container you cannot use it to resolve myapp
name. Still, you can pick one of those:
services:
myapp:
ports:
# host:container
- 8081:8081
Then reflect this change in your nginx configuration:
location / {
include uwsgi_params;
uwsgi_pass localhost:8081;
}
/tmp
into container:services:
myapp:
volumes:
# host:container
- /tmp:/tmp
Then configure your application to put the socket into /tmp
. The socket will appear in host's /tmp
and you can configure nginx to communicate to it. You may slightly improve this if you mount not the whole /tmp
but a single directory inside it. /tmp/myapp
for example. That way you eliminate chances that your container will mess something with host's files.
Upvotes: 1