Lastly, one of the up and coming features of Docker that we will be taking a look at will be that of the Docker networking. Now at its current form, this is a solution that has not yet been implemented, but is a feature set that will be coming soon. So, it's good to get ahead of the curve on this one and learn it so that you are ready to implement it or architect your future environments around it.
Since this feature is not part of the current Docker release, you need to install the experimental release to get this completed. To install Docker experimental releases, simply use the curl
command that you have seen previously. Now this will only work on Linux and Mac currently. In future, experimental builds might be installed on Windows systems. So to install, use the following command:
$ curl -sSL https://experimental.docker.com/ | sh
On Mac, run:
$ curl -L https://experimental.docker.com/builds/Darwin/x86_64/docker-latest > /usr/local/bin/docker $ chmod +x /usr/local/bin/docker
Now you will get a warning message if you already have Docker installed:
Warning: the "docker" command appears to already exist on this system. If you already have Docker installed, this script can cause trouble, which is why we're displaying this warning and provide the opportunity to cancel the installation. If you installed the current Docker package using this script and are using it again to update Docker, you can safely ignore this message. You may press Ctrl+C now to abort this script. sleep 20
You want to make sure you are installing experimental builds to a machine that is not a production-related one. For example, you probably don't want to install an experimental release to your laptop if you are using it to develop and test Docker-related items on. Best practice would be to install it on a virtual machine that you can throw away if it gets broken.
After running the curl
command, you will be able to see the networking option from the list of Docker commands now:
$ docker Usage: docker [OPTIONS] COMMAND [arg...] docker daemon [ --help | ... ] docker [ --help | -v | --version ] A self-sufficient runtime for containers. Options: --config=~/.docker Location of client config files -D, --debug=false Enable debug mode -H, --host=[] Daemon socket(s) to connect to -h, --help=false Print usage -l, --log-level=info Set the logging level --no-legacy-registry=false Do not contact legacy registries --tls=false Use TLS; implied by --tlsverify --tlscacert=~/.docker/ca.pem Trust certs signed only by this CA --tlscert=~/.docker/cert.pem Path to TLS certificate file --tlskey=~/.docker/key.pem Path to TLS key file --tlsverify=false Use TLS and verify the remote -v, --version=false Print version information and quit Commands: attach Attach to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on a container or image kill Kill a running container load Load an image from a tar archive or STDIN login Register or log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container network Network management pause Pause all processes within a container port List port mappings or a specific mapping for the CONTAINER ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart a container rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save an image(s) to a tar archive search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop a running container tag Tag an image into a repository top Display the running processes of a container unpause Unpause all processes within a container version Show the Docker version information volume Manage Docker volumes wait Block until a container stops, then print its exit code Run 'docker COMMAND --help' for more information on a command.
In the preceding command output, I have highlighted the section that we will be focusing on—the network
subcommand in Docker. There is also another command you may want to take a look at, and that is the volume
subcommand, but we will be focusing on the network
subcommand.
Let's create ourselves a network that our Docker containers can use to communicate on. From the output of the docker network
command, we can see our options:
$ docker network docker: "network" requires a minimum of 1 argument. See 'docker network --help'. Usage: docker network [OPTIONS] COMMAND [OPTIONS] [arg...] Commands: create Create a network rm Remove a network ls List all networks info Display information of a network Run 'docker network COMMAND --help' for more information on a command.
Doing a docker ls
will give us a view of what our current network setup is:
$ docker network ls NETWORK ID NAME TYPE 02f3d3834733 none null b22ff5151bcb host host f4b7c38b83b1 bridge bridge
Now let's get to creating ourselves a network. Using the network
subcommand as well as the create
option, we can create ourselves a network:
$ docker network create <name> $ docker network create docker-net 21625dd96ac08e1713621d951cfa140cebee96c9fae9f8ff44748f86a4c731d7 $ docker network ls NETWORK ID NAME TYPE 02f3d3834733 none null b22ff5151bcb host host f4b7c38b83b1 bridge bridge 21625dd96ac0 docker-net bridge
Now that we have our network, how do we tell our containers about it? That comes with a --publish-service=
switch when you use your docker run
command:
$ docker run -it --publish-service=<name>.<network_name> ubuntu:latest /bin/bash $ docker run -it --publish-service=web.docker-net ubuntu:latest /bin/bash
We can also create networks and provide drivers for those networks so that they can span across multiple hosts. By default, there is a driver named overlay that will allow you to do this. Now this is the first of many drivers that will be coming on board, either when this network feature is baked into Docker or at a later time, for sure. When you create the network is when you will specify the overlay driver. However, there is one thing that this driver does need. It will need access for not only itself, but also the other Docker hosts that you want to network together:
$ docker network create -d overlay docker-overlay