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?

More than 3 years have passed since last update.

docker-composeで環境変数を使用する

Last updated at Posted at 2022-07-23

docker-composeで環境変数を使用してみました。

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:5.7
    container_name: "mysql_${ENV_NAME:-default_name}"  #ここがポイント
    environment:
      - TZ=Asia/Tokyo
      - MYSQL_ROOT_PASSWORD=pass

ENV_NAME設定なしの場合

$ docker-compose up -d
$ docker-compose ps
       Name                    Command             State          Ports
------------------------------------------------------------------------------
mysql_default_name   docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp

ENV_NAME設定ありの場合
コンテナ名が変わりましたね。

$ export ENV_NAME=custom_name
$ docker-compose up -d
$ docker-compose ps
      Name                    Command             State          Ports
-----------------------------------------------------------------------------
mysql_custom_name   docker-entrypoint.sh mysqld   Up      3306/tcp, 33060/tcp

公式ドキュメントにも記載があります。
https://docs.docker.com/compose/compose-file/#interpolation

以上です。

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?