“black sperm whale” by Sho Hatakeyama on Unsplash
Since the advent of Docker, I rarely find my self directly installing development software on my local machine. Be it database servers (i.e Postgres), caching systems (i.e Redis, Memcache) or messaging systems (i.e Kafka) — I almost always try to find or build an appropriate docker image to use during development.
Installing software is hard. And it has nothing to do with your expertise as a developer. We have all seen our fair share of version clashes, esoteric build failure messages and missing dependency errors each time we embarked upon the task of installing a new software to use. We have spent countless hours copy pasting snippets of code from Stack Overflow onto our terminal and running them with the hope that one of them will magically resolve install issues and make the software run. The result is mostly despair, frustration and loss of productivity.
Docker provides a way out of this mess by reducing the task of installing and running software to as little as two commands (docker run and docker pull). In this post we will see this process in action by taking a step by step look at how easy and simple it is to setup a Postgres installation with docker.
Note that this is not a tutorial on docker. If you are curious about the inner workings of docker and all that you can do with it, I encourage you to surf the web as there is plenty of quality material out there on the topic.
This post assumes that you have a valid docker account and a docker daemon running. If you are completely new to docker, I would recommend starting here.
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]
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
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:
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 :)