I have a project in docker in which I have been struggling to generate some files from the dockerfile o docker-compose files.
I have a dockerized Django project with the following structure.
assets/
app/
db/
.gitignore (ignoring all files of the folder)
db.sqlite3
docker-compose.yml
Dockerfile
For the purpose of the post I just wrote a general structure to give an idea of how is the project and what I want to do.
What I was trying to do was to mount the database file as a volume (type: bind) but didn't work. What I did was to treat the folder db as a volume folder. Until here all works fine.
What I want to do is to generate the db.sqlite3 file when does not exist, run the migrations and run the command python manage.py collectstatic --noinput.
I tried creating an entrypoint file:
#/bin/sh
python manage.py collectstatic --noinput
db=/code/db/db.sqlite3
if [ ! -f "$db" ]; then
touch "$db"
fi
python manage.py migrate
I tried to call this file with ENTRYPOINT and CMD statement in the Dockerfile and none of them work. I also tried to run the command touch from the Dockerfile and the result was the same.
If your container has an entrypoint, then when that entrypoint exits, the container is done and docker ps
will show an "exited" status. If it also has a command, the entrypoint needs to take responsibility for running it. The most common way is to run it as the last line of an entrypoint script
#!/bin/sh
...
exec "$@"
This line is missing from the entrypoint you show in your question, so your container is only running the initial setup and migrations, but then is exiting instead of actually launching the server.