0

I try to dockerise a django container for production.

So I have my dockerfile:

FROM python:3.8-alpine

ENV PATH="/scripts:${PATH}"

COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN pip install -r /requirements.txt
RUN apk del .tmp

RUN mkdir /app
COPY ./Welzijn /app
WORKDIR /app
COPY ./scripts /scripts

RUN chmod +x /scripts/*

RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static
RUN adduser -D user
RUN chown -R user:user /vol
RUN chmod -R 755 /vol/web
USER user

CMD ["entrypoint.sh"]

and the docker-compose file:

version: "3.9"

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./Welzijn:/app
    command: >
      sh -c "python ./manage.py migrate &&      
             python ./manage.py runserver 0:8000"
    environment:
      - DEBUG=1
    env_file:
      - ./.env

and requirements.txt file:

Django>=4.0.4
uWSGI>=2.0.18,<2.1
djangorestframework>=3.13.1
psycopg2>=2.9.3
drf-spectacular>=0.22.1
Pillow>=9.1.0
drf-yasg==1.20.0 
django-cors-headers==3.10.1
django-dotenv

But when I execute docker-compose up --build

I get this errors:

 If you prefer to avoid building psycopg2 from source, please install the PyPI
3.686       'psycopg2-binary' package instead.
3.686
3.686       For further information please check the 'doc/src/install.rst' file (also at
3.686       <https://www.psycopg.org/docs/install.html>).
3.686
3.686       [end of output]
3.686
3.686   note: This error originates from a subprocess, and is likely not a problem with pip.
3.690 error: metadata-generation-failed
3.690
3.690 × Encountered error while generating package metadata.
3.690 ╰─> See above for output.
3.690
3.690 note: This is an issue with the package mentioned above, not pip.
3.690 hint: See above for details.
3.835
3.835 [notice] A new release of pip is available: 23.0.1 -> 23.2.1
3.835 [notice] To update, run: pip install --upgrade pip
------
failed to solve: process "/bin/sh -c pip install -r /requirements.txt" did not complete successfully: exit code: 1

Question: how to dockerise django for production?

mightycode Newton
  • 3,229
  • 3
  • 28
  • 54
  • 1
    Just before the text you quote, does it say `Error: pg_config executable not found`? That's described in [Failing to install psycopg2-binary on new docker container](https://stackoverflow.com/questions/62715570/failing-to-install-psycopg2-binary-on-new-docker-container) and [pg_config executable not found](https://stackoverflow.com/questions/11618898/pg-config-executable-not-found). – David Maze Aug 15 '23 at 10:22

1 Answers1

0

Here is the complete flow to dockerize the django for production with Postgres Database along with PgAdmin.

Dockerfile

FROM python:3.10-slim-buster

# Creating workdir and copying the config and requirements dependencies
WORKDIR /projectdirname
COPY requirements.txt /projectdirname/

RUN apt-get update && apt-get install -y git gcc\
    && pip install -r requirements.txt

COPY . /projectdirname
RUN chmod +x entrypoint.sh
ENTRYPOINT [ "sh", "entrypoint.sh" ]

docker-compose.yml

version: '3'

services:
  postgres:
    image: postgres:latest
    restart: always
    container_name: pg_container
    command: postgres -c 'max_connections=100'
    volumes:
      - postgres_db:/var/lib/postgresql/data
    env_file:
      - .env

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin4_container
    restart: always
    ports:
      - "5050:80"
    env_file:
      - .env
    environment:
      PGADMIN_DEFAULT_EMAIL: example@example.com
      PGADMIN_DEFAULT_PASSWORD: example
    volumes:
      - postgres_admin:/var/lib/postgresql/data/admin

  server:
    build: .
    restart: always
    container_name: django_container
    links:
      - postgres:postgres
      - pgadmin:pgadmin
    depends_on:
      - postgres
      - pgadmin
    env_file:
      - .env
    volumes:
      - ./:/projectdirname
    ports:
      - '8000:8000'

volumes:
  postgres_db:
  postgres_admin:

entrypoint.sh

python manage.py migrate
python manage.py runserver 0.0.0.0:8000

.env

SECRET_KEY=Place your secret key here
POSTGRES_USER=test
POSTGRES_PASSWORD=test
POSTGRES_DB=dbName
POSTGRES_PORT=5432
POSTGRES_HOST=postgres