DOCKER Introduction

There’s a lot of tutorials showing how to link containers using the docker run –link option, but the link flag is a depricated feature of Docker and may be eventually removed.
I will show you how to link containers via docker network providing a template of Dockerfile for your Golang application, but the focus here is really on the process.

It’s there already…

Assuming you haven’t created any networks yet, executing docker network ls should list the default Docker networks:

These three are created automatically.

  • the bridge is used by default when connecting containers.
  • the host – the network configuration inside the container will be identical to the host (for external access their port will be mapped to a host port)
  • the none – adding your containers to this network will isolate them (no network interfaces)

The bridge

Let’s focus on the default network (bridge) which is represented by docker0 network interface:

You can also list of Linux bridges with brctl:

Running this command shows details about the bridge network:

Pay attention to “Containers”: {}. It’s empty.
The docker run command automatically adds new containers to this network:

This time the list of Containers is not empty:

A bridge network is useful in cases where you want to run a relatively small network on a single host.

Define your own network

The basics are covered, so the next step would be to create our own networks where we can define most of the params within the structure we saw when were running ‘docker network inspect’.

I suggest to run docker network to see available options:

Creating a new one (bridge by default) is straight forward:

but you probably won’t use create new ones that way. The docker daemon automatically chooses and assigns a subnet for that network, so it could overlap with another subnet in your infrastructure that is not managed by docker. That’s why it’s recommended the –subnet option while creating a network:

  • -d lets you choose the type of driver
  • – -subnet specifies a subnetwork
  • – -attachable enables manual container attachment (set to false by default). It means we can use docker run to create a container within the scope of this new network.

Add MongoDB to the network

Great! we have a network, let’s make a use of that:

docker logs mongo should show logs produced by the mongo container. Between many lines you should find there something like:

or jump to the mongo’s (mongoDB) console directly:

If you try to remove your network at this point, you’ll get an error:

..but we don’t want to do that 🙂

Dockerise your Golang application

If you want to connect to the mongo container from your applicatiom, you can use mongo in any reference to the DB host here (for instance: mongodb://mongo:27017). The Docker embedded DNS server enables name resolution for containers connected to a given network.

If you haven’t dockerised your Golang code before, you can use this template of Dockerfile replacing your_github_user/your_code/source (and all other here) with your own values:

Save your Dockerfile and build an image from it:

Add your Golang application to the network

Assuming it’s built succesfully. Run it with additional params that will place your container in your network:

Now inspect the network to check if both mongo and your_golang_application containers belong to the same network:

Testing the connection

Jump to the shell:

and ping mongo:

or simply run your app and check if it accesses the database.


The purpose of article was to get you familiar with basics of networking docker containers. I hope to see less places where the depricated flag “–link” is used as things seem to change quickly in the Docker’s world. I think it’s important to mention one feature that user defined networks do not support (and you can do with –link) is sharing environmental variables between containers but you can use other mechanisms such as volumes to do it in a more controlled way.

Having foundations of networking containers is a good start to look at more advanced scenarios.


Add a Comment

Your email address will not be published. Required fields are marked *