docker ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on
Solution 1
You can't allocate port 9010 of your host for both services.
This is what you're doing in the ports
section of declaration of service nginx and web.
Moreover, by default nginx will listen to port 80 and 443 for https.
You can keep it like that and publish to a different port on your host. See how to use port
keyword in docker-compose :
https://docs.docker.com/compose/compose-file/#ports
Maybe you want something more like that:
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "10080:80"
- "10443:443"
volumes:
- .:/app
- ./config/nginx:/etc/nginx/conf.d
- ./static_cdn:/static
depends_on:
- web
web:
build: .
command: ./start.sh
container_name: "web-app"
volumes:
- .:/app
- ./static_cdn:/static
expose:
- "9010"
depends_on:
- db
db:
image: postgres
contents of config/nginx/nginx.conf
upstream web {
ip_hash;
server web-app:9010;
}
server {
location /static {
autoindex on;
alias /static/
}
location / {
proxy_pass http://web;
}
listen 80;
server_name localhost;
}
Concerning your last question, you could go for an official Python image from the Docker hub Python repository or start from any other base image like debian:jessie-slim from Debian official repository or keep the Ubuntu 18.04 image
Solution 2
Feb, 2022 Update:
Kill all nginx processes:
sudo killall nginx
Then:
docker-compose up --build
Related videos on Youtube
Anuj TBE
I am a software engineer persuing B.Tech with Computer Science. Like to write and deploy softwares,programs and website/web programs for windows,android and many...
Updated on February 10, 2022Comments
-
Anuj TBE about 2 years
I'm new to
Docker
and setting up my firstDjango
application usingDocker
My application path looks like
app |- helloworld |- __init__.py |- manage.py |- static_cdn |- static_root |- config |- nginx |- nginx.conf |- Dockerfile |- docker-compose.yml |- requirements.txt |- start.sh
the contents of
Docerfile
FROM ubuntu:18.04 # -- Install Pipenv: FROM python:3 ENV PYTHONUNBUFFERED 1 ENV LC_ALL C.UTF-8 ENV LANG C.UTF-8 # -- Install Application into container: RUN set -ex && mkdir /app WORKDIR /app ADD requirements.txt /app/ RUN pip install -r requirements.txt # -- Adding dependencies: ADD . /app/
contents of
docker-compose.yml
version: '3' services: nginx: image: nginx:latest ports: - "9010:9010" volumes: - .:/app - ./config/nginx:/etc/nginx/conf.d - ./static_cdn:/static depends_on: - web web: build: . command: ./start.sh volumes: - .:/app - ./static_cdn:/static ports: - "9010:9010" depends_on: - db expose: - "9010" db: image: postgres
contents of
config/nginx/nginx.conf
upstream web { ip_hash; server web:9010; } server { location /static { autoindex on; alias /static/ } location / { proxy_pass http://127.0.0.1; } listen 9011; server_name localhost; }
contents of
start.sh
#!/usr/bin/env bash # Start Gunicorn processes echo --: Starting application build echo --: Creating migration exec python3 manage.py makemigrations echo ------: makemigrations complete echo --: Running migration exec python3 manage.py migrate echo ------: migrate complete echo --: Running collectstatic exec python3 manage.py collectstatic echo ------: collectstatic complete echo Starting Gunicorn. exec gunicorn helloworld.wsgi:application \ --bind 0.0.0.0:9010 \ --workers 3
Now, when I build using docker
docker-compose up --build
It gives error as
ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint koober_nginx_1 (8ea5c084a7283a16afbf136a73dc4b27d9cae35fe14d735b83199ad5d0e03431): Bind for 0.0.0.0:9010 failed: port is already allocated
I have followed few tutorials to create those Docker files and nginx conf file.
1. How can I solve above issue.
2. Do I need to useFROM ubuntu:18.04
with above configuration?Edit 2
Now, it stuck after creating migration from
start.sh
commands -
Anuj TBE almost 6 yearsThanks and I made changes same as your answer but now stuck after a command from
start.sh
. -
gcharbon almost 6 yearsIt's hard to tell with your logs beacause all we can see is that postresql is alright and your application doesn't start. Can't you have more logs?
-
Anuj TBE almost 6 yearsI figured out that
start.sh
file executes only firstexec
command and stops then after. Is there anything wrong in.bash
file? I think it will be solved by making changes tostart.sh
file only. -
gcharbon almost 6 yearsWhy are you using
exec
in your last line? (exec gunicorn helloworld.wsgi:application \ --bind 0.0.0.0:9010 \ --workers 3
) and don't just call unicorn? -
gcharbon almost 6 yearsMaybe you should read stackoverflow.com/questions/43925487/… I don't know much about gunicorn and docs.gunicorn.org/en/stable/deploy.html
-
Anuj TBE almost 6 yearscan you tell me at what host it should be working. I tried
localhost
,localhost:9091
,127.0.0.1
,127.0.0.1:9091
but it's not working -
gcharbon almost 6 yearsIt should be working on localhost:10080 or localhost:10443 for https. Remember, you expose your unicorn app port on the docker network. Nginx can redirect to the expose port. But you will always reach nginx and not your unicorn app. So ports published on the port declaration section of service nginx in docker-composemust be used. Only publihed ports are accessible from hosts, not exposed ports. By the way localhost or 127.0.0.1 should be the same. And don't use exec when writing shell scripts, it does not behave the way you think (man7.org/linux/man-pages/man3/exec.3.html)