LoginSignup
2
1

More than 5 years have passed since last update.

Dockerで.envを活用してみたいメモ

Last updated at Posted at 2018-04-02

やりたいこと

.envを活用して色々やってみる。

使う時

前準備として、以下のようなファイルをdocker-compose.ymlと同じディレクトリに作っておく

SAMPLE_VAR=local.sample.jp

docker-compose.ymlで使う

${変数名} で参照できる

version: 3
services:
  name: sample
  environments:
    - SAMPLE_VAR: ${SAMPLE_VAR}
  extra_hosts:
    - 127.0.0.1: ${SAMPLE_VAR}

Dockerfileで使う

docker-compose.ymlのenvironmentsに書いておけば${SAMPLE_VAR}を受け取れる。

docker-compose.yml

version: 3
services:
  name: sample
  environments:
    - SAMPLE_VAR: ${SAMPLE_VAR}

Dockerfile

RUN echo $SAMPLE_VAR
# 出力: local.sample.jp

マウントしたファイルの中身に環境変数をセットさせる(番外編)

envsubstが必要

text.txt.template

${SAMPLE_VAR}

docker-compose.yml

version: 3
services:
  name: sample
  environments:
    - SAMPLE_VAR: ${SAMPLE_VAR}
  volumes:
    - ./text.txt.template: ./text.txt.template
  command:
    - envsubst '$$SAMPLE_VAR' < ./text.txt.template > ./text.txt
2
1
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
2
1