03. Containers Connection
July 9, 2024Less than 1 minute
Port Mapping
docker run -d -P training/webapp python app.py
docker run -d -p 5000:5000 training/webapp python app.py
Interpretation
- -P: map to a random local port
- -p: map to a specific local port
View Port Mapping
docker port <container> 5000
Containers Connection
Create network
docker network create -d bridge test-net
Interpretation
- -d: specify the net type (bridge/overlay)
Connect Container
Run a new container and connect to network test-net
docker run -itd --name test1 --network test-net ubuntu /bin/bash
docker run -itd --name test2 --network test-net ubuntu /bin/bash
Now the test1 and test2 can connect each other.
Config DNS
Add configuration to /etc/docker/daemon.json
.
{
"dns": [
"114.114.114.114",
"8.8.8.8"
]
}
Using the follow command to view the DNS infodocker run -it --rm ubuntu cat etc/resolv.conf
Specify DNS for container
docker run -it --rm -h host_ubuntu --dns=114.114.114.114 --dns-search=test.com ubuntu
Interpretation
- --rm: The file system will clean up when exit a container
- -h Hostname: set the container system name, the name can be write to
/etc/hostname
and/etc/hosts
. - --dns=IP_ADDRESS: add dns to
/etc/resolv.conf
- --dns-search=DOMAIN: set the container search domain.