Getting the Postgres Docker Image
To pull down an image for the latest stable release of Postgres, simply run
docker pull postgres
This will pull down the latest stable release Postgres image from the official Postgres docker hub repository. To pull down a version other than the latest stable release, we can provide an appropriate image tag name to the docker pull command above
docker pull postgres:[tag_you_want]
Create a Directory to Serve as the Local Host Mount Point for Postgres Data Files
If we want to persist data generated by the Postgres instance running inside a container beyond the container’s lifecycle, we need to map a local mount point as a data volume to an appropriate path inside the container. Typically I create a volumes folder (we can give the folder any name we like) in my home directory and then create subfolders for each of the applications I need to create data volume mount points for.
mkdir -p $HOME/docker/volumes/postgres
Run the Postgres Container
Starting the Postgres container is as simple as running the docker run command
docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
We have provided several options to the docker run command:
- — rm: Automatically remove the container and it’s associated file system upon exit. In general, if we are running lots of short term containers, it is good practice to to pass rm flag to the docker run command for automatic cleanup and avoid disk space issues. We can always use the v option (described below) to persist data beyond the lifecycle of a container
- — name: An identifying name for the container. We can choose any name we want. Note that two existing (even if they are stopped) containers cannot have the same name. In order to re-use a name, you would either need pass the rm flag to the docker run command or explicitly remove the container by using the command docker rm [container name].
- -e: Expose environment variable of name POSTGRES_PASSWORD with value docker to the container. This environment variable sets the superuser password for PostgreSQL. We can set POSTGRES_PASSWORD to anything we like. I just choose it to be docker for demonstration. There are additional environment variables you can set. These include POSTGRES_USER and POSTGRES_DB. POSTGRES_USER sets the superuser name. If not provided, the superuser name defaults to postgres. POSTGRES_DB sets the name of the default database to setup. If not provided, it defaults to the value of POSTGRES_USER.
- -d: Launches the container in detached mode or in other words, in the background.
- -p: Bind port 5432 on localhost to port 5432 within the container. This option enables applications running out side of the container to be able to connect to the Postgres server running inside the container.
- -v: Mount $HOME/docker/volumes/postgres on the host machine to the container side volume path /var/lib/postgresql/data created inside the container. This ensures that postgres data persists even after the container is removed.
Connect to Postgres
Once the container is up an running, connecting to it from an application is no different than connecting to a Postgres instance running outside a docker container. For example, to connect using psql we can execute
psql -h localhost -U postgres -d postgres
Hopefully this post has demonstrated how easy and straightforward it is to get up and running with Postgres in docker. For most other applications, the process is just as simple. So next time you need to install a piece of software, think docker pull before you reach for brew install, yum install, apt-get install or whatever the command is for your systems package manager 🙂
from My Reading List: Read and Unread http://bit.ly/2GUnTVi