Connect with us

Gadgets

How to Add Docker Containers to a Network

[ad_1]

Do you want multiple Docker containers to be able to communicate? You need to add the containers to the same docker network.

This guide explains multiple ways to connect docker containers to a network. Specifically, I’ll explain two methods to link a container to a network.

But before I do that, let me start by explaining how to create a Docker network.

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.

How to Create a Docker Network

How to Create a Docker Network

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 NetworkHow 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 inspectDocker network inspect

Add Docker Containers to a Network

There are two common methods to link containers to a Docker network. The first method is by connecting an existing container to the network.

Secondly, while starting a new container. I have provided the steps to do both in the subsections below.

Method 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 NetworkConnect 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!

Method 2: Join a Container to a Network at Start-up

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-upJoin 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 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.

Other Useful Resources

  1. docker network connect | Docker Docs
  2. docker network create | Docker Docs
  3. How to add containers to same network in Docker – Stack Overflow
  4. Docker Compose: Manage Multiple Containers with YAML – Itechguides.com
  5. Bridge network driver | Docker Docs
  6. Docker run Command Explained with Examples – Itechguides.com
  7. Docker ps Command Explained with Examples (itechguides.com)
  8. Docker Exec -it Command: What It Is and How to Use It (itechguides.com)

[ad_2]

Victor Ashiedu

Source link