“black sperm whale” by on Sho Hatakeyama 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 ( and ). 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. docker run docker pull 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 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 . To pull down a version other than the latest stable release, we can provide an appropriate image tag name to the command above repository docker pull 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 folder (we can give the folder any name we like) in my directory and then create subfolders for each of the applications I need to create data volume mount points for. volumes home mkdir -p $HOME/docker/volumes/postgres Run the Postgres Container Starting the Postgres container is as simple as running the command docker run 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 command: docker run : 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 flag to the command for automatic cleanup and avoid disk space issues. We can always use the option (described below) to persist data beyond the lifecycle of a container — rm rm docker run v 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 flag to the command or explicitly remove the container by using the command — name: rm docker run docker rm [container name]. Expose environment variable of name with value to the container. This environment variable sets the superuser password for PostgreSQL. We can set to anything we like. I just choose it to be for demonstration. There are additional environment variables you can set. These include and sets the superuser name. If not provided, the superuser name defaults to sets the name of the default database to setup. If not provided, it defaults to the value of -e: POSTGRES_PASSWORD docker POSTGRES_PASSWORD docker POSTGRES_USER POSTGRES_DB. POSTGRES_USER postgres. POSTGRES_DB POSTGRES_USER. Launches the container in detached mode or in other words, in the background. -d: : 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. -p : 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. -v 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 we can execute psql 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 before you reach for or whatever the command is for your systems package manager :) docker pull brew install, yum install, apt-get install