Stop installing postgres on your laptop : use Docker instead

Tired of managing a full-blown DB server on your development machine ?

Let’s give Docker a try !

Getting started with Docker

First, if your development machine is not running Linux (ie if you’re running windows or mac os x), it’s easier to use boot2docker to get started, since it will take care of installing:

  • VirtualBox (if you already have it installed, don’t worry, it won’t overwrite your current configuration)
  • A Linux VM (based on the latest boot2docker.iso, that will run virtusalized in Virtual Box)
  • Boot2Docker Management Tool v1.4.1
  • Docker Client v1.4.1

Once you’re able to successfully run :

you’re ready to proceed with PostgreSQL

PostgreSQL running on Docker for development

Let’s grab the latest version of the PostgreSQL image from Docker official repository, and let’s start the database :

A few explanation about this command line :

  • -p 5432:5432 : means that we port forward the container (effectively running the db) 5432 port to the host (in my case, running boot2docker, the VirtualBox vm named boot2docker-vm) 5432 port – since the postgres image EXPOSE the port 5432 for its service, we will be able to connect to the db using this port
  • –name : the name of your container – you can see all your containers and their names using the

    command
  • -e : sets an environment variable (in our case the postgres user password will be « password »)
  • -d : runs the container in detached mode
  • postgres : the name of the image (by default the latest version)

Let’s have a look at the newly created container :

We just got confirmation that our container, named postgres, is running the latest postgres image, and does the proper port forwarding.

Oh ! one last thing ! If using boot2docker, you have to know the ip address of the container’s host :

That’s it ! You’re all set ! You just installed a PostgreSQL db on your laptop without polluting your environment (it’s dockerized !), and you can access it from your app connecting to 192.168.59.103 on port 5432 as postgres with the password « password »

Interesting things to know

Client tools

I used to run PostgreSQL on a mac, using homebrew, and along with the server came the client tools such as pgql, pg_restore, pg_dump.

Now I use the ones bundled with pgAdmin3, on a typical mac install they are located in /Applications/pgAdmin3.app/Contents/SharedSupport :

Stopping / starting your dockerized PostgreSQL server

When you start working with your DB, you may want to not loose your changes. Hopefully, Docker can save the changes brought to your container.

If your container is running, find its id :

Then stop it :

or since the container is named :

To restart it, it’s just as easy as :

You can check it’s back on with :

Another approach (more production oriented approach) would be to use data volume containers

Boot2docker shutting down

From times to times (when my laptop goes to sleep or connect to another wifi hotspot) my boot2docker virtual box vm shuts itself down : so I restart it with

 

4 réflexions au sujet de « Stop installing postgres on your laptop : use Docker instead »

  1. Hi Anthony,

    If boot2docker offers a quick win on Windows & MacOS, it lacks flexibility… For personal projects and at work, I rely on VirtualBox/Vagrant/CoreOS. CoreOS is very light and allows with etcd to setup a network of many VMs (many on your machine, or yours connected to remote ones) with the benefit of being nearer from a production configuration. My current development environment at work involves a MongoDB cluster (2 nodes), 3 Node.js servers, 2 memcached nodes, one HAProxy, one Graphite server, and one Piwik server. With the help of Vagrant and a good provision file (most of the Docker commands are remotely executed over ssh into the VMs), the setup is just a matter of running vagrant up… After ~15 minutes, the database is bootstrapped and HAProxy is ready to dispatch my requests 😉

    Next stop depends on Kubernetes: is it mature enough to orchestrate the deployment according to the traffic load?

    If you don’t like CoreOS, check this post on Docker blog for alternatives.

    A+, Dom

  2. Hey there Dom,
    Wow, I took 6 months to anwer you, but finally !
    Thanks for your comment !
    So boot2docker has matured over 6 months, and even if your use case (10 containers !) is pretty « heavy » for a laptop, it should work !
    Of course you will need a beefy boot2docker VM (by default it only comes configured with 1GB, but through VirtualBox UI you can change that to 8GB for example)
    I suggest you use docker-compose to orchestrate all your containers.
    If you prefer to run this environment on a dedicated server, or « the cloud » you can go the Docker Engine way, using docker commands (and also docker compose commands) piped to your remote Docker host.
    I have not yet used Kubernetes, for now Docker Engine + Swarm + Docker compose almost fulfilled all my needs.
    Except a new blog post soon to describe that !
    Au plaisir de te relire,

  3. The title for this article is very misleading and the article itself falls completely flat in living up to the headline.

    Firstly I would still be installing Postgres on my laptop (just inside a container). Secondly, you haven’t address any reasons why I should adopt your approach. Thirdly, you haven’t address any issues related to using docker, like the additional VirtualBox overhead (on Macbook Air /w 2gb ram this is a major issue). Lastly, I might not be be as critical if this article was presented as a How-To, rather than « Stop doing that ».

    It seems like you just wanted to use Docker for the sake of using Docker, which is fine, but don’t tell others to do the same without presenting valid reasons.

  4. Hello Mario !
    That was a passionate comment !
    I don’t agree with you though : running the dev db in a container provides value.
    When I wrote this article, I was helping someone setting up a Ruby on Rails env on Windows; it was really, really hard; I can tell you that relying on a containerized db helped us a lot : one command line and you’re done (compared to installing dlls and stuff in the windows registry with plenty other tools you probably don’t need)
    Playing with volumes or simply containers, you can even easily snapshot the state of your db.
    When you say Docker is heavyweight, well, you were kind of right a few months ago; but now, go and try Docker 4 Mac or Docker 4 Windows, that integrates nicely with the OS provided VM solution (hyperkit for mac os) – defintely not a 2GB ram VM anymore.
    Actually, I wonder why someone would install a full blown db on his/her laptop today
    Finally, I used to use Docker for the sake of using Docker – true- but now, it’s a valuable development tool (containerizing builds, infra dependencies, etc.)

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée.

Le temps imparti est dépassé. Merci de saisir de nouveau le CAPTCHA.