The software described in this documentation is either no longer supported or is in extended support.
Oracle recommends that you upgrade to a current supported release.

4.6 Communicating Between Docker Containers

You can use the --link option with docker run to make network connection information about a server container available to a client container. The client container uses a private networking interface to access the exposed port in the server container. Docker sets environment variables about the server container in the client container that describe the interface and the ports that are available.

The following example demonstrates how to link an oraclelinux:6-based client container with an HTTP server container based on the mymod/httpd:v2 image that you created in Section 4.4, “Creating a Docker Image from a Dockerfile”.

To create an HTTP server and client containers that are linked:

  1. Create an HTTP server container named http_server:

    [root@host ~]# docker run -d --name http_server mymod/httpd:v2 
    a47169154222329eed66762128755cd9fdd24d0f27ff8e0f678ef136bbc66d03
  2. Create a client container named client1 that runs the bash shell and is linked to the http_server container:

    [root@host httpd]# docker run --rm -t -i --name client1 --link http_server:server \
      oraclelinux:6 /bin/bash
    [root@client1 ~]#

    The argument http_server:server to the --link option aliases the name http_server as server. Docker converts the alias to upper case (SERVER) and uses this string when setting up the names of the environment variables on the client.

You can now view the environment variables in the client1 container. You can also use ping to detect the server container by name or IP address, and use curl to access the web server running on the server:

[root@client1 ~]# env
HOSTNAME=10815c22e5b4
TERM=xterm
SERVER_PORT=tcp://172.17.0.16:80
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
SERVER_PORT_80_TCP_PORT=80
SERVER_PORT_80_TCP_ADDR=172.17.0.16
SERVER_PORT_80_TCP=tcp://172.17.0.16:80
SERVER_PORT_80_TCP_PROTO=tcp
SHLVL=1
SERVER_NAME=/client1/server
HOME=/
_=/usr/bin/env
[root@client1 ~]# ping -c 1 server
PING server (172.17.0.16) 56(84) bytes of data.
64 bytes from server (172.17.0.16): icmp_seq=1 ttl=64 time=0.105 ms

--- server ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.105/0.105/0.105/0.000 ms
[root@client1 ~]# ping -c 1 172.17.0.16
PING 172.17.0.16 (172.17.0.16) 56(84) bytes of data.
64 bytes from 172.17.0.16: icmp_seq=1 ttl=64 time=0.171 ms

--- 172.17.0.16 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.171/0.171/0.171/0.000 ms
[root@client1 ~]# curl http://server
HTTP server running on guest
[root@client1 ~]# curl http://172.17.0.16
HTTP server running on guest

You can start multiple client container instances with different names, each of which can access port 80 on the server container. Docker assigns a different IP address to each client. As shown in the following example output, Docker creates an entry for the server in the /etc/hosts files on each client but it does not create entries for the names of the client containers themselves:

[root@client1 ~]# cat /etc/hosts
172.17.0.17  10815c22e5b4
127.0.0.1    localhost
::1          localhost ip6-localhost ip6-loopback
fe00::0      ip6-localnet
ff00::0      ip6-mcastprefix
ff02::1      ip6-allnodes
ff02::2      ip6-allrouters
172.17.0.16  server
[root@client1 ~]# ping -c 1 client2
ping: unknown host client2
[root@client1 ~]# ping -c 1 172.17.0.18
PING 172.17.0.18 (172.17.0.18) 56(84) bytes of data.
64 bytes from 172.17.0.18: icmp_seq=1 ttl=64 time=0.268 ms

--- 172.17.0.18 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.268/0.268/0.268/0.000 ms

By default, the clients are visible to each other on the private network only by their IP addresses.

The docker ps command shows the containers that are running:

[root@host ~]# docker ps
CONTAINER ID  IMAGE            COMMAND          CREATED  STATUS        PORTS  NAMES
449abeac3041  oraclelinux:6    /bin/bash        ...   Up 1 minutes         client2
10815c22e5b4  oraclelinux:6    /bin/bash        ...   Up 2 minutes         client1
a47169154222  mymod/httpd:v2   /usr/sbin/httpd  ...   Up 3 minutes     80/tcp  
                                         client1/server,client2/server,http_server

The NAMES column shows that http_server is linked to client1 and client2 as server. The PORTS column shows that Docker has not remapped TCP port 80 on http_server to another port on the host.