2019.08現在、公式の方法は無いらしい(多分)

–build-argsを使う

.envはこんな感じ

1
2
3
4
$ cat .env
HOGE=hoge
FUGA=fuga
PIYO=piyo

Dockerfileはこんな感じ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ cat Dockerfile
FROM debian:buster-slim

ARG HOGE
ENV HOGE ${HOGE:-xxx}

ARG FUGA
ENV FUGA ${FUGA:-xxx}

ARG PIYO
ENV PIYO ${PIYO:-xxx}

ARG FOO
ENV FOO ${FOO:-xxx}

ARG BAR
ENV BAR ${BAR:-xxx}

RUN echo ${HOGE}
RUN echo ${FUGA}
RUN echo ${PIYO}
RUN echo ${FOO}
RUN echo ${BAR}

実行コマンド

1
$ docker build --no-cache --force-rm `cat .env | xargs -IENV echo --build-arg ENV | tr '\n' ' '` .
1
2
3
4
5
6
7
8
9
10
11
// ビルドログ(抜粋)
Step 12/16 : RUN echo ${HOGE}
hoge
Step 13/16 : RUN echo ${FUGA}
fuga
Step 14/16 : RUN echo ${PIYO}
piyo
Step 15/16 : RUN echo ${FOO}
xxx
Step 16/16 : RUN echo ${BAR}
xxx