0
1

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.

Dockerfileでビルド時に外部から変数を注入して、ファイルに書き込みたい

Posted at

目的

  • Dockerfileでビルド時に外部から変数を注入して、ファイルに書き込みたい

ARGとENVを用いる

Dockerfile
FROM alpine
ARG TEST
ENV TEST ${TEST}
RUN echo "TEST eq ${TEST}" > /test

docker-compose.yml
version: "3"

services:
  testweb:
    build: 
      context: .
      dockerfile: ./Dockerfile
      args:
        - TEST="aaa"
    tty: true
$ docker-compose up --build -d && docker ps
Building testweb
Step 1/4 : FROM alpine
 ---> d6e46aa2470d
Step 2/4 : ARG TEST
 ---> Using cache
 ---> 7a550663ef1a
Step 3/4 : ENV TEST ${TEST}
 ---> Using cache
 ---> 63c05c3ea630
Step 4/4 : RUN echo "TEST eq ${TEST}" > /test
 ---> Using cache
 ---> 8e0906c4cb26

Successfully built 8e0906c4cb26
Successfully tagged test2_testweb:latest
Starting test2_testweb_1 ... done
CONTAINER ID   IMAGE           COMMAND     CREATED          STATUS                  PORTS     NAMES
e4726f3e8e02   test2_testweb   "/bin/sh"   11 seconds ago   Up Less than a second             test2_testweb_1

$ docker exec -it e4 sh
/ # cat /test
TEST eq aaa

Ref

Dockerfile リファレンス — Docker-docs-ja 1.11.0 ドキュメント

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?