Dev DBs with Docker

I’ve been using Docker for my dev machine databases for a few years now, and it works really well. Upon doing a fresh install of Ubuntu I thought I would document how I set it up.

Using Docker has some advantages - much easier to get the versions I want and have them be the same across different machines. This is especially noticable when updating to latest Ubunutu on my dev laptop for instance. I would have to do a fair amount of work to get postgres 9.6 and mongo 3.4 since ubuntu packages include more recent versions.

I won’t go into all the advantages of containerization, suffice to say it is popular for good reasons, but couple quick notes on advantages over standard package install; the DB servers are a little easier to not have running except when I am using them, and if and when we want to change versions, using containers also makes that a lot less painful potentially. Makes me wonder if there are things like sdkman, nvm, or rvm even available for DBs? Anyway. Docker and Docker Hub fit the bill.

This is for a postgres and mongo dev db setup:

install docker - straight from the package repos - no need for all the hoops the docker website tries to get you to jump through.

1
apt install docker.io docker-compose

add docker to suders so don’t have to sudo all the things

1
sudo groupadd docker

get your docker-compose.yml file in place. whereever you like - maybe something like this

mkdir ~/dev/projectName/devDBs/
cd dev/projectName/devDBs/
touch docker-compose.yml
nano docker-compose.yml

content in my case looks something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: "3"
services:

sqldb:
image: postgres:9.6-alpine
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
# only need environment lines for initial setup
#environment:
# POSTGRES_PASSWORD: example

mongo:
image: mongo:3.4
volumes:
- mongodata:/data/db
ports:
- "27017:27017"

volumes:
pgdata:
mongodata:

in the case of PostgreSQL it has an init script it will run if you provide a password to get it kicked off
so comment out that environment line and give it a run

1
docker-compose up

should see it do the magic stuff : Ctrl+C to stop

you can comment back out that environment line and from now docker-compose up should have your DBs up and humming in no time.

if you need some other tools to work with dbs like dump and restore tools

1
sudo apt install mongo-tools postgres-client

for pg_dump and pg_restore you will probably want to create .pgpass file touch ~/.pgpass

and put a line like this in it:

localhost:5432:myDevDbName:postgres:mySecrt

https://www.postgresql.org/docs/current/libpq-pgpass.html

then gotta mod the permissions on it a bit to make postgres happy

1
chmod 600 ~/.pgpass

I literally have an uncle named Bob.