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] depends onについて

Last updated at Posted at 2023-01-17

自己紹介

初めまして。私は都内でwebエンジニアをしています。

今の会社にインターンとして経て、入社しました。

業務では、Nuxtjs, Golang, Kubernetesを用いて運用保守開発や

最近はPandasを用いてデータ分析など日々苦戦している若輩者です。

背景

Dockerのdepends_onについて、簡単に学んだことをまとめました。

対象としている人

Dockerについて、一通り学習したことがある人

depends_on

サービス間の依存関係を指定できる

docker-compose.yaml
services:
  service_a:
    image: busybox
  service_b:
    image: busybox
    # `service_b` を `service_a` に依存させる
    depends_on:
      - service_a

service_b を service_a に依存させることができます

  • docker-compose up: service_a → service_b の順に起動する
  • docker-compose run: ( docker-compose up と同じ)
  • docker-compose stop: service_b → service_a の順に停止する

指定パターン

  • Short syntax (リスト)
    • 依存先のサービス名を単純に記述するだけ
docker-compose.yaml
depends_on:
  - service_a
  • Long syntax (オブジェクト)
    • 依存関係にあるサービスをオブジェクト形式で記述するもの
    • この方法では依存先に加えて「条件」を指定することができます
docker-compose.yaml
depends_on:
  service_a:
    condition: service_started

条件の指定には condition を使用
condition にデフォルト値は無いため、 long syntax を選ぶ場合は必ず指定する必要があり

  • service_started
    • 依存先のサービスが起動したら起動する
      • 依存先のサービスが「起動したこと」を条件とします
  • service_healthy
    • 依存先のサービスが起動して、なおかつ、 healthcheck が通ったら起動する
      • 依存先のサービスが起動してなおかつ healthcheck が通ることを条件とする
  • service_completed_successfully
    • 依存先のサービスが正常終了したら起動する
      • 依存先のサービスが正常に終了したことを条件
0
0
1

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?