Back up the Docker-based PostgreSQL database

[Editor's Note] This article is a basic introductory article, the author describes how to back up the Docker-based PostgreSQL database. Through this article you can learn the construction of the Docker container and run the commands and take you through the basics of the Docker container.

I found that many beginners are concerned about Docker can solve some of the existing problems, of course, before the solution can still be used. So the subject of this article is to use Docker to back up your relational database. I'll show you how to use Docker to back up PostgreSQL through concrete examples.

If you have a PostgreSQL service running in Docker, I would like to say that it would be nice to combine these two very cool software together But you are a smart person, you know you need to back up your own database. But how did it? PostgreSQL runs in Docker and can not access data within the container.

In simple terms, you can run pg_dump on a PostgreSQL instance. Specific to the current problem, you can run in three ways:

First in the container. According to your Docker version, use nsenter or docker exec to run the shell command in the container and share the dump data into the volume:

  Docker exec postgres pg_dump -h db -f /shared/backup.sql 

But we can do better, the ideal process is no need to start the shell in the container can be directly connected to PostgreSQL. There are two ways to achieve:

First, you can use a containerized way. Create a pg_dump container and connect it to the PostgreSQL container at run time. Here is the Dockerfile for the pg_dump container:

  FROM debian: wheezy 

RUN apt-get update -y && \
Apt-get install -y postgresql-client && \
Apt-get clean -y

ENTRYPOINT ["/ usr / bin / pg_dump"]

Use the docker build command to build:

  $ Docker build -t pg_dump - <Dockerfile 

Note: It is not necessary to build a container that runs pg_dump. You can include the pg_dump container directly in the PostgreSQL container. We are here just to practice.

Build and run:

  $ Docker run -it --link postgres: db pg_dump -h db 

Another solution is the Docker exposed container on port 5342, and we run the pg_dump command directly.

Original link: Backuping PostgreSQL with Docker (translation: Chrysanthemum under the revised school: Li Yingjie)

Heads up! This alert needs your attention, but it's not super important.