YouTip LogoYouTip

Docker Container Connection

Previously, we implemented accessing services running in docker containers through network ports. Network applications can run in containers, and to allow external access to these applications, you can use the **-P** or **-p** parameter to specify port mapping. Below we will implement connecting to a docker container through ports. * * * ## Network Port Mapping We created a python application container. tutorial@tutorial:~$ docker run -d -P training/webapp python app.py fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d Additionally, we can specify the network address to bind the container to, for example binding to 127.0.0.1. We use **-P** to bind the port number, and using **docker ps** you can see container port 5000 is bound to host port 32768. tutorial@tutorial:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper We can also use the **-p** flag to specify container port binding to a host port. The difference between the two methods is: * **-P:** Maps container internal ports **randomly** to host ports. * **-p:** Binds container internal ports to **specified** host ports. tutorial@tutorial:~$ docker run -d -p 5000:5000 training/webapp python app.py 33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0 tutorial@tutorial:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper Additionally, we can specify the network address to bind the container to, for example binding to 127.0.0.1. tutorial@tutorial:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py 95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c tutorial@tutorial:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker 33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper This way we can access the container's port 5000 by visiting 127.0.0.1:5001. In the examples above, TCP ports are bound by default. To bind a UDP port, add **/udp** after the port. tutorial@tutorial:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py 6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a tutorial@tutorial:~$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 6779686f06f6 training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya 95c6ceef88ca training/webapp "python app.py" ... 5000/tcp, 127.0.0.1:5001->5000/tcp adoring_stonebraker 33e4523d30aa training/webapp "python app.py" ... 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" ... 0.0.0.0:32768->5000/tcp grave_hopper The **docker port** command allows us to quickly check port binding status. tutorial@tutorial:~$ docker port adoring_stonebraker 5000127.0.0.1:5001 * * * ## Docker Container Connection Port mapping is not the only way to connect docker to another container. Docker has a linking system that allows multiple containers to be connected together, sharing connection information. Docker linking creates a parent-child relationship, where the parent container can see information about the child container. * * * ### Container Naming When we create a container, docker automatically assigns it a name. Additionally, we can use the **--name** flag to name the container, for example: tutorial@tutorial:~$ docker run -d -P --name tutorial training/webapp python app.py 43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441 We can use the **docker ps** command to view container names. tutorial@tutorial:~$ docker ps -l CONTAINER ID IMAGE COMMAND ... PORTS NAMES 43780a6eabaa training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp tutorial ### Create a Network First, create a new Docker network below. $ docker network create -d bridge test-net !(#) Parameter description: **-d**: The parameter specifies the Docker network type, which can be bridge or overlay. The overlay network type is used for Swarm mode, you can ignore it in this section. ### Connect Containers Run a container and connect it to the test-net network: $ docker run -itd --name test1 --network test-net ubuntu /bin/bash Open a new terminal and run another container and join it to the test-net network: $ docker run -itd --name test2 --network test-net ubuntu /bin/bash Click
← Eclipse CharsetNumber Toradians β†’