Tired of managing a full-blown DB server on your development machine ?
Let’s give Docker a try !
Contents
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 :
1 |
$ docker run helloworld |
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 :
1 2 |
$ docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=password -d postgres 1dece489d6d0594f0b173223e7cb260d26152d41ebe39c7f9da8441fcdb6bd62 |
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
1$ docker ps -a
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 :
1 2 3 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dece489d6d0 postgres:latest "/docker-entrypoint. 7 minutes ago Up 7 minutes 0.0.0.0:5432->5432/tcp postgres |
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 :
1 2 |
$ boot2docker ip 192.168.59.103 |
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 :
1 2 |
$ ls /Applications/pgAdmin3.app/Contents/SharedSupport branding docs i18n pg_dump pg_dumpall pg_restore plugins.d psql settings.ini |
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 :
1 2 3 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dece489d6d0 postgres:latest "/docker-entrypoint. 6 days ago Up 6 days 0.0.0.0:5432->5432/tcp postgres |
Then stop it :
1 |
$ docker kill 1dece489d6d0 |
or since the container is named :
1 |
$ docker kill postgres |
To restart it, it’s just as easy as :
1 |
$ docker start postgres |
You can check it’s back on with :
1 2 3 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dece489d6d0 postgres:latest "/docker-entrypoint. 6 days ago Up 1 seconds 0.0.0.0:5432->5432/tcp postgres |
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
1 |
$ boot2docker start |
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 withetcd
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 runningvagrant up
… After ~15 minutes, the database is bootstrapped and HAProxy is ready to dispatch my requestsNext 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
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,
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.
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.)
2 problems for me with this approach:
1. Shutting down the container will destroy any and all data that I’ve created, because of (2)
2. I’m running Windows 10 Home with Docker Tools and when I set a volume for postgres, I’ll get the next error when it reboots:
FATAL: could not open file « pg_xlog/000000010000000000000001 »: No such file or directory
This has to do with the container using a linux commands trying to bind a *nix folder to a Windows folder, from what I understand. Which is weird, because the Jenkins container runs just fine ¯\_(ツ)_/¯
This means I can’t have a folder on windows with my data