The Docker container is connected
Earlier, we implemented a network port to access services running within the docker container.
Let's implement connecting to a docker container through a port.
Network port mapping
We created a container for the python app.
w3cschool@w3cschool:~$ docker run -d -P training/webapp python app.py fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
In addition, we can specify the network address of the container binding, such as binding 127.0.0.1.
We use the -P parameter to create a container and docker ps to see port 5000 bound host port 32768.
w3cschool@w3cschool:~$ 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 identity to specify that the container port is bound to the host port.
The difference between the two approaches is:
- -P : is a high port that is randomly mapped to the host by the internal port of the container.
- -p : Is the internal port of the container bound to the specified host port.
w3cschool@w3cschool:~$ docker run -d -p 5000:5000 training/webapp python app.py 33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
w3cschool@w3cschool:~$ 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 of the container binding, such as binding 127.0.0.1.
w3cschool@w3cschool:~$ docker run -d -p 127.0.0.1:5001:5002 training/webapp python app.py 95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c w3cschool@w3cschool:~$ 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
This allows us to access the container's port 5002 by accessing 127.0.0.1:5001.
In the example above, the default is a bound tcp port, and if you want to bind a UPD port, you can add /udp after the port.
w3cschool@w3cschool:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py 6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a w3cschool@w3cschool:~$ 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
The docker port command allows us to quickly see how the port is bound.
w3cschool@w3cschool:~$ 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 to share connection information.
The docker connection creates a parent-child relationship where the parent container can see the information for the child container.
The container is named
When we create a container, docker automatically names it. In addition, we can use the --name identity to name containers, for example:
w3cschool@w3cschool:~$ docker run -d -P --name youj training/webapp python app.py 43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
We can use the docker ps command to view the container name.
w3cschool@w3cschool:~$ 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 youj