Kirill Zonov

How to install and use PostgreSQL (or whatever you want) using Docker

October 08, 2017 | 4 Minute Read

Today I’ll talk about really trivial thing, but unexpectedly I found myself living without it for a while, but now I started using it and cannot stop anymore %) The thing is Docker. For those who lived past years on the Moon and have no idea what is it - short explanation. Docker is a tool, which helps you run some application(s) in an isolated environment (container). It means that it doesn’t pollute your global directories, namespaces and if you want - you may have various versions of one app in different containers. F.e. you have to maintain two versions of Redis because one of your microservices uses really out-of-date version and here Dockers is your best friend. All this speaking about ready to use containers, but you also can create your own containers with your app/microservice. It becomes really helpful when you want to give backend to your frontend developers and don’t want them to struggle with Ruby version managers, image magics, and other pesky tools.

In the current article, I’ll give a short introduction of how to install a DB locally using Docker containers hub. I think I’m late for about 3 years with this topic xD But I think there are still plenty of people who don’t use this approach and probably it will be the last drop for them and they’ll start using it.

1. Install the Docker itself.

Just go here to the official website and get the stable version. I found it’s the easiest and the most convenient way to install it. It installs like any OSX app. Just don’t forget to open it after install and you can also make it auto loadable on the system start up.

2. Install the DB.

I use PostgreSQL on one of my projects (honestly I’d like to use it almost everywhere) and will show you how to install and use it with Docker. Actually, on my daily job, I have Mongo, Postgres and Redis as databases and it makes it really convenient to use them together with the Docker. So first we go to the dockerhub to find the suitable image, here it is. Installation is straightforward:

$ docker pull postgres

It takes some time to download the image, be patient. Then to start it you can simply run:

$ docker run --name postgres -d postgres:latest

Where –name specifies the container name, you can write whatever you want here, -d specifies the image and the latest in my case means that I want to run the latest available version of the image, if there will be a new one on Dockerhub, it will be downloaded.

3. Managing the container

Here are few commands you need to know to work with Docker: How to list all running containers:

$ docker ps

There you can see only those containers which are running at the moment. If you need to see all registered containers - simply add the flag -a:

$ docker ps -a

It can be useful for starting containers after system restart. Let’s look at the most important attributes we have there: CONTAINER ID - your container id, I’ll show later how to use it to start and stop containers. STATUS - here you may see is your container running or not. Let’s now stop and start container again:

$ docker stop
$ docker start 0714498eefff

As you understand, 0714498eefff is my container id. Checking logs is just as elementary:

$ docker logs 0714498eefff

It can be really useful if you have a problem with container start, I personally had it few times.

4. Ports mapping

The last thing you have to know when starting using a Docker is port mapping or port exposing. When you start a docker you can see some port displaying to you, but keep in mind that it’s an internal port, you cannot connect to it. To be able to do so you have to explicitly map it to your machine’s port. Let’s first remove our container:

$ docker rm <span class="s3">0714498eefff</span>

Then start it again but with port mapping:

$ docker run --name postgres -d postgres:latest <span class="com">-p 5432:5432</span>

Hope you’ll have a fun time with it!