Skip to content

Instantly share code, notes, and snippets.

@janjaali
Last active May 31, 2024 15:27
Show Gist options
  • Save janjaali/6021580003bb175860be5c20cfb03a7e to your computer and use it in GitHub Desktop.
Save janjaali/6021580003bb175860be5c20cfb03a7e to your computer and use it in GitHub Desktop.
Snapshot instruction for dockerized postgres

Create snapshot of dockerized postgres

The goal of this instruction is to enable users to create Docker images that contain a predefined postgres instance with existing schema and data. We will use the official docker postgres image and mostly docker's own commands.

The first step is to setup a docker container running postgres containing t# Create snapshot of dockerized postgres

The goal of this instruction is to enable users to create Docker images that contain a predefined postgres instance with existing schema and data. We will use the official docker postgres image and mostly docker's own commands.

The first step is to setup a docker container running postgres containing the schema and data that we want to include in the created snapshot:

docker run --name postgres-container -d postgres

Per default the postgres container stores any schema information and data in the mounted volume directory /var/lib/postgresql/data (ref). Thus docker commit does not take along this volume data we copy them to a separate file location:

docker exec -it postgres-container cp -r /var/lib/postgresql/data postgres-data

Now that we have copied the data to a different directory than the mounted one, we can commit the container instance and back up our data:

docker commit postgres-container postgres-with-data

The created image contains our backed up schema and data in the postgres-data image.

To start a container instance from this image we only have to tell postgres where it may find the data stored instead to use the default location. We use the PGDATA environment variable for this purpose here:

docker run —env PGDATA=postgres-data -d postgres-with-data

Why not "only" docker commit

Docker commit does not include any data contained in volumes mounted inside the container (docker commit).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment