Docker connect from a container to a service on the host

如何從 Docker 容器連到本機的 host port

情境

SSH Tunnel 連至遠端伺服器 (localhost:8888 -> remote:3306)

想透過 docker 啟動的 web service (LNMP) 操作 adminer/phpmyadmin 連至遠端的資料庫。

原本想更改 docker network mode 為 host , 讓容器直接使用同一張網卡,但 windows 和 Mac 環境不支援 (只支援 Linux),查了好久終於找到解法。

https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds

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. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

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

解法

使用 host.docker.internal:8888

Leave a Reply

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>