Docker Networks & Links Cheat Sheet¶
Docker has a networks feature. Docker automatically creates 3 network interfaces when you install it (bridge, host none). A new container is launched into the bridge network by default. To enable communication between multiple containers, you can create a new network and launch containers in it. This enables containers to communicate to each other while being isolated from containers that are not connected to the network. Furthermore, it allows to map container names to their IP addresses. See working with networks for more details.
docker network createNAME Create a new network (default type: bridge).
docker network rmNAME Remove one or more networks by name or identifier. No containers can be connected to the network when deleting it.
docker network lsList networks
docker network inspectNAME Display detailed information on one or more networks.
docker network connectNETWORK CONTAINER Connect a container to a network
docker network disconnectNETWORK CONTAINER Disconnect a container from a network
You can specify a specific IP address for a container:
# create a new bridge network with your subnet and gateway for your ip block docker network create --subnet 203.0.113.0/24 --gateway 203.0.113.254 iptastic # run a nginx container with a specific ip in that block $ docker run --rm -it --net iptastic --ip 203.0.113.2 nginx # curl the ip from any other place (assuming this is a public ip block duh) $ curl 203.0.113.2
This has been deprecated to some extent by user-defined networks.
NOTE: If you want containers to ONLY communicate with each other through links, start the docker daemon with
-icc=false to disable inter process communication.
If you have a container with the name CONTAINER (specified by
docker run --name CONTAINER) and in the Dockerfile, it has an exposed port:
Then if we create another container called LINKED like so:
docker run -d --link CONTAINER:ALIAS --name LINKED user/wordpress
Then the exposed ports and aliases of CONTAINER will show up in LINKED with the following environment variables:
And you can connect to it that way.
To delete links, use
docker rm --link.
Generally, linking between docker services is a subset of "service discovery", a big problem if you're planning to use Docker at scale in production. Please read The Docker Ecosystem: Service Discovery and Distributed Configuration Stores for more info.
Thanks to @wsargent for creating this cheat sheet.