Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.6 Container Networking

In this lesson you'll learn all about the Docker bridge network. I'll show you how to use the bridge network to connect containers to one another.

2.6 Container Networking

Hello everyone. This is Reggie Dawson. Welcome to the Docker for Deployment course for Tuts+. In this video, we're going to learn how our container networks are configured. When we create a container, it would not be very useful if we could not connect to it. In some of the containers we have set up so far, we have been using the p option to set our ports. Then we are connecting to the IP of our host machine to access our containers. We also have IP addresses that are assigned to each container. These IP addresses come from the Docker network install with the Docker machine. Docker machine provides three default networks. Additionally, it provides a couple different network drivers for our use, the bridge and overlay driver. The overlay driver is used when we have multiple hosts, and for most cases, the bridge network is all we need. The bridge network basically creates a network on the fly on the host machine. Whenever we create a container, it is added to the bridge network by default. We can list the networks we have by running docker network ls. This shows us our default networks. We will only concern ourselves with the bridge network. Make sure you have a container running, and use the docker network inspect bridge command. This will display the properties of our bridge network. Any container that we add will be listed here along with its IP address. One of the benefits of Docker is building applications from microservices. What this means is that we can connect the database to a front end to build a web app, for example. A lot of the time, we need an IP address to make this happen. We can also create our own network through the doctor network create command. When we create this network, we are creating it with the default bridge network driver. If we list our networks, we can verify our network has been created. Then if we inspect the network we added, no container should be connected. Let's add a container to our new network. First let's remove a container from our original network. This command removes our container from the bridge network. Then we need to add it to the network we created. Again, we're just using docker network connect and my-net is the name of the network we're connecting to and working-apache is the container that we're connecting. Of course, if we inspect our network, we will see that our container has been added to our new network. Now, understand that even though these networks are on the same host, they are two different networks and will not be able to talk to one another. We can also attach our container to multiple networks if we like. We didn't have to disconnect our container from the bridge network to connect it to our new network. Let's add it back to the bridge network. First let's look at the bridge network to verify there's only one container connected. Then we're going to connect our container back to the bridge network. Now if we inspect both networks, the bridge network has two hosts and our second network has one container, which is the same container that's on our bridge network. Now, beyond this type of networking, we can also use the link flag at runtime on our images to link our containers. This flag takes the name of the image we are connecting as well as an optional alias. We will discuss this further when we use this flag in a later video. That's everything you need to understand to use Docker networking. In the next video, we will use Docker to set up some web servers.

Back to the top