How to use docker network disconnect command to disconnect a container from a network

DockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage container network connections using the docker network disconnect command. We will begin by creating a custom network and running a container connected to it.

Following that, you will see how to connect an existing container to an additional network. Finally, you will learn how to disconnect the container from a specific network and verify the disconnection. This hands-on exercise will provide practical experience in controlling container network connectivity.

Create a network and a container

In this step, we will learn how to create a custom Docker network and run a container connected to it. By default, Docker containers are connected to the bridge network, but creating custom networks allows for better isolation and communication control between containers.

First, let's create a new bridge network named my-network.

docker network create my-network

You should see the ID of the newly created network printed in the output.

Next, we will pull the nginx image from Docker Hub. This image will be used to create our container.

docker pull nginx

This command downloads the nginx image to your local machine.

Now, let's run an Nginx container and connect it to the my-network we just created. We will name the container my-nginx.

docker run -d --name my-nginx --network my-network nginx

Let's break down this command:

  • docker run: This command is used to run a container.
  • -d: This flag runs the container in detached mode, meaning it runs in the background.
  • --name my-nginx: This assigns the name my-nginx to the container.
  • --network my-network: This connects the container to the my-network we created.
  • nginx: This is the image to use for creating the container.

After running the command, Docker will start the my-nginx container and connect it to my-network.

Connect the container to the network

In the previous step, we created a network and ran a container connected to it. In this step, we will demonstrate how to connect an already running container to an additional network. This is useful if you need a container to communicate with other containers on a different network.

First, let's create another new bridge network named another-network.

docker network create another-network

You should see the ID of this new network printed in the output.

Now, we will connect our existing my-nginx container to this new another-network.

docker network connect another-network my-nginx

This command connects the specified container (my-nginx) to the specified network (another-network). The container will now have network interfaces on both my-network and another-network.

To verify that the container is connected to both networks, we can inspect the container's network settings.

docker inspect my-nginx

Look for the "Networks" section in the output. You should see entries for both my-network and another-network, indicating that the container is successfully connected to both.

Disconnect the container from the network

In the previous step, we connected our my-nginx container to an additional network, another-network. In this step, we will learn how to disconnect a container from a specific network. This is useful when you no longer need a container to be part of a particular network.

To disconnect the my-nginx container from another-network, we use the docker network disconnect command.

docker network disconnect another-network my-nginx

This command takes the network name (another-network) and the container name (my-nginx) as arguments.

After executing this command, the container will be removed from the specified network. It will no longer be able to communicate with other containers on another-network unless they are also on a shared network.

To confirm that the container has been disconnected, we can again inspect the container's network settings.

docker inspect my-nginx

Look at the "Networks" section in the output. You should now only see the my-network listed, and another-network should be absent. This confirms that the container has been successfully disconnected from another-network.

Verify the container is disconnected

In the previous step, we disconnected the my-nginx container from the another-network. In this step, we will perform a final verification to ensure that the container is indeed no longer connected to another-network.

We can use the docker inspect command again to examine the network configuration of the my-nginx container.

docker inspect my-nginx

Review the output of this command. Specifically, look for the "Networks" section. You should only see the my-network listed under "Networks". The absence of another-network in this list confirms that the container has been successfully disconnected from it.

This step serves as a confirmation of the previous action and reinforces how to check the network status of a Docker container.

Summary

In this lab, we learned how to manage Docker container network connections. We began by creating a custom bridge network and launching a container directly connected to it using the docker run --network command. This demonstrated the initial connection of a container to a specific network during its creation.

Subsequently, we explored how to connect an already running container to an additional network using the docker network connect command. Finally, we learned how to disconnect a container from a network using the docker network disconnect command and verified the disconnection, illustrating the flexibility of managing container network memberships after creation.