How to run dev database via docker

Updated on: Mon Mar 29 2021

When working on pet projects I constantly need to some of postgresql or mongodb instance to run. For me the best way to achieve this without polluting the entire system by globally installed database engines is to use docker.

Run postgresql database via docker

First pull appropriate postgresql docker image. It's a lot of them in the list. Here I use some stable release of version 12. And I use it's alpine prefixed version to run it without any additional features, only stuff needed to run database is included into this image.

copied bash

docker pull postgres:12.3-alpine

To run a container:

copied bash

docker run --rm --name test-name -e POSTGRES_PASSWORD=123321 -p 8084:5432 postgres:12.3-alpine
  • --rm - goes for container's removal right after exit.
  • --name test-name - is to easy manage container by it's name without guessing it's PID
  • POSTGRES_PASSWORD=123321 - is default user password to access the database
  • -p 8084:5432 - map external 8084 to internal 5432 port. We will connect to port 8084 from our system
  • -d - this one is not included into current example but we can add it to start container in a daemon mode

Run mongodb via docker

copied bash

docker pull mongo:4.2-bionic

copied bash

docker run --rm --name test-mongo -p 27018:27017 -d mongo:4.2-bionic

Connection to mongo via nodejs in this case could be established without providing any username/password credentials and could look like this:

copied javascript

const client = new MongoClient(
  "mongodb://localhost:27018/?poolSize=20&writeConcern=majority",
  {
    useUnifiedTopology: true,
  }
);