How to use the new “custom network” feature with Docker compose

Mathieu Fosse
The Swell
Published in
2 min readJan 27, 2016
Grand Central (https://flic.kr/p/bG7ckp)

You might have heard about the networking feature introduced in Docker 1.9.

Docker now comes with 3 default networks :

None

It creates a container-specific network stack. The only interface available in your container in this network is the loopback device.

Host

It allows your containers to communicate with your host’s interfaces.

Bridge

The bridge is the default network used by Docker. When you run this command, you’re actually running a container in the bridge network :

$ docker run -ti --rm busybox sh

It allows your containers to communicate with each other. The container is actually attached to the docker0 interface bridge.

The drawback is that you must know the IP of all your containers or use the soon-to-be-deprecated link option to link your containers.

Custom network

Here we are! You can now create a network where containers can communicate to each other.

Let’s create a dev network :

$ docker network create dev

You can now run containers in this network by using the net option :

$ docker run -d --name app --net=dev busybox sleep 60

We can now try access this container from a new container within the same network :

$ docker run -ti --rm --net=dev busybox ping -c 4 app

Docker uses the container’s name or name + network name for the name resolution.

So you can access the container app with the following names :

  • app
  • app.dev

Docker compose

Let say your docker-compose.yml file looks like this :

redis:
image: redis
app:
image: busybox
command: ping redis
links:
- redis

The app service is linked to the redis service.

Now the same file using a custom network :

version: 2
services:
redis:
image: redis
app:
image: busybox
command: ping redis

Running the docker-compose up command will create a custom network “current-directory-name_defaults”. And every services will be launched on this network.

Note that you have to install docker-compose 1.6.0-rc1 or greater to use the networking feature.

If you like this post, Follow The Swell!

--

--

Mathieu Fosse
The Swell

Co-founder of @useantenna. Former CTO and Co-founder at @tigerlily • @sleekapp. I love writing code, learning things and taking photos.