Docker. How to fill volume at startup
I use docker-compose to run a Django project.
My goal is for docker to start filled with existing data (statics, database, and all the contents of the media directory).
Currently, statics are collected and distributed via volume. The database starts from a prepared dump.
/media directory added to .dockerignore
The question is, how to correctly fill the media volume from the media directory when you first run docker compose (that is, when the media volume is still empty)?
docker-compose.yaml file:
volumes:
mysql_data:
static:
media:
services:
db:
image: mysql:8.0
restart: always
volumes:
- mysql_data:/var/lib/mysql
- ./database_dump.sql:/docker-entrypoint-initdb.d/dump.sql
environment:
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
env_file:
- .env
backend:
build: ./
depends_on:
- db
restart: always
volumes:
- static:/app/static/
- media:/app/media/
env_file:
- .env
nginx:
build: ./gateway/
depends_on:
- backend
ports:
- "8000:80"
restart: always
volumes:
- static:/static/
- media:/media/
Dockerfile:
FROM python:3.11
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV DJANGO_SETTINGS_MODULE='app.settings'
RUN apt-get update && apt-get install -y netcat-openbsd
COPY ./requirements.pip ./
RUN pip install --upgrade pip && pip install -r requirements.pip --no-cache-dir
COPY . .
RUN python manage.py collectstatic --no-input
RUN chmod +x ./entrypoint.sh
EXPOSE 8000
ENTRYPOINT ["./entrypoint.sh"]
entrypoint.sh:
#!/bin/sh
echo "Waiting for MySQL..."
while ! nc -z $MYSQL_HOST $MYSQL_PORT; do
sleep 0.1
done
echo "MySQL started"
python manage.py migrate --no-input
gunicorn app.asgi:application \
--bind 0.0.0.0:8000 \
--workers 8 \
--worker-class uvicorn.workers.UvicornWorker
exec "$@"