Docker Basics – Expose ports, port binding and docker link

This post illustrates three methods to link Docker containers.

Expose ports and port binding

Expose ports

This method is used for within the same network or the docker host. Containers on the same network can talk to each other over their exposed ports and you can expose the ports by one of the below methods.

– Put EXPOSE 80 (or any port you want) in your Dockerfile that’s going to tell Docker that your container’s service can be connected to on port 80.
– Expose a port using ‘–expose [port number]’ from the docker container by ‘run –expose [port number]’:

# docker run --expose=[port number] test

Port binding

This method is used for outside the same network. To allow communication via the defined ports to containers outside of the same network,you need to publish the ports by using -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports. You can do port porting via one of the below ways:

-Expose a port via Dockerfile by –expose and publish it with the -P flag. It will bind the exposed port to the Docker host on a random port.
-Expose a port via Dockerfile by –expose and publish it with the -p 21:21 flag, this will bind the expose port to Docker host on certain port 21 with guest 21.
– Bind the port by docker container run command:

# docker run -p [port number on docker host]:[port number on container]/tcp -p [port number on docker host]:[port number on container]/udp test

Example:

# docker run -p 80:80/tcp -p 500:500/udp test

Unix Domain Socket within a single host

To communicate between containers within a single host, you can use IPC mechanisms on Linux using the same socket.Start the two dockers from the docker host sharing a volume that should be created in the host machine:

# docker run -d -it --name dvc1 -v /var/tmp:/host oraclelinux:7 /bin/bash
# docker run -d -it --name dvc2 -v /var/tmp:/host oraclelinux:7 /bin/bash

After the above command, container dvc1 and dvc2 can use sockets as /var/tmp/SocketX to interconnection.

Use Docker link mechanism to set up a link between containers

Docker can create a tunnel between two containers by using environment variables to pass information from the parent container to the child container. To create a link, you use the –link flag:

# docker run -itd --name=[child container] --link [parent container] [child container image] /bin/bash

Example:
-Check parent container’s network:

# docker ps|grep dvc
7d5ad19de678 j_web:v1 "/bin/sh -c '/usr/bi…" 2 days ago Up 5 hours dvc1
# docker exec -it dvc1 /bin/bash
# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:04
inet addr:172.17.0.4 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

-Link the two new containers with the parent container:

# docker run -it --name dvc1_dup1 --rm --link dvc1 j_web:v1 /bin/bash

-Inspect the linked container:

# docker inspect -f "{{ .HostConfig.Links }}" dvc1_dup1
[/dvc1:/dvc1_dup1/dvc1]

-Check the new container’s host file, it has the parent container’s information:

# docker exec -it dvc1_dup1 /bin/bash
[root@b56d465976bc /]# cat /etc/hosts
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.4 dvc1 7d5ad19de678
172.17.0.5 b56d465976bc
[root@b56d465976bc /]#
Warning: The –link flag is a legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using –link. One feature that user-defined networks do not support that you can do with –link is sharing environment variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.
Related Post