0

I followed this tutorial to run my django web-app locally, apart for the web-app the only service is a postgres db.

I wrote a simple script entrypoint.sh to automate the initial operations needed by a django app, like migrate, makemigrations, collectstatic, createsuperuser;

Everything works fine, except that entrypoint.sh runs everytime I use docker-compose up, performing initial operations that should only run once.

How can I set up my Dockerfile or docker-compose.yml so that entrypoint.sh is run just the first time and not everytime I docker-compose down and then docker-compose up again?

Dockerfile

# importing base image
FROM python:3.9

# updating docker host or host machine
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

# changing current working directory to /usr/src/app
WORKDIR /usr/src/app

# copying requirement.txt file to present working directory
COPY requirements.txt ./

# installing dependency in container
RUN pip install -r requirements.txt

# copying all the files to present working directory
COPY . .

# informing Docker that the container listens on the
# specified network ports at runtime i.e 8000.
EXPOSE 8000
ENTRYPOINT ["./entrypoint.sh"]

docker-compose.yml

version: '3.7'

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  app:
    build: ./
    command: gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000
    volumes:
      - ./data/:/usr/src/app/data/
      - ./media/:/usr/src/app/media/
    ports:
      - 8000:8000
      - 5432:5432
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - [email protected]
      - [email protected]
      - DJANGO_SUPERUSER_PASSWORD=passadmin
    depends_on:
      - db

entrypoint.sh

#!/bin/bash

python3 manage.py migrate;

python3 manage.py makemigrations;

python3 manage.py migrate;

python3 manage.py collectstatic --clear;

python3 manage.py createsuperuser --no-input;

gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;

RECAP

In the directory where my Dockerfile and docker-compose.yml file are:

  1. sudo docker-compose build app
  2. sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created
  3. sudo docker-compose down
  4. sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created AGAIN. I'm trying to avoid this.

I'm new to docker-compose and any help is really appreciated thanks.

1
  • 2
    Have your entrypoint script create a flag file. Check for the existence of that file when the script starts, and skip the initialization logic if it does.
    – larsks
    Commented Jul 4, 2022 at 13:12

1 Answer 1

0

A dirty but simple way would be to ignore the error of the createsuperuser command by changing it to python3 manage.py createsuperuser --no-input || true;.

This might even be the solution you prefer, because if the variables for docker-compose change, a new superuser would be created with the changed values.

#!/bin/bash

python3 manage.py migrate;

python3 manage.py makemigrations;

python3 manage.py migrate;

python3 manage.py collectstatic --clear;

python3 manage.py createsuperuser --no-input || true;

gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.