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:
sudo docker-compose build app
sudo docker-compose up
-> initial migrations are applied, static files are collected, superuser createdsudo docker-compose down
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.