5
3

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 1 year has passed since last update.

docker-compose.yml ネットワーク周りの覚え書き

Posted at

概要

  • Dockerコンテナ内からホストにアクセスする方法(Linux)
  • docker-composeでコンテナのIPアドレスを固定する方法

Dockerコンテナ内からホストにアクセスする方法(Linux)

version: '3'
services:
  test:
    image: IMAGE
    ports:
      - 8080:8080
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - ./test/:/test

docker-composeには、extra_hostsというオプションがあるのでそれを使用。
host.docker.internal:host-gatewayでコンテナ内からホストにアクセスできるように、ホストのIPアドレスを渡す。

docker-composeでコンテナのIPアドレスを固定する方法

固定サブネットでネットワークを定義する。(今回は172.30.0.0/24を指定)
test_networkはネットワーク名なので任意。

version: '3'

networks:
  test_network:
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/24

上記で設定したネットワークを指定したコンテナをデプロイ。

services:
  test:
    image: IMAGE
    ports:
      - 8080:8080
    volumes:
      - ./test/:/test
    networks:
      test_network:
        ipv4_address: 172.30.0.2

こちらが全体像

version: '3'

networks:
  test_network:
    ipam:
      driver: default
      config:
        - subnet: 172.30.0.0/24

services:
  test:
    image: IMAGE
    ports:
      - 8080:8080
    volumes:
      - ./test/:/test
    networks:
      test_network:
        ipv4_address: 172.30.0.2
5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?