Docker 05: Networking in Docker – Connecting Containers and Exposing Ports

Posted by:Bhanu Chaddha Posted on:February 9, 2023 Comments:0

Docker is a powerful tool for containerizing applications, but its networking capabilities are what truly make it stand out. In this blog post, we will dive into the world of Docker networking and explore how to connect containers and expose ports for communication.

The basics of Docker networking

Docker uses a virtual network to connect containers, and each container is assigned an IP address. By default, Docker creates a virtual network for each container, but you can also connect multiple containers to a shared network. This allows containers to communicate with each other and the host system.

Connecting containers to a shared network

You can connect containers to a shared network using the --network flag when launching a container. To create a shared network, you can use the docker network create command. For example, the following command creates a shared network called my_network:

docker network create my_network

Next, you can launch a container and connect it to the shared network using the --network flag:

docker run --name my_container --network my_network -d <image_name>

Connecting multiple services using network

Docker networks provide a way for you to connect multiple services running in separate containers. This allows you to build applications composed of multiple microservices that can communicate with each other over the network.

For example, suppose you have a web service and a database service, and you want to connect them using a shared network. First, you would create a shared network as described above:

docker network create my_network

Next, you would launch the web service and connect it to the shared network:

docker run --name web_service --network my_network -d web_image

Finally, you would launch the database service and connect it to the same network:

docker run --name db_service --network my_network -d db_image

Now, the web service and database service can communicate with each other using their respective IP addresses.

Exposing ports

To allow communication with the host system or other containers, you need to expose ports from your containers. You can do this using the -p or --publish flag when launching a container. The -p flag takes two arguments: the host port and the container port.

For example, the following command launches a container and maps port 80 in the container to port 8080 on the host system:

docker run --name my_container -p 8080:80 -d <image_name>

Advanced networking techniques

Docker networking is a vast and complex topic, and this blog post only scratches the surface. There are many other advanced techniques that you can use, such as linking containers, creating overlay networks, and using network plugins.

To learn more about Docker networking, we recommend checking out the official Docker documentation here.

In conclusion, Docker networking is a powerful and versatile tool for connecting containers and exposing ports. Whether you are just starting out with Docker or you are an experienced user, there is always more to learn about this exciting technology. So go ahead, experiment with Docker networking, and see what you can build!

Category