Loading...

How to use docker network

Jonas Scholz
3 min read 01. Dez. 2024

When you're working with Docker, managing networks is something you'll often need to do. Docker networks help your containers communicate with each other and the outside world. Let's walk through how you can manage these networks using Docker's command-line interface.

First off, the basic command to start with is docker network. This is your gateway to doing all sorts of network-related tasks. Let's break down what you can do with it.

Creating a Network

Imagine you're setting up a new project, and you need a fresh network for your containers to use. You can create a new network with the docker network create command. Here's how you might do it:

docker network create my_new_network

This command creates a network named my_new_network. Now, your containers can use this network to talk to each other securely.

Connecting a Container to a Network

Once you have your network, you might want to connect a container to it. This is done with the docker network connect command. Say you have a container named my_container and you want to connect it to my_new_network:

docker network connect my_new_network my_container

This command hooks up my_container to my_new_network, allowing it to communicate with other containers on the same network.

Disconnecting a Container from a Network

If you need to disconnect a container from a network, you use the docker network disconnect command. For instance, to remove my_container from my_new_network, you would run:

docker network disconnect my_new_network my_container

This command will cleanly disconnect my_container from my_new_network.

Inspecting a Network

Sometimes, you'll want to take a closer look at a network to see what's connected to it and what its settings are. You can do this with the docker network inspect command. For example, to inspect my_new_network, you would run:

docker network inspect my_new_network

This command gives you a detailed output about the network, including its configuration and the containers attached to it.

Listing All Networks

To see all the networks you have set up, use the docker network ls command. It's like taking inventory of your networks:

docker network ls

This command lists all your networks, showing their names, IDs, and types.

Removing a Network

When a network is no longer needed, you can remove it with the docker network rm command. To remove my_new_network, you would run:

docker network rm my_new_network

This command deletes the network, but be careful – you can't remove a network if containers are still connected to it.

Pruning Unused Networks

Over time, you might end up with networks that are no longer in use. To clean up these unused networks, you can use the docker network prune command:

docker network prune

This command removes all networks that are not currently being used by any containers.

By using these commands, you can effectively manage your Docker networks, ensuring your containers can communicate as needed and keeping your Docker environment tidy.

Welcome to the container cloud

Sliplane makes it simple to deploy containers in the cloud and scale up as you grow. Try it now and get started in minutes!