The best Docker container connection Tutorial In 2024, In this tutorial you can learn Network Port Mapping,Docker container connection,
In front of us to achieve access to the network through the port is operating in the docker container services. Let's connect to a docker container port
We created a python application container.
w3big@w3big:~$ docker run -d -P training/webapp python app.py fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
In addition, we can specify the network address binding container, such as binding 127.0.0.1.
We use the -P parameter to create a container, use docker ps port 5000 is bound to see a host port 32768.
w3big@w3big:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fce072cc88ce training/webapp "python app.py" 4 minutes ago Up 4 minutes 0.0.0.0:32768->5000/tcp grave_hopper
We can also use the -p flag to specify the container port is bound to the host port.
Is the difference between the two ways:
w3big@w3big:~$ docker run -d -p 5000:5000 training/webapp python app.py 33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
w3big@w3big:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33e4523d30aa training/webapp "python app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" 8 minutes ago Up 8 minutes 0.0.0.0:32768->5000/tcp grave_hopper
In addition, we can specify the network address binding container, such as binding 127.0.0.1.
w3big@w3big:~$ docker run -d -p 127.0.0.1:5001:5002 training/webapp python app.py 95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c w3big@w3big:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 95c6ceef88ca training/webapp "python app.py" 6 seconds ago Up 6 seconds 5000/tcp, 127.0.0.1:5001->5002/tcp adoring_stonebraker 33e4523d30aa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" 10 minutes ago Up 10 minutes 0.0.0.0:32768->5000/tcp grave_hopper
So that we can access the container through the access port 127.0.0.1:5001 5002.
The above example, the default is to bind tcp port, if you want to bind UPD ports, a port in the back plus / udp.
w3big@w3big:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py 6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a w3big@w3big:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6779686f06f6 training/webapp "python app.py" 4 seconds ago Up 2 seconds 5000/tcp, 127.0.0.1:5000->5000/udp drunk_visvesvaraya 95c6ceef88ca training/webapp "python app.py" 2 minutes ago Up 2 minutes 5000/tcp, 127.0.0.1:5001->5002/tcp adoring_stonebraker 33e4523d30aa training/webapp "python app.py" 5 minutes ago Up 5 minutes 0.0.0.0:5000->5000/tcp berserk_bartik fce072cc88ce training/webapp "python app.py" 12 minutes ago Up 12 minutes 0.0.0.0:32768->5000/tcp grave_hopper
docker port command allows us to quickly and easily view the port binding information.
w3big@w3big:~$ docker port adoring_stonebraker 5002 127.0.0.1:5001
Port mapping is not the only way to connect to another docker container.
docker has a connection system allows multiple containers together, sharing the connection information.
docker connection will create a parent-child relationship, the parent container in which you can see the information of the sub container.
When we create a container of time, docker it will automatically be named. In addition, we can also use the --name to identify the naming container, for example:
w3big@w3big:~$ docker run -d -P --name w3big training/webapp python app.py 43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
We can view the name of the container using a docker ps command.
w3big@w3big:~$ docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 43780a6eabaa training/webapp "python app.py" 3 minutes ago Up 3 minutes 0.0.0.0:32769->5000/tcp w3big