Skip to content
Mike Robinson edited this page Feb 26, 2016 · 2 revisions

Tips & Tricks

Connecting containers (networking)

NOTE: Check out dream for a fancy, low maintenance option

For simple development environments, a neat way to handle this is to put your connected containers on the same network, and give them simple fixed container names.

First create your network (for example, we'll use the name myapps): docker-machine network create myapps

Then in each project's docker-compose.yml you must make the following changes:

  • Set the version to 2, which includes nesting your services under the services key
  • Add a unique container_name to your app service
  • Add the default and myapps networks to the app service networks key
  • Add the networks config to mark the network as external

Example using a simple railsy compose file:

version: '2'

services:
  db:
    image: postgres
  app:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000"
    links:
      - db
    networks:
        - default
        - myapps
    container_name: app_one

networks:
  myapps:
    external: true

Finally docker-compose up - your containers can now reference each other by their container_name.

Note: The container_name step is essential if your containers would be by default be named with underscores, as Ruby's URI lib doesn't like them

This can also all be made more fanciful with a DNS container - but that's a bit beyond a simple setup.

Rails complains "server already running"

During testing I encountered this often, yet does not seem to be a problem with docker itself. It seems to be due to server.pid not being cleaned up, so a simple fix is to remove this file as part of the app service command:

app:
  build: .
  command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
  volumes:
    - .:/app
  ports:
    - "3000"
  links:
    - db