0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

docker compose fileを分割する

Posted at

背景

kindindでcontainerを複数準備する必要があり、composeファイルが徐々に肥大化して辛くなってしまっていた。
yamlのファイル分割は良いが、他のアプローチが存在しないかと思いreference したらあったので、覚書と軽く運用方法を記載する。

TL;DR

  1. Includeを利用する
  2. Merrgeを利用する

0. 現状

frontendとapi serverの二種類のみ作成します。(redisやdbやproxyなどは置いておく)

tree . -L 2
.
├── README.md
├── apps
│   ├── api-server # api server
│   └── web_client # vite などweb-frontendのrun server
├── compose.yaml
compose.yaml
services:
  nest_container:
    container_name: nest_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - ./apps/api-server:/workspace
    tty: true
    restart: on-failure:3
    ports:
      - 3000:3000
    networks:
      - nest_container
  web_client:
    container_name: web_client_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - ./apps/web_client:/workspace
    tty: true
    restart: on-failure:3
    ports:
      - 4200:4200
    networks:
      - nest_container

networks:
  nest_container: {}

1. Includeを利用する

Includeは、docker compose upで利用するcompose.yamlに依存関係をinclude blockで記載する。

tree . -L 3 -P 'compose.yaml' --prune
.
├── apps
│   ├── app-server
│   │   └── compose.yaml
│   └── web_client
│       └── compose.yaml
└── compose.yaml
compose.yaml
include:
  - apps/app-server/compose.yaml
  - apps/web_client/compose.yaml

networks:
  nest_container: {}
apps/app-server/compose.yaml
services:
  nest_container:
    container_name: nest_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - .:/workspace
    tty: true
    restart: on-failure:3
    ports:
      - 3000:3000
    networks:
      - nest_container
apps/web_client/compose.yaml
services:
  web_client:
    container_name: web_client_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - .:/workspace
    tty: true
    restart: on-failure:3
    networks:
      - nest_container

2. Mergeを利用する

Mergeは、-fでファイルを指定して統合利用することができる。

tree . -L 3 -P 'compose.yaml' --prune
.
├── apps
│   ├── app-server
│   │   └── compose.yaml
│   └── web_client
│       └── compose.yaml
apps/app-server/compose.yaml
services:
  nest_container:
    container_name: nest_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - .:/workspace
    tty: true
    restart: on-failure:3
    ports:
      - 3000:3000
    networks:
      - nest_container

networks:
  nest_container:
    external: true
apps/web_client/compose.yaml
services:
  web_client:
    container_name: web_client_container
    build:
      context: .
      dockerfile: dockerfile
      target: development
      args:
        - NODE_VERSION=22.13
    volumes:
      - .:/workspace
    tty: true
    restart: on-failure:3
    networks:
      - nest_container

networks:
  nest_container:
    external: true

networkについては、上書きをベースに記載すればいいが、順序性が生まれて地味にめんどくさそうなので、最初に作る想定でよいかなと思います。

compose = @docker compose

.PHONY: local.up, local.init

local.init:
	@docker network create -d bridge nest_container

local.up:
	$(compose) -f apps/app-server/compose.yaml -f apps/web_client/compose.yaml up -d

一丁一旦ありそうな気がするのですが、localでとりあえず開発する時はIncludeで良いかなと思った。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?