在 Linux 中托管 Docker Internal
Docker 允许开发人员通过将应用程序包装在称为容器的标准化单元中来高效地构建、测试和部署应用程序。 在使用 Docker 容器时,您可能会遇到需要将容器与主机连接的场景。
Linux 等效于 host.docker.internal 命令
18.03 之前的 Docker 版本仅在 Mac 和 Windows 系统上支持 host.docker.internal
命令。 不过2020年12月Docker发布了20.10.0版本,后续版本支持在Linux机器上使用host.docker.internal命令连接主机。
根据 Docker 文档,如果主机的 IP 地址不断变化,我们可以使用 host.docker.internal
连接到主机,它解析为主机的内部 IP 地址。 但是,这只能在开发环境中使用。
在 Windows 和 Mac 中,使用 host.docker.internal
连接到主机要容易得多。 假设我们想连接到主机默认端口 3606 上运行的 MySQL 服务。
我们可以使用以下命令让 Docker 容器连接到此服务。
代码:
host.docker.internal:3306
另一方面,在 Linux 系统上,我们需要添加一个额外的标记才能成功连接到主机。 通过在 docker run
命令旁边添加 --add-host 标志,这仅适用于 20.10.0 之后的 Docker 版本。
代码:
$ docker run -d --add-host host.docker.internal:host-gateway new_container
此命令向 etc/hosts 目录添加一个新条目,将 host.docker.internal
映射到主机网关。 或者,我们也可以使用 172.17.0.1,也称为 localhost,这是 Docker 中默认桥接网络的网关地址。
Docker 网络是一个广泛的主题,上面的命令或方法可以根据你想要完成的事情而有不同的使用方式。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application