8

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

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks and I made changes same as your answer but now stuck after a command from start.sh.
It'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?
I figured out that start.sh file executes only first exec command and stops then after. Is there anything wrong in .bash file? I think it will be solved by making changes to start.sh file only.
Why 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?
Maybe you should read stackoverflow.com/questions/43925487/… I don't know much about gunicorn and docs.gunicorn.org/en/stable/deploy.html
|
3

For me, stopping/restarting the NGINX server worked.

To stop Nginx, use the following command:

sudo systemctl stop nginx

To start Nginx when it is stopped, use the following command:

sudo systemctl start nginx 

To restart Nginx, use the following command:

sudo systemctl restart nginx 

To reload Nginx after making configuration changes, use the following command:

sudo systemctl reload nginx 

1 Comment

works for me , it's preferable to disable nginx daemon from starting in boot : sudo systemctl disable nginx
0

Following the upgrade to Ubuntu 22.04, I faced the same issue due to the installation of apache2 during the upgrade process. To resolve this, I uninstalled apache2, and everything worked smoothly. Since I'm using docker, apache2 isn't necessary for me. However, if you need both apache2 and docker, you might want to consider either using a different port for nginx or stopping apache2 while using nginx.

Here are the commands for stopping apache2:

  • Stop apache2

    sudo service apache2 stop

  • Uninstall apache2

    sudo apt-get purge apache2 apache2-utils apache2.2-bin

    sudo rm -rf /etc/apache2

Alternatively, you can find which service is using port 80 and stop it using:

sudo ss -tuln | grep 80

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.