How to add a persistant volume

We create a volume with

$ sudo docker volume create pgdata

and we call docker run

$ sudo docker run -p 5432:5432 -d \
    -e POSTGRES_PASSWORD=secret \
    -e POSTGRES_USER=postgres \
    -e POSTGRES_DB=postgres \
    --name postgres11 \
    -v pgdata:/var/lib/postgresql/data \
    postgres

5432 is the usual port number for PostgreSQL and we tunnel the port of the host into the same port within the container.

The parameter -v maps this volume with the standard location of data in PostgreSQL cluster

/var/lib/postgresql/data is the usual location of PostgreSQL data files. Those files are located in the container. To know the location of the files on the host, we inspect the volume

$ sudo docker volume inspect pgdata pgdata

that gives

...
"Mountpoint": "/var/lib/docker/volumes/pgdata/_data"
... 

We do a ls /var/lib/docker/volumes/pgdata/_data on the host and we see the same files !

We have a look on the running container

$ sudo docker ps

that gives

CONTAINER ID    IMAGE       COMMAND                  CREATED             STATUS              PORTS                           NAMES
c9159dae8aae    postgres    "docker-entrypoint.s…"   14 minutes ago      Up 14 minutes       0.0.0.0:5432->5432/tcp          postgres11

and we open a psql session on it

$ sudo docker exec -it c9159dae8aae psql -U postgres postgres

and we create a database, a table and a record in it

create database window-functions;

create table test (a text);

insert into test(a) values('hello');

select * from a;

and psql answers

  a   
-------
 hello
(1 row)

and we quit psql

\q

We stop the container (or we do a shutdown our computer)

$ sudo docker stop c9159dae8aae

We start it again (to list the containers, do a sudo docker ps -a after a shutdown because sudo docker ps gives running containers only)

$ sudo docker start c9159dae8aae

We come back in psql

$ sudo docker exec -it c9159dae8aae psql -U postgres postgres

and we connect to our database window-functions

postgres=# \c window-functions

psql answers

You are now connected to database "window-functions" as user "postgres".

and we type

window-functions=# select * from test;

and we obtain

   a   
-------
 hello
(1 row)

The deal is in the bag !

The CONTAINER IDs are persistent. Then we can encapsulate the docker commands in a script.
And this containerized version of PostgreSQL coexists with the local one as we can see in the list of running processes.

$ ps -ef | grep postgres

gives

UID        PID  PPID  C STIME TTY          TIME CMD

...
postgres  1661     1  0 16:41 ?        00:00:00 /usr/lib/postgresql/11/bin/postgres 
       -D /var/lib/postgresql/11/main -c config_file=/etc/postgresql/11/main/postgresql.conf
postgres  1736  1661  0 16:41 ?        00:00:00 postgres: 11/main: checkpointer   
postgres  1737  1661  0 16:41 ?        00:00:00 postgres: 11/main: background writer   
postgres  1738  1661  0 16:41 ?        00:00:00 postgres: 11/main: walwriter   
postgres  1740  1661  0 16:41 ?        00:00:00 postgres: 11/main: autovacuum launcher   
postgres  1742  1661  0 16:41 ?        00:00:00 postgres: 11/main: stats collector   
postgres  1743  1661  0 16:41 ?        00:00:00 postgres: 11/main: logical replication launcher   
999       8637  8615  0 17:47 ?        00:00:00 postgres
999       8719  8637  0 17:47 ?        00:00:00 postgres: checkpointer  
999       8720  8637  0 17:47 ?        00:00:00 postgres: background writer  
999       8721  8637  0 17:47 ?        00:00:00 postgres: walwriter  
999       8722  8637  0 17:47 ?        00:00:00 postgres: autovacuum launcher  
999       8723  8637  0 17:47 ?        00:00:02 postgres: stats collector  
999       8724  8637  0 17:47 ?        00:00:00 postgres: logical replication launcher  
999       8973  8637  0 17:49 ?        00:00:28 postgres: mchl postgres 172.17.0.3(45606) idle
...

The user 999 is the owner of the postgres processes launched by docker and the user postgres is the user of the local PostgreSQL cluster.

The PPID (Parent Process ID of the postgres process owned by 999 is 8615 : the docker process.

The PPID of the postgres process owned by postgres is 1 : /sbin/init, the father of all processes.

To run a shell inside the container, we do

$ sudo docker exec -it c9159dae8aae /bin/bash

and to run psql inside the container, we do

$ sudo docker exec -it c9159dae8aae psql -U postgres -d postgres

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s