Stay Hungry,Stay Foolish!

docker dns

docker容器添加自定义hosts

https://www.cnblogs.com/erlou96/p/13884130.html

方案一

动时增加hosts,参考自docker docs

 
docker run -d --name test1 \ --add-host test1.a:1.2.3.4 \ local/test

 

方案二

docker-compose.yml文件指定,参考自stackoverflow

 
test2: build: local/test extra_hosts: test1.a: 1.2.3.4 test1.b: 4.3.2.1

 

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 named my-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 special host-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 to host-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 the host.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.

 

posted @   lightsong  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2017-01-05 由LazyMan联想到的
千山鸟飞绝,万径人踪灭
点击右上角即可分享
微信分享提示