Dev DBs with Docker

Published on

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.

apt install docker.io docker-compose

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

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

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

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

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

chmod 600 ~/.pgpass

I literally have an uncle named Bob.

UPDATE

mongo-tools got all disjointed from mongo in more recent versions. If you are not on an LTS of Ubuntu you will need to go get it and install it rather than being able to just install from package repositories

for newer Ubuntu I also had to add my user to the docker group

sudo usermod -a -G docker $USER