Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Migrations doesn't starts for dockerized Django project

I’ve created e simple hello world project with Django and Docker. At the end of Dockerfile there is the command below:

ENTRYPOINT ["/app/web/entrypoint.sh"]

that activate this script:

#!/bin/sh

echo " ---> DB Connection Parameters: \
            DB name: $DB_NAME \
            DB host: $DB_HOST \
            DB port: $DB_PORT"

poetry run python3 website/manage.py migrate --noinput

poetry run python3 website/manage.py collectstatic --noinput

poetry run python3 website/manage.py createsuperuser --noinput

echo " ---> Django Project Port: $PROJECT_PORT"

poetry run python3 website/manage.py runserver 0.0.0.0:"$PROJECT_PORT"

exec "$@"

The project starts but the migration isn’t run and I’m forced to log inside the container and use python3 manage.py migrate to start the migration.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why the migration command doesn’t run and collectstatic runs automatically?

Below the docker-compose:

version: '3.7'

services:
  db:
    container_name: dev_db
    image: postgis/postgis
    restart: always
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${DB_NAME}
    volumes:
      - db-data:/var/lib/postgresql/data

  website:
    image: maxdragonheart/${PROJECT_NAME}
    build:
      context: ./web
      dockerfile: Dockerfile
    environment:
      PROJECT_NAME: ${PROJECT_NAME}
      PROJECT_PORT: ${PROJECT_PORT}
      SECRET_KEY: ${SECRET_KEY}
      DB_ENGINE: ${DB_ENGINE}
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_PORT: ${DB_PORT}
      DB_HOST: ${DB_HOST}
      DEBUG: ${DEBUG}
      ALLOWED_HOSTS: ${ALLOWED_HOSTS}
    container_name: dev_website
    restart: always
    ports:
      - ${PROJECT_PORT}:${PROJECT_PORT}
    volumes:
      - website-static-folder:/app/web/static-folder
      - website-media-folder:/app/web/media-folder
      - logs:/app/logs
    depends_on:
      - db


volumes:
  db-data:
  website-static-folder:
  website-media-folder:
  logs:

>Solution :

Docker runs manage.py collectstatic --noinput automatically, you don’t have to run command for it. For custom commands, you can try adding this:

website:
  ...
  command: bash -c "python3 website/manage.py migrate --noinput &&
                    python3 website/manage.py createsuperuser --noinput &&
                    python3 website/manage.py runserver 0.0.0.0:[YOUR PORT]"
  ...

to your docker-compose.yml file.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading