How to Add Docker Containers to a Network

Photo of author

By Victor Ashiedu

Published

Do you want multiple Docker containers to be able to communicate? You need to add the containers to the same docker network and this guide explains several ways to do that.

If you start and manage containers with Docker compose and YAML file, Docker compose creates the network and automatically links all the containers to the network. However, you can use existing networks in Compose as well.

Step 1: Create a Docker Network (Optional)

Creating a new Docker network is pretty simple. All you require is a single command – docker network create <network-name>.

In the example command below, I will create a bridge network called “mysql-phpadmin-nw.”

docker network create mysql-phpadmin-nw

The command creates the network and displays it ID. To confirm that the network has been created, I’ll run the “docker network ls” command.

How to Create a Docker Network

Finally, to confirm that no container is currently connected to this network, I’ll execute the command below:

Docker network inspect mysql-phpadmin-nw

The command returns a bunch of information about the network. Scrolling down to the “Containers” section, you’ll notice that it is currently empty.

This confirms that no container is connected to this network.

Docker network inspect

Step 2 (Option 1): Attach an Existing Container to a Network

You can join a running container to a network with a very simple Docker command. I have a running MySQL container that is not linked to the “mysql-phpadmin-nw” Docker network I created in the last section.

To bind the MySQL container to mysql-phpadmin-nw, I will execute the command below:

Docker network connect mysql-phpadmin-nw mysql

“mysql” is the name of the MySQL container. You can get this information by running the “docker ps” command.

Connect an Existing Container to a Network

Before proceeding, let’s check that the MySQL container is now linked to the mysql-phpadmin-nw network.

This time, the “Docker network inspect mysql-phpadmin-nw” command confirms that a container called “mysql” is connected to this network!

Step 2 (Option 2): Join a Container to a Network at Creation

While starting a container with the “Docker run” command, you can attach a network to the container. To demonstrate this, I’ll start a phpmyadmin container and bind it to the network I created earlier.

A phpmyadmin container needs to connect to a running MySQL or MariaDB server in Docker. As I mentioned earlier, I have an existing MySQL container that is I connected to the mysql-phpadmin-nw Docker network.

I will now start a new phpmyadmin container and join it to the mysql-phpadmin-nw Docker network. Here is the command that performs this magic!

docker run -p 8081:80 \
--network mysql-phpadmin-nw \
--name phpmyadmin \
--link mysql:db \
-d phpmyadmin

The “–network” option is used to bind the new container to an existing network. Equally important to a phpmyadmin specifically is the “–link” option which is required to link it to the MySQL databases.

Let’s now execute the command. Because I used the “-d” option, the container starts in detached mode.

This means that it starts on the background.

Once again, we’ll confirm that this newly started container is also linked to the network. Running the “Docker network inspect” command reveals that both containers are indeed connected to our Docker network!

Join a Container to a Network at Start-up

Frequently Asked Questions

1. How do I move a Docker Container to another network?

Run the “docker network disconnect” command to detach the container from the existing network. After that, execute the “docker network connect” command to link the container to the new network.

2. Can a Docker Container be on two networks?

Yes, you can connect a Docker container to two networks.

3. How to create a Docker network Bridge?

You can create a Docker network bridge by running the “Docker network create” command and entering the name of the bridged network you want to create at the end of the command.

For example to create a Docker network bridge called “test-bridge-network,” run this command: Docker network create test-bridge-network.

4. Do Docker Containers have their own IP?

Yes, each Docker container has its IP address. You can view the IP address of a container by running the “docker container inspect” command.

The IP address of the container will be displayed in the “Networks” section of the results.

5. What is the default network in Docker?

The default Docker network driver is bridge.

Thank you for reading. Kindly share your thoughts about this topic or ask a question using the comments form at the bottom of this page.

Alternatively, you can respond to the “Was this page helpful?” question below.

About the Author

Photo of author

Victor Ashiedu

Victor is the founder of InfoPress Media, publishers of Ilifeguides and Itechguides. With 20+ years of experience in IT infrastructure, his expertise spans Windows, Linux, and DevOps. Explore his contributions on Itechguides.com for insightful how-to guides and product reviews.

Related Articles

Get in Touch

We're committed to writing accurate content that informs and educates. To learn more, read our Content Writing Policy, Content Review Policy, Anti-plagiarism Policy, and About Us.

However, if this content does not meet your expectations, kindly reach out to us through one of the following means:

  1. Respond to "Was this page helpful?" above
  2. Leave a comment with the "Leave a Comment" form below
  3. Email us at [email protected] or via the Contact Us page.

Leave a comment

Send this to a friend