The secret of Docker network connection: Make your containers interconnected and even more powerful!

Summary

  • Docker has become the swiss army knife of modern application development and deployment, but there are still many people who don't know much about container networking. This blog will unveil the mystery of Docker network connection for you, and let your containers communicate with each other in the network, even more powerful! From basic concepts to advanced operations, let us explore the world of Docker networking, take you through container communication with ease, and help you become a master of container networking!

A container network basic command

  • When it comes to container networking, Dockerthere are several networking options available to control how containers communicate with the host and with other containers.
  1. Docker network type:

    • bridgeNetwork: DockerA network created by default, suitable for container communication on a single host. Each container is assigned an IPaddress and can communicate using the container name or IP address.
    • hostNetwork: Containers use the host network directly, sharing the host's network namespace. Containers share the same IPaddresses and ports as the host.
    • overlayNetworking: Used to connect containers across multiple hosts. Applicable Swarmmode, which allows containers to communicate between different hosts, so that containers in a multi-host environment can run as if they were on a single host.
    • macvlanNetwork: Allows the container to be bound to MACthe address of the physical network interface, so that the container has the same MACaddress as the physical device, which is suitable for some special network configuration requirements.
  2. View the list of networks:

    docker network ls
    
  3. Create a custom network:

    docker network create <network_name>
    
  4. Specify the network when creating the container:

    docker run -d --name <container_name> --network <network_name> <image_name>
    
  5. Connect the container to the network:

    docker network connect <network_name> <container_name>
    
  6. Disconnect the container from the network:

    docker network disconnect <network_name> <container_name>
    
  7. View the network information of the container:

    docker inspect <container_name> --format='{
          
          {.NetworkSettings.Networks}}'
    
  8. Port Mapping:

    • Use -pthe parameter to map a port inside the container to a port on the host. For example, to map port 8080 inside the container to port 80 on the host:
    docker run -d -p 80:8080 --name <container_name> <image_name>
    
  9. Shell connected to the container:

    docker exec -it <container_name> /bin/bash
    
  10. Delete network:

    docker network rm <network_name>
    

Two actual cases

2.1 View the network where the container is located

To check if two containers are on the same network you can use the following command:

  1. Get the container's network name or ID. Run the following command:
    docker inspect <容器ID或名称> --format='{
          
          {.NetworkSettings.Networks}}'
    
  2. Compares the network information of two containers, if they have the same network name or ID, they are on the same network.
  • Demonstration result:
    root@armbian:/usr/local/aurora-springboot# docker inspect services --format='{
          
          {.NetworkSettings.Networks}}'
    map[docker_default:0x4000530180]
    root@armbian:/usr/local/aurora-springboot# docker inspect aurora-springboot-0.0.1.jar --format='{
          
          {.NetworkSettings.Networks}}'
    map[bridge:0x40005dc9c0]
    
    • The first container services are in the network docker_default, while the second container aurora-springboot-0.0.1.jaris in the network bridge, the two are not in the same network.

2.2 Container joins the network

  • If you want these two containers to communicate on the same network, consider connecting them to the same network. You can use docker network connectthe command to attach a container to a specified network. For example, you can run the following command to connect the second container to docker_defaultthe network:
docker network connect docker_default aurora-springboot-0.0.1.jar

2.3 Persistence of network configuration

  • If the container holding the project is restarted, will the network connection still work?
  • When restarting the container, the network connection will remain valid. **The network configuration associated with the container is persistent, so after restarting the container it will reconnect to the previously connected network. It only disconnects from the network when the container is deleted. **So if the container holding the project is restarted, the network connection will remain the same, the container will remain on the same network and can communicate with other containers.

2.4 Container join and remove network

root@armbian:/usr/local/aurora-springboot# docker inspect aurora-springboot-0.0.1.jar --format='{
    
    {.NetworkSettings.Networks}}'
map[bridge:0x40002fe000 docker_default:0x40002fe180]

Now that aurora-springboot-0.0.1.jarthe container is in both networks, remove it bridge:0x40002fe000and keep it only in docker_default:0x40002fe180the network.

  1. Stop the container:
    docker stop aurora-springboot-0.0.1.jar
    
  2. Remove the container's network connection:
    docker network disconnect bridge aurora-springboot-0.0.1.jar
    
  3. Connect the container to the target network:
    docker network connect docker_default aurora-springboot-0.0.1.jar
    
  4. Start the container:
    docker start aurora-springboot-0.0.1.jar
    

Now, aurora-springboot-0.0.1.jarthe container will only remain in docker_defaultthe network, and will no longer bridgebe associated with the network.

Guess you like

Origin blog.csdn.net/yang2330648064/article/details/131487614