Running Development Databases with Docker

A lot of the projects I work on use multiple database engines. SQL Server or Mongo for persistence, Redis for caching, Elastic for search, etc. Setting all of them up whenever I configure a development environment can be a hassle. Turns out Docker solves this problem very well.

Docker not really explained

If you don't know what Docker is I recommend checking out this intro article. Ideally, I'd convert my projects to all use Docker containers, but a lot of them aren't .Net Core. That doesn't mean Docker isn't useful when developing non-Docker projects. I can spawn all the database engines for my projects without having to install them on my machine. Here is a quick reference for getting a number of database engines running at the same time.

Getting started with automated databases

To get started with all of this we need to install Docker. Here's a link to install Docker on your machine. Once you've done that, open up a command prompt and get to business.

Redis

Running a Redis instance is as easy as running one command:

docker run --name redis -p 6379:6379 -d redis

Docker automatically downloads a Redis image, and it runs in the background for you to develop against. Awesome, right? To see what containers are running just type a command.

docker container ls -a

If you want to stop it you can run another command.

docker container stop redis

Removing the whole container is simple as well.

docker container rm redis

Re-run the container list command and you can see it's gone. Let's try another database engine.

MongoDB

Turns out most of the common database engines exist as Docker containers. We can run Mongo quickly by doing a similar command.

docker run --name mongo -p 27017:27017 -d mongo

You can see the container running with the previous commands. There are also some simple ones for starting and stopping the container without removing it altogether.

docker container stop mongo
docker container start mongo

Pretty cool, right? Quickly spawning up database instances without running complicated installs.

Elasticsearch

A few engines are a bit more difficult. Elasticsearch, for example, uses a public repository that is different from the default one on the Docker site. This requires you to reference the location directly.

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -d docker.elastic.co/elasticsearch/elasticsearch:5.6.3

The default Docker image comes with X-Pack security installed so authentication gets a bit more complicated. We can get rid of it though with a couple commands directly on the Docker container.

docker exec -i -t elasticsearch /bin/bash
elasticsearch-plugin remove x-pack
exit
docker restart elasticsearch

SQL Server

Even Microsoft has gotten onboard with Docker. We can spawn up a SQL Server instance on a cross-platform Docker container. Yes, you can run SQL Server on OSX. Make sure you have 4GB of RAM allocated to Docker and you can quickly run SQL Server.

docker run --name mssql -p 1433:1433 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -d microsoft/mssql-server-linux:latest

Tying it all together with Docker Compose

This is all useful, but running all these commands is still a bit of a pain. Even more so if you do it frequently. Luckly, Docker knows this and created Docker Compose. You can create a file called docker-compose.yml and fill it with this:

version: '3'
services:
  redis:
    image: 'redis'
    ports:
      - 6379:6379
  mongo:
    image: 'mongo'
    ports:
      - 27017:27017
  elasticsearch:
    image: 'docker.elastic.co/elasticsearch/elasticsearch:5.6.3'
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
  mssql:
    image: 'microsoft/mssql-server-linux:latest'
    ports:
      - 1433:1433
    environment:
      - ACCEPT_EULA=Y
      - SA_PASSWORD=yourStrong(!)Password

This file says run Redis, Mongo, Elastic, and MSSQL at the same time and expose all the ports out of the container. You can start all these by running one simple command.

docker-compose up -d

This will auto download all the images and spawn all the containers at the same time in the background. Check to see they're running by typing docker container ls -a. Now you can stop them all at the same time with a single command.

docker-compose stop

Once they're downloaded and have run before quickly start them again.

docker-compose start

Or, and this is awesome, you can take everything down and remove it all with a single command.

docker-compose down

I didn't go deep into Docker here because it does a lot of things. Hopefully, this quick intro can give you Docker value right out of the gate. Happy Coding.