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


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.


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.


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!


Frequently Asked Questions
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.
Yes, you can connect a Docker container to two networks.
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.
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.
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
- docker network connect | Docker Docs
- docker network create | Docker Docs
- How to add containers to same network in Docker – Stack Overflow
- Docker Compose: Manage Multiple Containers with YAML – Itechguides.com
- Bridge network driver | Docker Docs
- Docker run Command Explained with Examples – Itechguides.com
- Docker ps Command Explained with Examples (itechguides.com)
- Docker Exec -it Command: What It Is and How to Use It (itechguides.com)
[ad_2]
Victor Ashiedu
Source link
