Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on

I'm new to Docker and setting up my first Django application using Docker

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 use FROM ubuntu:18.04 with above configuration?

Edit 2

Now, it stuck after creating migration from start.sh commands enter image description here

like image 697
Anuj TBE Avatar asked Jun 03 '18 09:06

Anuj TBE


1 Answers

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

like image 198
gcharbon Avatar answered Nov 01 '22 01:11

gcharbon