[转]docker访问宿主机 host.docker.internal 域名不生效的问题

原文地址:docker 网络问题 host.docker.internal 不生效? - SegmentFault 思否

host.docker.internal 是一个开发功能,只在 Docker Desktop 有效。你用的是 Docker Desktop 吗?(Linux 下一般都不是)

https://docs.docker.com/deskt...

The host has a changing IP address (or none if you have no network access). We recommend that you connect to the special DNS name host.docker.internal which resolves to the internal IP address used by the host. This is for development purpose and does not work in a production environment outside of Docker Desktop.

 

Docker 宿主机默认ip 为 172.17.0.1 可以在容器中使用这个ip来访宿主机的服务。

也可以更改 /etc/docker/daemon.json 来绑定指定的ip

{
    "bip":"172.0.0.1/24"
}

踩过的坑

在dockerFile 里面直接操作hosts 文件是不被允许的

#0 0.161 /bin/sh: 1: cannot create /etc/hosts: Read-only file system

但又不想每次容器运行去覆盖hosts文件 使用CMD 命令

建议还是取个能记住的ip 地址得了,折腾好久了!!!!

posted @ 2024-05-08 14:57  dirgo  阅读(476)  评论(0编辑  收藏  举报