Wayan Jimmy's Brain

Docker Network

Docker Mastery

Docker Network

Run a docker container

docker container run -p 80:80 --name webhost -d nginx

see published ports

docker container port webhost

Get ip address using the inspect command

docker container inspect --format "{{.NetworkSettings.IPAddress}}" webhost

Docker Network CLI

bridge is the default network

Create a new network

docker network create my_app_net

List all networks, you will see the new added network

docker network ls

Run a new nginx container in the new network

docker container run -d --name new_nginx --network my_app_net nginx:alpine

Inspect the my_app_net network, we can see our new_nginx container connected to the network

docker network inspect my_app_net | jq '.[0].Containers'

Connect existing container to a network, what about we connect our new_nginx container to the bridge network

# get the new_nginx container id
docker container ls
# docker network connect <network_id> <container_id>
docker network connect 9dca90e4801a 4f3dbbf7fb70

Inspect the container, now we can see the container connected to 2 network

docker container inspect new_nginx | jq '.[0].NetworkSettings.Networks' | jq 'keys'

Try to connect our old nginx container to the new my_app_net network

docker network connect 3c4eb7d8f105 959f536cd6f2

then inspect the network

docker network inspect 3c4eb7d8f105 | jq '.[0].Containers' | fx 'Object.entries' | fx '.[].filter(x => x.Name)' | fx '.[].Name'

Disconnect from a network

docker network disconnect 3c4eb7d8f105 959f536cd6f2

inspect again

docker network inspect 3c4eb7d8f105 | jq '.[0].Containers' | fx 'Object.entries' | fx '.[].filter(x => x.Name)' | fx '.[].Name'