gpt4 book ai didi

docker - 从 docker 容器访问主机数据库

转载 作者:IT老高 更新时间:2023-10-28 12:33:18 33 4
gpt4 key购买 nike

如果我在某个主机上运行 mysql 数据库,并且该主机也运行 docker 容器:我将如何从主机上运行的 docker 容器中访问 mysql 数据库?。

例如,有没有办法将主机端口发布到容器(与 docker run -p 相反)?

最佳答案

来自 18.03 文档:

I want to connect from a container to a service on the host

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.

The gateway is also reachable as gateway.docker.internal.

示例:这是我用于容器内的 MySQL 连接字符串以访问主机上的 MySQL 实例的内容:

mysql://host.docker.internal:3306/my_awesome_database

关于docker - 从 docker 容器访问主机数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28056522/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com