Why to Docker Compose your developer environment

综合编程 2017-09-25

This article originally appeared on my blog

The joys of a new job.

You get there on your first day and after the introductions, you are given your first task: to get the project up and running on your dev box.

The task comes with a link to the wiki, which has THE instructions:

  1. Download X
  2. Install Y
  3. Run “./build --special-flag”
  4. Copy Z to W
  5. Make sure foo but not bar
  6. ...

You know how it is going to end. And just in case you had any doubts, your new boss just confirms it:

“It’s probably a bit out of date, so can you please update it if you find any issues?”

Two days later, you emerge triumphant, proud of your achievement, and with a smile on your face: Your updated instructions are so accurate that the next person joining will be forever thankful.

Or maybe not

The instructions on the wiki seem like a good enough solution, given that each member of the team needs to pass the ordeal just once ... except when:

  1. You get a new and shiny dev box.
  2. Your dev box dies.
  3. You move to another project.
  4. Six month latter, you go back to the original project. Nothing works anymore.
  5. A new feature requires Zookeeper, and everybody to configure a local Zookeeper cluster!
  6. Your operating system is upgraded and “insert-your-fav-database-here” doesn't want to start anymore. Welcome to DLL hell.
  7. That old project that your team still maintains needs Maven 2.1.1 but the current project requires > 3.3, and requires Postgres 7.1 but the new one requires at least 9.5 with PostPic and OpenFTS extensions.
  8. Polyglot Microservices company! Install maven, gradle, npm, yarn, node, make, go, ruby, rake, lein, sbt, rebar, cassandra, mongodb, redis, postgresql, couchbase…

But sometimes, some component, for some unknown reason, somehow stops working, and it won't ever start again.

Enter Docker Compose

Docker Compose is a tool which allows one to define a multi-container system using one file, and to run them in Docker, creating a private network to isolate the system.

A Docker Compose file that will start a Postgres and a Redis is as simple as:

version: "3"
    image: postgres:9.5
      - ./db-provision:/docker-entrypoint-initdb.d/
      - 5432:5432
    image: redis:3.2.9

You can configure the container for your project and Docker Compose will be able to create a new environment, mapping some of the ports and files between the internal containers and your dev box.

Something like:

Using Docker Compose to set up and run our local development environment gives us:

One command to set up and start the system docker-compose up
One command to upgrade or add new components docker-compose up --build
One command to stop the system docker-compose stop
One command to clean up any trace of the system docker-compose down --remove-orphans --rmi all
A precise and repeatable process The Docker Compose file will be executed by a machine, so there is no room for imprecision.
Just make sure that you use an immutable version of the image.
A process that never gets out of dateAs the team will use the Docker Compose everyday for their development, and any change in the system must be done within the Docker files, there is no opportunity for the instructions to get stale.
A process that can be version controlledThe Docker files are plain text, they will be collocated with the rest of the project source code, they will be peer reviewed and changes can be rolled back, diffed, ...
A process that allows for experimentationAs it is easy to destroy and recreate systems, and as the Docker files can be rolled back, there is no risk on trying new versions of components or fiddling around with the configuration.
A way of running more than version of the same project or more than one project at the same time Unfortunately, this is probably requires some manual fiddling to remap some of the ports exposed in the Docker Compose file, but at least all the ports are in just one file.
After that is a simple: docker-compose -p my-project-one up -d; docker-compose -p my-project-again up

A production-like environment or not?

When talking about Docker, one of the benefits usually mentioned is that it allows one to run a production-like environment anywhere.

Indeed this is a big benefit. Your CI server should run its tests against a production-like version, and you should be able to run such a version locally, but for the day to day development experience, you want a setup that allows for a fast feedback cycle.

For example, you don’t want to minimize your JavaScript files for each and every change that you make and you probably want some sort of auto refresh.

Fast feedback is just more important.

This development environment should include any build tools that your project requires, so your setup instructions should not have any “install maven/npm” step. Those tools should come within a Docker container. This way, everybody in the team will be using the same tool version in the same operative system version.


Even if you don’t use Docker in production, it is well worth using it just to make setting up or upgrading a dev environment an uneventful process.

This process should be as close to “docker-compose up” as possible. Nothing more.

Don’t treat your dev box as some big global mutable variable. Use Docker Compose to treat it as a nice immutable one.

You have here a detailed example of how to Docker Compose a local developer environment.

More about efficiency:

  1. Disable notifications
  2. Master your IDE
  3. Write programs for yourself
  4. Why to Docker Compose your local environment (you are here)
The Practical Developer

责编内容by:The Practical Developer (源链)。感谢您的支持!


GDD 2017全程回顾–轮椅上的程序员们都站了起来... 前言: GDD 2017于12月14日下午圆满结束,本次大会鄙人受益匪浅,Google的体量,对技术的思考的深度,对未来的探索广度,无不让我惊叹。技术上总结起来就是:前端技术发展势头强劲,未来真的属...
We Programmers The Good. There is a little red sports car heading out towards the asteroid belt and, we pro...
黄哥Python:提醒要转行当程序员的朋友,学习要分先后主次... 很多要转行当程序员的朋友,被所谓的项目所迷惑,认为自己照着葫芦画瓢学了几个项目开发,认为自己就能转行成功,下面听黄哥细细道来。 1、黄哥提醒:短时间内你开发项目的能力不可能达到有多年...
How Metavine Is Enabling Business Users to Develop... Thanks to Steve Terlizzi, V.P. Strategy and Markets at Metavine for sharing his insights on ...
Developers Show off Impressive Surface and Image D... Apple announced ARKit with iOS 11 last year. The augmented reality platform was created for the dev...