Dockerfile – Import Postgres Database Dump

I was recently working with an application to dockerize it, essentially writing a Dockerfile for it. The application had a Python Flask frontend and postgresql as the backend database. During the planning stage, we’d determined that we’re not going to require any container / docker orchestration (such as kubernetes / swarm) because we’ll only need one instance of this application running. Logically, we ended up deciding on docker-compose.

As part of the process, I received a database dump that was supposed to be imported in the database for the application to work. Now the job here was to actually import the dump in a vanilla postgresql container which will then be clubbed with the frontend in docker-compose. While the task is not that complexed, I guess it’ll help someone out.

Here’s the dockerfile that was used and the explanation

FROM postgres:12.3
COPY my_database_dump.sql /home/tempdir/my_dump.sql
COPY /docker/entrypoint-initdb.d/

Here’s the one-liner

set -e
psql -U youruser -d myapp < /home/tempdir/my_dump.sql

Now the first question is, why a shell script? I mean we could just put it in the docker file.

The reason to use the shell script is that docker uses /bin/sh by default. In some cases, sh is known to have issues with modern commands. You could use “SHELL” instruction in dockerfile as an alternative, but in this case I just figured we could write a shell one-liner and ensure that it uses /bin/bash instead of sh.

Second question is, how do you actually ensure that the database dump was import AFTER the database container is created? If you read the documentation of postgres on Dockerhub, it says that you can put your scripts in “/docker/entrypoint-initdb.d/” and they will be executed at the runtime and not at buildtime. So that’s what we did.

I hope this clarified any doubts you had and happy learning!

One reply on “Dockerfile – Import Postgres Database Dump”

Leave a Reply

Your email address will not be published. Required fields are marked *