25
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker for Macでコンテナからホストのサーバに接続する

Last updated at Posted at 2018-05-31

概要

Dockerのコンテナの中からホスト(macOS)に接続したい場合、 host.docker.internal を使えばアクセスできます。

DNS name host.docker.internal should be used for host resolution from containers. Older aliases (still valid) are deprecated in favor of this one. (See https://tools.ietf.org/html/draft-west-let-localhost-be-localhost-06).

動作確認

$ docker run -d -p 80:80 --name webserver nginx # nginxのサーバを立ち上げる
$ wget -O - http://localhost # localhostでつながる
$ docker run -it alpine sh # 適当なコンテナ立ち上げて中に入る

以下コンテナ内で実行

# wget -O - http://localhost # localhostではつながらない
# wget -O - http://host.docker.internal # これでつながる
25
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?