Posted on 28 Jun 2018
This is the third article of the Getting started with Docker series. Here I want to show you how to use Docker to create a container where you can install PostgreSQL.
The first step of this tutorial is to write the Dockerfile for our PostgreSQL application. This is the code of our Dockerfile.
Line 1 specifies that Ubuntu 16.04 is the starting point for our image. On line 2 we have the RUN command that installs the PostgreSQL package plus some additional packages containing useful tools for developing and debugging (i.e. ping, nc, vim, and netstat). Line 4 contains another RUN command we will use to create the PostgreSQL data and log directories plus a folder that will contain the startup scripts. Line 9 has the USER command that switches the current user to postgres. From this moment on all the commands will be executed by the postgres user. On line 10 the COPY command copy all the startup scripts in the container folder /usr/local/bin/cluster/postgresql. Line 11 the EXPOSE command exposes the 5432 port that is the port on which PostgreSQL will listen. Finally, line 12 the ENTRYPOINT command specifies the script that will be launched at startup.
To create the image we define the script build_image.sh that will use the “docker build” command to create the image. Here the content of the script.
The image name is postgresql and you can see it with the “docker image ls” command. I created a second script called clean_image.sh to drop the image when it is not necessary anymore. Here the code of this script.
The Docker container is created with the script start_containers.sh:
The script uses the “docker create” command to create the container starting from the image postgresql. This command creates the container in a detached mode so you can access it or exit it without losing changes. The internal port 5432 on which PostgreSQL listens will be mapped on the same port on the host environment. The command “docker start” will start the container. The container can be destroyed by the script stop_container.sh that stop and remove the container.
The container is created and it is up and running, to access it use the following command.
As usual, you can get the CONTAINER_ID with the command “docker container ls” and use the one related to the postgresql image.
When Docker starts the container, it executes the script /usr/local/bin/cluster/postgresql/entrypoint.sh. This script runs the /usr/local/bin/cluster/postgresql/bin/entrypoint.sh script and wait for its completion.
The /usr/local/bin/cluster/postgresql/bin/entrypoint.sh script will be responsible to create the data directory (line 3), copy the configuration files (lines 5 and 6), and start PostgreSQL (line 8).
As a prerequisite to testing the Docker container, you need to install PostgreSQL client on your host system. I use Mac and the command to install it is:
Windows and Linux systems have an equivalent command you can search by google. Build the image and start the container:
Now you can access PostgreSQL with the command:
Once you logged in PostgreSQL you can create a database.
You can check its creation with the command \list. To quit from psql command line use the command \q.
In this article, we created a simple Docker container where we installed a PostgreSQL database server. You can download the sample code here in the postgresql folder. In the next article, I will explain how Docker networking works. This step is a prerequisite to creating our cluster.