How To Network Containers ContainerHyperNetwork

Container Networking on cloud

You just previously succeeded in building and running a simple web application in the last blog of this series, but that’s not enough for real web service on the Internet. So, in this section we will teach you how to network your containers on to set up your microservices.

Name your container

You’ve already seen that each container you create has an automatically generated name. Actually, you can also name containers yourself. This naming provides two useful functions:

  • That makes it easier for you to remember them, for example naming a container running a web application web.

  • Naming of containers makes it easier for referencing of containers. There are several commands (like --link) that support this and you’ll use one in an exercise later.

You name your container by using the --name flag, for example launch a new container called web:

$ hyper run -d --name web training/webapp python

Use the hyper ps command to check the name:

$ hyper ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES               PUBLIC IP
d54e6f546795        training/webapp     "python"     11 seconds ago      Up 5 seconds>5000/tcp   web   

Container names must be unique. That means you can only call one container web. If you want to re-use a container name you must delete the old container.

Note that container name is not its hostname, container uses container ID as default hostname, and you can also configure your own by using hyper run --hostname:

-h, --hostname                  Container host name

Manage the public network of your container

You may have noticed that containers in have their own private IP as long as they are created. cloud makes the networking transparent for the end user, by using mature SDN component to build VLAN based tenants. also provides a public network solution, named floating IP, for you to connect containers from outside world., in which the basic usage of hyper fip allocate was showcased in the “Run web application” blog.

$ hyper fip --help

Usage:  hyper fip [OPTIONS] COMMAND [OPTIONS]

  allocate             	Allocate a or some IPs
  attach    			Attach floating IP to container
  detach	            Detach floating IP from conainer
  ls                    List all floating IPs
  release               Release a floating IP

In, floating IP is a top level resource, that means you can manage it independently from the containers and other resources.

Let’s create and list them:

$ hyper fip allocate 2
$ hyper fip ls
Floating IP         Container      
  • 2 is the number of floating IPs you want to create, we created two IP addresses in this example

Then you can attach the IP address with a container.

$ hyper fip attach web

Done! Now you can reach you app through

$ hyper fip ls
Floating IP         Container     d54e6f5467954e510026d7d2a0c0d602c0de1f0fd4443c6d7c3adea2bd064120

You can disconnect a container by detaching the floating IP. To do this, you just need to supply the container name or container id.

$ hyper fip detach web

As you can disconnect a container from a public network, you can also delete the floating IP to free this network resource.

$ hyper fip release

Link containers in cloud

The Docker style link in allow containers to discover each other and securely transfer information about one container to another container. When containers are linked, information about a source container can be sent to a recipient container. This allows the recipient to see selected data describing aspects of the source container.

To establish links, relies on the names of your containers. We have emphasized the importance of naming in previous article, and we’ll soon see the reason.

To create a link, we use the --link flag. First, let’s create a new container, this time one containing a database.

$ hyper run -d --name db training/postgres

This creates a new container called db from the training/postgres image, which contains a PostgreSQL database.

Now, you need to delete the web container you created previously, so you can replace it with a linked one:

$ hyper rm -f web

Now, create a new web container and link it with your db container.

$ hyper run -d --name web --link db:db training/webapp python

This will link the new web container with the db container you created earlier. The --link flag syntax is:

--link <name or id>:alias

Where name is the name of the container we’re linking to and alias is an alias for the link name. You’ll see how that alias gets used shortly. The --link flag can also use the syntax:

--link <name or id>

In which case the alias will match the name. You could have written the previous example as:

$ hyper run -d --name web --link db training/webapp python

Next, inspect your linked containers with hyper inspect:

$ hyper inspect -f "{{ .HostConfig.Links }}" web

You can see that the web container is now linked to the db container db. Which allows it to access information about the db container.

Then how to consume the link? exposes connectivity information for the source container to the recipient container in two ways:

  • Environment variables,
  • Names in the /etc/hosts file.

Environment variables

Containers in creates several environment variables when you link containers. These environment variables in the target container (web container) are created based on the --link parameters. It will also expose all environment variables originating from the source container (db container) including:

  • the ENV commands in the source container’s Dockerfile
  • the -e or --env options on the hyper run command when the source container is started

These environment variables enable programmatic discovery from within the target container of information related to the source container.

WARNING: Don’t abuse environments! All environment variables originating from within a container are made available to any container that links to it. This could have serious security implications if sensitive data is stored in them. sets an <alias>_NAME environment variable for each target container listed in the --link parameter. This is a Docker compatible environment system. For example, if a new container called web is linked to a database container called db via --link db:webdb, then creates a WEBDB_NAME=webdb variable in the web container. also defines a set of environment variables for each port exposed by the source container. Each variable has a unique prefix in the form:


The components in this prefix are:

  • the alias <name> specified in the --link parameter (for example, webdb)
  • the <port> number exposed
  • a <protocol> which is either TCP or UDP uses this prefix format to define three distinct environment variables:

  • The prefix_ADDR variable contains the IP Address from the URL, for example WEBDB_PORT_5432_TCP_ADDR=
  • The prefix_PORT variable contains just the port number from the URL for example WEBDB_PORT_5432_TCP_PORT=5432.
  • The prefix_PROTO variable contains just the protocol from the URL for example WEBDB_PORT_5432_TCP_PROTO=tcp.

If the container exposes multiple ports, an environment variable set is defined for each one. For example, if a container exposes 4 ports that creates 12 environment variables, 3 for each port.

Additionally, creates an environment variable called <alias>_PORT. This variable contains the URL of the source container’s first exposed port. The ‘first’ port is defined as the exposed port with the lowest number.

For example, consider the WEBDB_PORT=tcp:// variable. If that port is used for both tcp and udp, then the tcp one is specified.

Finally, as we mentioned before, also exposes each originated environment variable from the source container as an environment variable in the target. For each variable Hyper creates an <alias>_ENV_<name> variable in the target container. The variable’s value is set to the value used when it started the source container.

Back to our database example, you can run the env command to list the specified container’s environment variables.

$ hyper run --rm --name web2 --link db:db training/webapp env

Each variable coming from the source container is prefixed with DB_, which is populated from the alias you specified above. You can use these environment variables to configure your applications to connect to the database on the db container.

NOTE: --link works everywhere in cloud. Unlike Docker legacy --link, software defined network in guarantee the container connectivity within the same tenant. Users should never need to care things like cross-host or customized network.

However, IP addresses stored in the environment variables are not automatically updated if the source container is restarted. And these environment variables are only set for the first process in the container. Some daemons, such as sshd, will scrub them when spawning shells for connection.

We recommend using the host entries in /etc/hosts to resolve the IP address of linked containers in production.

##Updating the /etc/hosts file In addition to the environment variables, adds a host entry for the source container to the /etc/hosts file. Here’s an entry for the web container:

$ hyper run -t -i --rm --link db:webdb training/webapp /bin/bash
root@07c4b5c0305b:/opt/webapp# cat /etc/hosts localhost
...  webdb

You can see an entry uses the link alias webdb to reference the IP address of the db container. You can ping that host now via any of these entries:

root@07c4b5c0305b:/opt/webapp# ping webdb
PING webdb ( 56(84) bytes of data.
64 bytes from webdb ( icmp_seq=1 ttl=64 time=2.64 ms
64 bytes from webdb ( icmp_seq=2 ttl=64 time=1.10 ms
64 bytes from webdb ( icmp_seq=3 ttl=64 time=1.03 ms

Here, you used the ping command to ping the db container using its host entry, which resolves to You can use this host entry to configure an application to make use of your db container.

Note: You can link multiple recipient containers to a single source. For example, you could have multiple (differently named) web containers attached to your db container.

When you restart the source container (db container), will make sure it’s IP address will not change, allowing linked communication to continue.

Best Practice

As you can see above, cloud networking is simple but useful, it avoids boring users and try to free their creativity to compose micro-services as they wish. For better usage of this networking model, some best practices are recommended here.

  • Always naming your containers. --link uses names to identify related containers, and renaming containers afterward is costly.
  • Do not abuse floating IP. For security reason, and for resource saving reason.
  • Expose service ports in your Dockerfile. This is not required by, but this will bring you more automation, since only exposed ports will be added to target containers’ ENV.

A nice example to show these best practice is dockercloud/haproxy image from Docker Inc.

$ hyper run -d --name web-1 hyperhq/webapp:host python
$ hyper run -d --name web-2 hyperhq/webapp:host python
$ hyper run -d --name lb --link web-1 --link web-2 dockercloud/haproxy
$ FIP=$(hyper fip allocate 1)
$ hyper fip attach $FIP lb
$ curl $FIP:80
> Hello my host name is: de380811142a
$ curl $FIP:80
> Hello my host name is: 32d28908d30a

Exposed ports from web-1 and web-2 are added into the HAproxy container as ENV automatically. And these ENVs are tagged by container names as we mentioned in this article. So for HAproxy, consuming WEB-1_PORT and WEB-2_PORT is enough.