i've seen in the docs and many solution to initialize a postgres container with a database using an init.sql as mentioned in this question:
COPY init.sql /docker-entrypoint-initdb.d/10-init.sql
The problem is that my database data in .sql
is to large(8GB). This makes the dump process to long and the file to heavy. I was trying to make something similar to this approach generating a .dump
file with (540mb).
I have 2 containers to run my django project. A Dockerfile for my python environment and postgres image. My docker files are shown in the end of this question. So far i've managed to run the dump in my container with these steps:
- I add my
.dump
file to my container in docker-compose - build my containers
- Go inside my postgres container and execute a
pg_restore
command to dump my database. - Then go to my python/django container to execute a migrate command.
- And finally run server.
This works but its not automated. How can i automate this problem in my docker files so i don't need to execute these commands manually?
Bellow are my files and the commands i required to run my .dump
file on postgres container:
generate and add ./project-dump/latest.dump
to my project
docker-compose up -d web db #build containers
docker-compose exec db bash # go in my postgres container
pg_restore --verbose --clean --no-acl --no-owner -h {pg_container_name} -U postgres -d database_dev
exit # exit postgres container
docker-compose exec web bash # go in my python/django project container
./manage.py migrate # migrate database
./manage.py runserver 0.0.0.0:3000 #run server
Dockerfile:
FROM python:3.7.3-stretch
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
COPY init.sql /docker-entrypoint-initdb.d/10-init.sql
RUN python manage.py migrate
EXPOSE 3000
docker-compose.yaml
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_DB: database_dev
POSTGRES_PASSWORD: secret
ports:
- "5432:5432"
volumes:
- ./project-dump/:/var/www/html
web:
build: .
command: python3 manage.py runserver 0.0.0.0:3000
volumes:
- .:/django-docker
ports:
- "3000:3000"
depends_on:
- db
init.sql
CREATE DATABASE database_dev;
GRANT ALL PRIVILEGES ON DATABASE database_dev TO postgres;