Docker, Inc. has published a detailed tutorial on Dockerizing PostgreSQL. To keep the length of this tutorial manageable, we will simply use a publicly available Docker image hosted on Docker Hub. We leave Docker’s official example as an exercise for readers who want a deeper understanding of how Docker containerizes the PostgreSQL service. The goal of this section is simply to introduce some basic Docker commands and frequently used command options.
Please go through the steps outlined under Preflight Checks to ensure that the system is properly configured.
Search for a PostgreSQL 9.3 Docker image that has at least 5 stars.
Host% docker search --stars=5 "postgresql-9.3" # -s, --stars=0 Only displays with at least x stars NAME DESCRIPTION STARS OFFICIAL helmi03/docker-postgis PostGIS 2.1 in PostgreSQL 9.3 18 jamesbrink/postgresql A simple PostgreSQL 9.3 contai 6 kamui/postgresql PostgreSQL 9.3 with configura 5
We will use the
kamui/postgresqlimage throughout this tutorial.
Launch a PostgreSQL service container from the
Host% docker run -p ::5432 \ --name db \ -e POSTGRESQL_DB=hartl \ -e POSTGRESQL_USER=docker \ -e POSTGRESQL_PASS=docker \ kamui/postgresql # -p Publish a container's port to the host. The :: notation # delegates the host port selection to Docker. # --name Assign a name to the container. # -e Set environment variables.
Make sure the PostgreSQL Docker container is up and running with
docker ps -a. In this example, Docker has mapped
port 49153on the Boot2Docker virtual machine to
port 5432. on the newly started
Host% docker ps -a CONTAINER ID IMAGE STATUS PORTS NAMES bd1f9fb66975 kamui/postgresql Up 49153->5432 db # -a, --all=false Show all containers. Only running containers are # shown by default.
Confirm that the PostgreSQL database container may be accessed locally. Since the PostgreSQL database container was launched with environment variable
POSTGRESQL_PASSset to docker, that will be the password in this example.
Host% psql -h localhost -p 49153 -U docker hartl Password for user docker: psql (9.2.4, server 9.3.4) WARNING: psql version 9.2, server version 9.3. Some psql features might not work. Type "help" for help. Hartl=#
Because we had forwarded ports in the
49000..49900range from the Mac OS X host to the Boot2Docker virtual machine, the dockerized PostgreSQL service may be accessed from the Mac OS X host with
To stop the PostgreSQL database container, press
Ctrl-cin the console or run
docker stop <container_id>or
docker stop <container_name>in another console with its environment variable
DOCKER_HOSTset. In this example, the PostgreSQL database container ID is
Host% docker stop bd1f9fb66975 # OR Host% docker stop db # OR if the alias "dlc" has been defined to be "docker ps -l -q" Host% docker stop `dlc`
Since the PostgreSQL database container was not launched with the
--rmoption, the exited container will show up when
docker ps -ais run.
Host% docker ps -a CONTAINER ID IMAGE STATUS PORTS NAMES bd1f9fb66975 kamui/postgresql Exited 49153->5432 db
Remove the exited container.
Host% docker rm bd1f9fb66975 # OR Host% docker rm db # OR if the alias "dlc" has been defined to be "docker ps -l -q" Host% docker rm `dlc`
Docker, Inc. published a detailed tutorial to demonstrate how easy dockerizing system services like PostgreSQL can be. We launched a public Docker image built in a way that is similar to the tutorial, so we may introduce some basic Docker commands and frequently used command options. In the process, we showed how the Mac OS X host can connect to a dockerized PostgreSQL service on the Boot2Docker virtual machine.