How to Keep Your Development Environment Clean

Tin Rabzelj
Tin RabzeljHire me

October 10, 2017

Imagine you're working on a project which includes a PostgreSQL database, Redis cache layer, Elasticsearch engine, Consul for dynamic configuration, and more. The last thing you want is to install all of these services on your local machine during development.

Enter Docker.

Docker is a tool for working with containers, which are isolated spaces where apps can run. It enables painless installation of services and sharing development environments with others. The same applies for production, amongst many other benefits.

Getting started

Install Docker and Docker Compose.

Basic example

Lets say you need a MySQL database. Simply run the following command.

docker run --name database -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql

This creates a container named database running a MySQL instance on port 3306. Environment variable MYSQL_ROOT_PASSWORD holds the root user's password.

To get container's IP address, run the following command.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' database

You can use MySQL CLI tool like this.

docker exec -it database mysql -u root -p

You could then run another container, with a different version of MySQL server, on port 3307 by specifying a tag.

docker run --name database2 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql:5.6

To find all available Docker images visit Docker Hub.

Using Docker Compose

This section shows how to set up a hypothetical development environment involving a MongoDB database, a back-end service in Go and a static frontend site. The end product is a website, which displays anonymous posts.


Inside project's root directory create a docker-compose.yaml file with the following content.

version: '3.3'
    image: 'mongo:latest'
    container_name: 'mongo'
      - '27100:27017'

This defines a MongoDB service called mongo.

In the same directory run this command to build it.

docker-compose up -d --build

Or by specifying the location of docker-compose.yaml file.

docker-compose up -d --build -f ./docker-compose.yaml

List currently running containers.

docker-compose ps
Name              Command             State            Ports
mongo mongod   Up>27017/tcp

Web service in Go

Define another service inside docker-compose.yaml, which depends on mongo.

version: '3.3'
    container_name: 'api'
    build: './api'
      - '8080:8080'
      - './api:/go/src/app'
      - 'mongo'
    image: 'mongo:latest'
    container_name: 'mongo'
      - '27100:27017'

The api service will be built using a custom "Dockerfile". Create it inside api directory. Name it exactly Dockerfile, without an extension.

FROM golang:1.8

WORKDIR /go/src/app
COPY . .

RUN go get
RUN go-wrapper download
RUN go-wrapper install

CMD [ "fresh" ]

This Dockerfile extends official golang image, installs the pilu/fresh package and all other dependencies using go-wrapper utility commands. The default command is set to fresh. This will enable live reloading for development.

Create main.go file inside api directory.

package main

import (

type Post struct {
  Text      string    `json:"text" bson:"text"`
  CreatedAt time.Time `json:"createdAt" bson:"created_at"`

var posts *mgo.Collection

func main() {
  // Connect to mongo
  session, err := mgo.Dial("mongo:27017")
  if err != nil {
    log.Fatalln("mongo err")
  defer session.Close()
  session.SetMode(mgo.Monotonic, true)
  // Get posts collection
  posts = session.DB("app").C("posts")

  // Set up routes
  r := mux.NewRouter()
  r.HandleFunc("/posts", createPost).
  http.ListenAndServe(":8080", cors.AllowAll().Handler(r))
  log.Println("Listening on port 8080...")

func createPost(w http.ResponseWriter, r *http.Request) {
  // Read body
  data, err := ioutil.ReadAll(r.Body)
  if err != nil {
    responseError(w, err.Error(), http.StatusBadRequest)
  // Read post
  post := &Post{}
  err = json.Unmarshal(data, post)
  if err != nil {
    responseError(w, err.Error(), http.StatusBadRequest)
  post.CreatedAt = time.Now().UTC()
  // Insert new post
  if err := posts.Insert(post); err != nil {
    responseError(w, err.Error(), http.StatusInternalServerError)
  responseJSON(w, post)

func responseError(w http.ResponseWriter, message string, code int) {
  w.Header().Set("Content-Type", "application/json")
  json.NewEncoder(w).Encode(map[string]string{"error": message})

func responseJSON(w http.ResponseWriter, data interface{}) {
  w.Header().Set("Content-Type", "application/json")

Rebuild containers.

docker-compose up -d --build

Try calling the /posts endpoint.

curl localhost:8080/posts -d '{"text":"hello"}'

Declare a new handler which reads all posts.

func readPosts(w http.ResponseWriter, r *http.Request) {
  result := []Post{}
  if err := posts.Find(nil).Sort("-created_at").All(&result); err != nil {
    responseError(w, err.Error(), http.StatusInternalServerError)
  } else {
    responseJSON(w, result)

Hook it up inside the main function.

r.HandleFunc("/posts", readPosts).

Without rebuilding containers, you should be able to call this endpoint.

curl localhost:8080/posts

Serving a static site with NGINX

Declare a new service inside docker-compose.yaml file called web.

  container_name: 'web'
  image: 'nginx:latest'
    - '8081:80'
    - './web:/usr/share/nginx/html'
    - 'api'

Create a Dockerfile inside web directory.

FROM nginx
COPY . /usr/share/nginx/html

Create the index.html file.

<!DOCTYPE html>
    <meta charset="utf-8" />
    <title>Dockerized Posts</title>
    <script src=""></script>
    <h1>Dockerized Posts</h1>
    <form id="form">
      <input type="text" placeholder="New post..." id="post-input" />
    <div id="posts"></div>
      $(document).ready(function () {
        $.get('http://localhost:8080/posts', function (posts) {
          $list = $('#posts');
          for (var i = 0; i < posts.length; i++) {
            $list.append('<p>' + posts[i].text + '</p>');
        $('#form').submit(function (event) {
          var text = $('#post-input').val();
            JSON.stringify({ text: text }),
            function () {

Now build it.

docker-compose up -d --build

Navigate to http://localhost:8081/ with your browser and try creating a few posts.


Here's how you can run Go tests. Inside api create sanity_test.go test file.

package main

import "testing"

func TestSanity(t *testing.T) {
  t.Log("Has sanity")

Then run go test -v inside api container.

docker-compose run api go test -v
=== RUN   TestSanity
--- PASS: TestSanity (0.00s)
        sanity_test.go:6: Has sanity
ok      app     0.004s

If you need a temporary database during testing, you could simply define another service.


These were just a couple of examples of why the world of containers is awesome. If you haven't already, give Docker a try. If not for production use, at least during development, because managing local installations of services is extremely unpleasant.

Get source code for the Docker Compose example on GitHub.



Get awesome articles delivered right to your doorstep

Protected by reCAPTCHA - Privacy - Terms


Building a Microservices Application in Go Following the CQRS Pattern

Dependency Injection in Go

Real-time Chat with GraphQL Subscriptions in Go