Docker network examples
You can create a Docker network using the "docker network create" command followed by the network name. For example:
docker network create my_network
This command creates a custom bridge network named "my_network".
You can list all Docker networks on your system using the "docker network ls" command.
To inspect details about a specific Docker network, you can use the "docker network inspect" command followed by the network name or ID. For example:
docker network inspect my_network
You can connect containers to a specific network using the "--network" option when running the container with "docker run". For example, to connect a container to the "my_network" network:
docker run -d --name my_container --network my_network nginx
This command runs an NGINX container named "my_container" in detached mode ("-d") and connects it to the "my_network" network.
To disconnect a container from a network, you can use the "docker network disconnect" command followed by the network name and container name or ID. For example:
docker network disconnect my_network my_container
To remove a Docker network, you can use the "docker network rm" command followed by the network name. For example:
docker network rm my_network
Note that you cannot remove a network that is currently in use by one or more containers. You must first disconnect any containers from the network before deleting it.
You've now learned how to create, list, inspect, and manage Docker networks using the Docker CLI.