docker容器添加自定义hosts
https://www.cnblogs.com/erlou96/p/13884130.html
方案一
启动时增加hosts,参考自docker docs
方案二
docker-compose.yml文件指定,参考自stackoverflow
Add entries to container hosts file (--add-host)
https://docs.docker.com/reference/cli/docker/container/run/#add-host
You can add other hosts into a container's
/etc/hosts
file by using one or more--add-host
flags. This example adds a static address for a host namedmy-hostname
:docker run --add-host=my-hostname=8.8.8.8 --rm -it alpine / # ping my-hostname PING my-hostname (8.8.8.8): 56 data bytes 64 bytes from 8.8.8.8: seq=0 ttl=37 time=93.052 ms 64 bytes from 8.8.8.8: seq=1 ttl=37 time=92.467 ms 64 bytes from 8.8.8.8: seq=2 ttl=37 time=92.252 ms ^C --- my-hostname ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max = 92.209/92.495/93.052 ms
You can wrap an IPv6 address in square brackets:
docker run --add-host my-hostname=[2001:db8::33] --rm -it alpine
The
--add-host
flag supports a specialhost-gateway
value that resolves to the internal IP address of the host. This is useful when you want containers to connect to services running on the host machine.It's conventional to use
host.docker.internal
as the hostname referring tohost-gateway
. Docker Desktop automatically resolves this hostname, see Explore networking features.The following example shows how the special
host-gateway
value works. The example runs an HTTP server that serves a file from host to container over thehost.docker.internal
hostname, which resolves to the host's internal IP.echo "hello from host!" > ./hello
python3 -m http.server 8000 Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
docker run \ --add-host host.docker.internal=host-gateway \ curlimages/curl -s host.docker.internal:8000/hello hello from host!
The
--add-host
flag also accepts a:
separator, for example:docker run --add-host=my-hostname:8.8.8.8 --rm -it alpine
Networking overview
https://docs.docker.com/engine/network/
Understanding Docker DNS
https://medium.com/@prajwal.chin/understanding-docker-dns-2ed4b070a0
When working with Docker containers, networking plays a crucial role in enabling communication between containers and the outside world. One fundamental aspect of container networking is DNS (Domain Name System), which allows containers to discover and communicate with each other using domain names instead of relying on IP addresses. In this post, we’ll explore Docker DNS and how it facilitates container communication.
🔎 What is DNS?
The Domain Name System (DNS) is a hierarchical decentralized naming system that translates human-readable domain names, like www.example.com, into IP addresses, such as 192.0.2.1. DNS acts as the phonebook of the internet, enabling computers to locate and connect with each other using recognizable names.
🐋 Docker DNS Resolution
In the context of Docker, DNS resolution refers to the process of translating domain names into IP addresses for containers running on the same network. Docker provides a built-in DNS service that allows containers to resolve domain names to the correct IP addresses within the same Docker network. By default, Docker containers use the embedded DNS resolver provided by the Docker daemon.
🔌 Docker Default DNS Configurations
When you create a Docker network, it automatically configures DNS resolution for containers within that network. By default, Docker uses the following DNS configurations:
1️⃣ Docker daemon as the DNS server: Containers use the Docker daemon’s embedded DNS resolver, which listens on the default DNS server IP address 127.0.0.11. This DNS server handles DNS queries from containers and forwards them to the appropriate DNS server for resolution.
2️⃣ Container name as hostname: Docker automatically assigns a unique hostname to each container, which is the container’s name. This hostname can be used for internal DNS resolution within the Docker network.
3️⃣ Automatic DNS aliasing: Docker provides automatic DNS aliasing for container-to-container communication. Each container’s name is mapped to an IP address in the Docker network’s DNS server. This allows containers to resolve other containers’ domain names by using their respective names as hostnames.