Docker container connection
Earlier we implemented access to services running in docker containers through network ports. Let's connect to a docker container through the port
Network port mapping
We created a container for the python application.
php@php:~$ docker run -d -P training/webapp python app.py fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
In addition, we can specify the network address to which the container is bound, such as binding 127.0.0.1.
We use the -P parameter to create a container and use docker ps to see that port 5000 is bound to host port 32768.
php@php:~$ 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 that the container port is bound to the host port.
The difference between the two methods is:
-P: is the container’s internal port that is randomly mapped to the host’s high port.
-p : is the container internal port bound to the specified host port.
php@php:~$ docker run -d -p 5000:5000 training/webapp python app.py 33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
php@php:~$ 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 to which the container is bound, such as binding 127.0.0.1.
php@php:~$ docker run -d -p 127.0.0.1:5001:5002 training/webapp python app.py 95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c php@php:~$ 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 we can access the container's 5002 port by accessing 127.0.0.1:5001.
In the above example, the tcp port is bound by default. If you want to bind the UPD port, you can add /udp after the port.
php@php:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py 6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a php@php:~$ 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 check the binding status of the port.
php@php:~$ docker port adoring_stonebraker 5002 127.0.0.1:5001
Docker Container Connection
Port mapping is not the only way to connect docker to another container.
Docker has a connection system that allows multiple containers to be connected together and share connection information.
Docker connection will create a parent-child relationship, in which the parent container can see the information of the child container.
Container naming
When we create a container, docker will automatically name it. In addition, we can also use the --name flag to name the container, for example:
php@php:~$ docker run -d -P --name php training/webapp python app.py 43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
We can use the docker ps command to view the container name.
php@php:~$ 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 php