1

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:

  1. I add my .dump file to my container in docker-compose
  2. build my containers
  3. Go inside my postgres container and execute a pg_restore command to dump my database.
  4. Then go to my python/django container to execute a migrate command.
  5. 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;
Marcelo Fonseca
  • 1,705
  • 2
  • 19
  • 38
  • There are multiple ways to fix your issues, but first I would like to understand why you are doing the steps you do. So you have a database that you start from a dump, then you take a dump of the same and run a migrate in a different container... Did I get this wrong? Also the whole idea of containers is to run the database and the application in 2 separate containers. You are putting all in one container in the end, right? – Mihai Apr 09 '19 at 07:16
  • thanks for the response @Mihai ! I guess i dont really need to migrate in my application with my dump working. i was testing diferent ways to run my app with the containers and some steps could be removed. I should also change my dump to also create de db and remove the init.sql file. I have 2 containers. The database is in DB container and my python app is in WEB container. – Marcelo Fonseca Apr 09 '19 at 13:03

0 Answers0