English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We have previously implemented accessing services running in docker containers through network ports.
Some network applications can run in the container. To allow external access to these applications, you can do so by -P or -p parameter to specify port mapping.
Next, we will implement connecting to a docker container through a port.
We created a container for a python application.
w3codebox@w3codebox:~$ docker run -d -P training/webapp python app.py fce072cc88cee71b1cdceb57c2821d054a4a59f67da6b416fceb5593f059fc6d
In addition, we can specify the network address bound to the container, such as binding 127.0.0.1.
We use -P Parameter to create a container, using docker ps As can be seen from the container port 5000 Binding host port 32768.
w3codebox@w3codebox:~$ 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 -p Identify to specify the container port binding to the host port.
The difference between the two methods is:
-P :is the container internal portRandommapped to the high port on the host.
-p : is the container internal port bound toSpecifyto the host port.
w3codebox@w3codebox:~$ docker run -d -p 5000:5000 training/webapp python app.py 33e4523d30aaf0258915c368e66e03b49535de0ef20317d3f639d40222ba6bc0
w3codebox@w3codebox:~$ 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
In addition, we can specify the network address bound to the container, such as binding 127.0.0.1.
w3codebox@w3codebox:~$ docker run -d -p 127.0.0.1:5001:5000 training/webapp python app.py 95c6ceef88ca3e71eaf303c2833fd6701d8d1b2572b5613b5a932dfdfe8a857c w3codebox@w3codebox:~$ 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 127.0.0.1:5001 to access the container 5000 port.
In the above example, the default is to bind the TCP port. If you want to bind the UDP port, you can add it after the port number. /udp.
w3codebox@w3codebox:~$ docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py 6779686f06f6204579c1d655dd8b2b31e8e809b245a97b2d3a8e35abe9dcd22a w3codebox@w3codebox:~$ 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
docker port Command allows us to quickly view the port binding status.
w3codebox@w3codebox:~$ docker port adoring_stonebraker 5000 127.0.0.1:5001
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, sharing connection information.
docker connection will create a parent-child relationship, where the parent container can see the information of the child container.
When we create a container, docker will automatically name it. In addition, we can also use --name Identifier to name the container, for example:
w3codebox@w3codebox:~$ docker run -d -P --name w3codebox training/webapp python app.py 43780a6eabaaf14e590b6e849235c75f3012995403f97749775e38436db9a441
We can use docker ps Command to view container names.
w3codebox@w3codebox:~$ docker ps -l CONTAINER ID IMAGE COMMAND ... PORTS NAMES 43780a6eabaa training/webapp "python app.py" ... 0.0.0.0:32769->5000/tcp w3codebox
First, let's create a new Docker network.
$ docker network create -d bridge test-net
Parameter Description:
-dThe parameter specifies the Docker network type, which has bridge and overlay.
Among them, the overlay network type is used for Swarm mode, which can be ignored in this section.
Run a container and connect to the newly created test-net network:
$ docker run -itd --name test1 --network test-net ubuntu /bin/bash
Open a new terminal and run another container and add it to test-net network:
$ docker run -itd --name test2 --network test-net ubuntu /bin/bash
Click the image to view the full size:
Below, we use ping to prove that test1 Container and test2 The container has established an interconnection.
If test1、test2 If there is no ping command in the container, execute the following command to install ping (instant learning: you can install it in one container, commit the container to an image, and then run the above two containers with the new image).
apt-get update apt install iputils-ping
In test1 The container can enter the following command:
Click the image to view the full size:
Similarly in test2 The container will also successfully connect to:
Click the image to view the full size:
So, test1 Container and test2 The container has established an interconnection.
If you have multiple containers that need to connect to each other, it is recommended to use Docker Compose, which will be introduced later.
We can configure DNS on the host machine /etc/docker/Add the following content to the daemon.json file to set the DNS for all containers:
{ "dns" : [ "114.114.114.114", "8.8.8.8" ] }
After setting, the DNS of the container will be automatically configured as 114.114.114.114 and 8.8.8.8.
After the configuration is complete, you need to restart docker for it to take effect.
You can use the following command to check if the container's DNS is effective, which will output the container's DNS information:
$ docker run -it --rm ubuntu cat etc/resolv.conf
Click the image to view the full size:
Manually specify the container's configuration
If you only want to set DNS in the specified container, you can use the following command:
$ docker run -it --rm -h host_ubuntu --dns=114.114.114.114 --dns-search=test.com ubuntu
Parameter Description:
--rm: Automatically clean up the container's file system when the container exits.
-h HOSTNAME or --hostname=HOSTNAME: Set the container's hostname, which will be written to the container's /etc/hostname and /etc/hosts.
--dns=IP_ADDRESS: Add DNS server to the container's /etc/in resolv.conf, let the container use this server to resolve all domains not in /etc/in the hosts.
--dns-search=DOMAIN: Set the container's search domain. When the search domain is set to .example.com, when searching for a host named host, DNS will not only search host but also host.example.com.
Click the image to view the full size:
If no DNS is specified when the container starts --dns and --dns-search, Docker will use the host machine's /etc/Configure the container's DNS using resolv.conf.