Changing Environment Setup in Docker Compose

Jan 18, 2019 min read

I use Docker for every aspect of my development and deployment workflows as I possibly can. For me it solves so many problems and has saved so much time I can praise it enough. The consistency across environments, operating systems is incredible.

For me I like to setup each environment just a little bit differently however. For example, when I’m developing a NodeJS application locally I might have an environment setup that looks like this:

This example is pretty simple. I have a container running my NodeJS API application, a second container for my database, MySQL or MongoDB, etc., and a third container running my user interface server, Vue.js. I use Docker Compose to manage all of this.

My docker-compose.yml might look something like this:

# docker-compose.dev.yml
version: "3"
services:
  server:
    build:
      context: .
      dockerfile: node-8.Dockerfile
    container_name: server
    restart: always
    ports:
      - "3000:3000"
    links:
      - mongo
    stdin_open: true
    volumes:
      - ./server:/api
    working_dir: /api
  database:
    container_name: mongo
    image: mongo
    ports:
      - "27017:27017"
  web:
    build:
      context: .
      dockerfile: vuejs.Dockerfile
    container_name: web-server
    ports:
      - "4000:4000"
    volumes:
      - ./client:/ui
    working_dir: /ui

There are a few reasons that I like a development setup like this:

Individual control over each aspect of the application

I can start / stop individual containers easily and independently through a separate terminal window. This also allows be to modify the underlying application environment quickly while developing. For example, adding or changing a Node environment variable.

I can view the individual applications / processes running in the container and monitor terminal output for things like errors or console.log() statements.

That works great for local development, I want fine grained control. But what about moving those same containers to different environments like DEV, QA, or PROD. The container landscape might look a lot different. I probably want applications running in containers to startup automatically. I might also have different configurations based on the environment, URLS, port numbers, etc.