How to use args :, environment :, env_file: and .env files with docker-compose command

How to use args :, environment :, env_file: and .env files with docker-compose command

In docker-compose v3, I sometimes got lost due to the usage of args:, environment:, env_file: and the .env file, so I organized it for myself.

Summary

About args:

--It's a build variable ――So, it doesn't exist at the time of run --If you declare the same variable, it's docker-compose.yml> Dockerfile --Declaring it in docker-compose.yml is equivalent to declaring it in Dockerfile.

About environment:

--You can set environment variables --Variables declared in docker-compose.yml will be added / overwritten at run. --Note that what you declare with the ENV instruction of Dockerfile can be used like a variable of the ARG instruction at the time of build. --And at the time of build, you need to be careful because the variable declared in docker-compose.yml is not passed as mentioned above.

About env_file:

--It's a mechanism to add environment variables from a file --If an environment variable with the same name exists, the priority is environment:> env_file: --If you specify multiple env_file:, the latter value in the list will be added / overwritten.

About .env

--The special file used by docker-compose is .env. --The file that makes the variable replacement function convenient is .env. -(Probably) You can't use the file name .env as another name. --Variables declared and defined in .env can be used as variables on docker-compose.yml.

How to use args:

First, the official documentation.

https://docs.docker.com/compose/compose-file/#args

Add build arguments, which are environment variables accessible only during the build process.

Free translation: You can set variables for the build.

A mechanism that allows you to set build variables.

ARG_VALUE1 --Declare and set values up to ARG_VALUE4 as follows, and check the behavior.

Dockerfile docker-compose.yml
ARG_VALUE1 arg1_in_Dockerfile
ARG_VALUE2 arg2_in_Dockerfile arg2_in_yml
ARG_VALUE3 arg3_in_yml
ARG_VALUE4

./args/Dockerfile

./args/Dockerfile


FROM busybox

ARG ARG_VALUE1="arg1_in_Dockerfile."
ARG ARG_VALUE2="arg2_in_Dockerfile."

RUN echo "ARG_VALUE1 is ${ARG_VALUE1}" \
  && echo "ARG_VALUE2 is ${ARG_VALUE2}" \
  && echo "ARG_VALUE3 is ${ARG_VALUE3}" \
  && echo "ARG_VALUE4 is ${ARG_VALUE4}"
RUN echo "ARG_VALUE1 is $ARG_VALUE1" >> /tmp/outs.txt \
  && echo "ARG_VALUE2 is $ARG_VALUE2" >> /tmp/outs.txt \
  && echo "ARG_VALUE3 is $ARG_VALUE3" >> /tmp/outs.txt \
  && echo "ARG_VALUE4 is $ARG_VALUE4" >> /tmp/outs.txt

CMD cat /tmp/outs.txt \
  && echo "-----" \
  && echo "ARG_VALUE1 is ${ARG_VALUE1}" \
  && echo "ARG_VALUE2 is ${ARG_VALUE2}" \
  && echo "ARG_VALUE3 is ${ARG_VALUE3}" \
  && echo "ARG_VALUE4 is ${ARG_VALUE4}"

./args/docker-compose.yml

./args/docker-compose.yml


version: '3'
services:
  with-args:
    build:
      context: ./
      args:
        ARG_VALUE2: "arg2_in_yml"
        ARG_VALUE3: "arg3_in_yml"

Run.

% docker-compose build --no-cache && docker-compose up
...Abbreviation...
Step 4/6 : RUN echo "ARG_VALUE1 is ${ARG_VALUE1}"   && echo "ARG_VALUE2 is ${ARG_VALUE2}"   && echo "ARG_VALUE3 is ${ARG_VALUE3}"   && echo "ARG_VALUE4 is ${ARG_VALUE4}"
 ---> Running in 64893f52d5bc
ARG_VALUE1 is arg1_in_Dockerfile.
ARG_VALUE2 is arg2_in_yml
ARG_VALUE3 is
ARG_VALUE4 is
Removing intermediate container 64893f52d5bc
 ---> a66e7626d5eb
...Abbreviation...
[Warning] One or more build-args [ARG_VALUE3] were not consumed
...Abbreviation...
with-args_1  | ARG_VALUE1 is arg1_in_Dockerfile.
with-args_1  | ARG_VALUE2 is arg2_in_yml
with-args_1  | ARG_VALUE3 is
with-args_1  | ARG_VALUE4 is
with-args_1  | -----
with-args_1  | ARG_VALUE1 is
with-args_1  | ARG_VALUE2 is
with-args_1  | ARG_VALUE3 is
with-args_1  | ARG_VALUE4 is
args_with-args_1 exited with code 0

result.

Dockerfile docker-compose.yml At build time When run
ARG_VALUE1 arg1_in_Dockerfile arg1_in_Dockerfile
ARG_VALUE2 arg2_in_Dockerfile arg2_in_yml arg2_in_yml
ARG_VALUE3 arg3_in_yml [Warning]
ARG_VALUE4

--The value is overwritten by args: in docker-compose.yml --Warning if there are unused variables in args: declared in docker-compose.yml --There is no warning for variables that are used in Dockerfile but not declared in docker-compose.yml. ――Of course, it is not available at the time of run

How to use environment:

First, the official documentation.

https://docs.docker.com/compose/compose-file/#environment

Add environment variables.

A mechanism that allows you to add environment variables.

Unlike the build variable mentioned above, this is an instruction to set the so-calledenvironment variable.

ENV_VALUE1 --Declare and set values up to ENV_VALUE4 as follows, and check the behavior.

Dockerfile docker-compose.yml
ENV_VALUE1 env1_in_Dockerfile
ENV_VALUE2 env2_in_Dockerfile env2_in_yml
ENV_VALUE3 env3_in_yml
ENV_VALUE4

./environment/Dockerfile

./environment/Dockerfile*


FROM busybox

ENV ENV_VALUE1="env1_in_Dockerfile."
ENV ENV_VALUE2="env2_in_Dockerfile."

RUN echo "ENV_VALUE1 is ${ENV_VALUE1}" \
  && echo "ENV_VALUE2 is ${ENV_VALUE2}" \
  && echo "ENV_VALUE3 is ${ENV_VALUE3}" \
  && echo "ENV_VALUE4 is ${ENV_VALUE4}" \
RUN echo "ENV_VALUE1 is $ENV_VALUE1" >> /tmp/outs.txt \
  && echo "ENV_VALUE2 is $ENV_VALUE2" >> /tmp/outs.txt \
  && echo "ENV_VALUE3 is $ENV_VALUE3" >> /tmp/outs.txt \
  && echo "ENV_VALUE4 is $ENV_VALUE4" >> /tmp/outs.txt

CMD cat /tmp/outs.txt \
  && echo "-----" \
  && echo "ENV_VALUE1 is ${ENV_VALUE1}" \
  && echo "ENV_VALUE2 is ${ENV_VALUE2}" \
  && echo "ENV_VALUE3 is ${ENV_VALUE3}" \
  && echo "ENV_VALUE4 is ${ENV_VALUE4}"

./environment/docker-compose.yml

./environment/docker-compose.yml


version: '3'
services:
  with-environment:
    environment:
      ENV_VALUE2: "env2_in_yml"
      ENV_VALUE3: "env3_in_yml"

Run.

% docker-compose build --no-cache && docker-compose up
...Abbreviation...
Step 4/6 : RUN echo "ENV_VALUE1 is ${ENV_VALUE1}"   && echo "ENV_VALUE2 is ${ENV_VALUE2}"   && echo "ENV_VALUE3 is ${ENV_VALUE3}"   && echo "ENV_VALUE4 is ${ENV_VALUE4}"
 ---> Running in bb4ae383c1e7
ENV_VALUE1 is env1_in_Dockerfile.
ENV_VALUE2 is env2_in_Dockerfile.
ENV_VALUE3 is
ENV_VALUE4 is
Removing intermediate container bb4ae383c1e7
 ---> a01b51cd008a
...Abbreviation...
with-environment_1  | ENV_VALUE1 is env1_in_Dockerfile.
with-environment_1  | ENV_VALUE2 is env2_in_Dockerfile.
with-environment_1  | ENV_VALUE3 is
with-environment_1  | ENV_VALUE4 is
with-environment_1  | -----
with-environment_1  | ENV_VALUE1 is env1_in_Dockerfile.
with-environment_1  | ENV_VALUE2 is env2_in_yml
with-environment_1  | ENV_VALUE3 is env3_in_yml
with-environment_1  | ENV_VALUE4 is
environment_with-environment_1 exited with code 0

result.

Dockerfile docker-compose.yml At build time When run
ENV_VALUE1 env1_in_Dockerfile env1_in_Dockerfile env1_in_Dockerfile
ENV_VALUE2 env2_in_Dockerfile env2_in_yml env2_in_Dockerfile env2_in_yml
ENV_VALUE3 env3_in_yml env3_in_yml
ENV_VALUE4

--At the time of build, the value described in Dockerfile is used. --At run, the value set in docker-compose.yml is passed and overwritten. --ENV instruction and environment: To the last, be aware that it sets environment variables. --At the time of build, the value prepared by the ENV instruction can be used as a build variable, so be careful.

How to use env_file:

First, the official documentation.

https://docs.docker.com/compose/compose-file/#env_file

Add environment variables from a file.

A mechanism to add so-called environment variables from a" file ".

ENV_VALUE1 --Declare and set values up to ENV_VALUE5 as follows, and check the behavior.

Dockerfile docker-compose.yml some_env.env
ENV_VALUE1 env1_in_Dockerfile
ENV_VALUE2 env2_in_Dockerfile env2_in_yml
ENV_VALUE3 env3_in_yml env3_in_file
ENV_VALUE4 env4_in_file
ENV_VALUE5

./env_file/Dockerfile

./env_file/Dockerfile


FROM busybox

ENV ENV_VALUE1="env1_in_Dockerfile."
ENV ENV_VALUE2="env2_in_Dockerfile."

RUN echo "ENV_VALUE1 is ${ENV_VALUE1}" \
  && echo "ENV_VALUE2 is ${ENV_VALUE2}" \
  && echo "ENV_VALUE3 is ${ENV_VALUE3}" \
  && echo "ENV_VALUE4 is ${ENV_VALUE4}" \
  && echo "ENV_VALUE5 is ${ENV_VALUE5}"
RUN echo "ENV_VALUE1 is $ENV_VALUE1" >> /tmp/outs.txt \
  && echo "ENV_VALUE2 is $ENV_VALUE2" >> /tmp/outs.txt \
  && echo "ENV_VALUE3 is $ENV_VALUE3" >> /tmp/outs.txt \
  && echo "ENV_VALUE4 is $ENV_VALUE4" >> /tmp/outs.txt \
  && echo "ENV_VALUE5 is $ENV_VALUE5" >> /tmp/outs.txt

CMD cat /tmp/outs.txt \
  && echo "-----" \
  && echo "ENV_VALUE1 is ${ENV_VALUE1}" \
  && echo "ENV_VALUE2 is ${ENV_VALUE2}" \
  && echo "ENV_VALUE3 is ${ENV_VALUE3}" \
  && echo "ENV_VALUE4 is ${ENV_VALUE4}" \
  && echo "ENV_VALUE5 is ${ENV_VALUE5}"

./env_file/docker-compose.yml

./env_file/docker-compose.yml


version: '3'
services:
  with-env_file:
    build:
      context: ./
    environment:
      ENV_VALUE2: "env2_in_yml"
      ENV_VALUE3: "env3_in_yml"
    env_file:
      - some_env.env

./env_file/some_env.env

./env_file/some_env.env


ENV_VALUE3="env3_in_file"
ENV_VALUE4="env4_in_file"

Run.

% docker-compose build --no-cache && docker-compose up
...Abbreviation...
Step 4/6 : RUN echo "ENV_VALUE1 is ${ENV_VALUE1}"   && echo "ENV_VALUE2 is ${ENV_VALUE2}"   && echo "ENV_VALUE3 is ${ENV_VALUE3}"   && echo "ENV_VALUE4 is ${ENV_VALUE4}"   && echo "ENV_VALUE5 is ${ENV_VALUE5}"
 ---> Running in 5851a9b3aa91
ENV_VALUE1 is env1_in_Dockerfile.
ENV_VALUE2 is env2_in_Dockerfile.
ENV_VALUE3 is
ENV_VALUE4 is
ENV_VALUE5 is
Removing intermediate container 5851a9b3aa91
 ---> 39f56354d7cd
...Abbreviation...
with-env_file_1  | ENV_VALUE1 is env1_in_Dockerfile.
with-env_file_1  | ENV_VALUE2 is env2_in_Dockerfile.
with-env_file_1  | ENV_VALUE3 is
with-env_file_1  | ENV_VALUE4 is
with-env_file_1  | ENV_VALUE5 is
with-env_file_1  | -----
with-env_file_1  | ENV_VALUE1 is env1_in_Dockerfile.
with-env_file_1  | ENV_VALUE2 is env2_in_yml
with-env_file_1  | ENV_VALUE3 is env3_in_yml
with-env_file_1  | ENV_VALUE4 is env4_in_file
with-env_file_1  | ENV_VALUE5 is
env_file_with-env_file_1 exited with code 0

result.

Dockerfile docker-compose.yml some_env.env At build time When run
ENV_VALUE1 env1_in_Dockerfile env1_in_Dockerfile env1_in_Dockerfile
ENV_VALUE2 env2_in_Dockerfile env2_in_yml env2_in_Dockerfile env2_in_yml
ENV_VALUE3 env3_in_yml env3_in_file env3_in_yml
ENV_VALUE4 env4_in_file env4_in_file
ENV_VALUE5

--The value of environment: takes precedence over the value specified by env_file: --If specified in env_file: and not set in docker-compose.yml, the value specified inenv_file:will be used. --Although I have not experimented, as described in the document, if you specify multiple files in env_file:, it will be added / overwritten by the latter value in the list.

How to use the .env file

The .env file is a specially treated file in docker-compose.

Used in the variable replacement function.

Variable replacement function

The .env file is used by the variable replacement function of docker-compose, so first try the variable replacement function.

Official documentation. https://docs.docker.com/compose/compose-file/#variable-substitution

Your configuration options can contain environment variables. Compose uses the variable values from the shell environment in which docker-compose is run.

Free translation: You can use the environment variables of the shell that executes docker-compose as variables of docker-compose.

Prepare ./variable-substitution/docker-compose.yml with the following contents.

./variable-substitution/docker-compose.yml


version: '3'
services:
  variable-substitution:
    image: busybox:${BUSYBOX_VERSION}

Temporarily set the environment variable BUSYBOX_VERSION = latest and docker-compose up. (Since there is no need for build, it will be directly up.)

% env BUSYBOX_VERSION="latest" docker-compose up
...Abbreviation...
variable-substitution_variable-substitution_1 exited with code 0

I pulled it up safely and finished.

Similarly, set BUSYBOX_VERSION = musl to an environment variable and do docker-compose up.

% env BUSYBOX_VERSION="musl" docker-compose up
...Abbreviation...
variable-substitution_variable-substitution_1 exited with code 0

This was also successfully pulled up and finished.

For confirmation, check the image list at hand with docker images.

% docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             musl                8bce8d24824c        5 weeks ago         1.47MB
busybox             latest              f0b02e9d092d        5 weeks ago         1.23MB

Certainly, I confirmed that there is an image with latest, musl TAG.

In this way, docker-compose has a mechanism to pass the environment variables of the environment where the shell is executed to docker-compose.

This is one of the variable replacement functions.

.env file

The official documentation says:

https://docs.docker.com/compose/compose-file/#variable-substitution

You can set default values for environment variables using a .env file, which Compose automatically looks for. Values set in the shell environment override those set in the .env file.

Free translation: docker-compose sets the default environment variables when it finds the .env file. Also, the values set from the shell's environment variables will be overwritten with the values set in the .env file.

Prepare the ./variable-substitution-dotenv/.env file as shown below.

./variable-substitution-dotenv/.env


BUSYBOX_VERSION=latest

Same content as before, but prepare ./variable-substitution-dotenv/docker-compose.yml.

./variable-substitution-dotenv/docker-compose.yml


version: '3'
services:
  variable-substitution-dotenv:
    image: busybox:${BUSYBOX_VERSION}

In this state, try up without setting a temporary environment variable.

% docker-compose up
Creating network "variable-substitution-dotenv_default" with the default driver
Pulling variable-substitution-dotenv (busybox:latest)...
latest: Pulling from library/busybox
9758c28807f2: Pull complete
Digest: sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Status: Downloaded newer image for busybox:latest
Creating variable-substitution-dotenv_variable-substitution-dotenv_1 ... done
Attaching to variable-substitution-dotenv_variable-substitution-dotenv_1

You can see that latest was successfully pulled up.

Next, rewrite ./variable-substitution-dotenv/.env with the following contents.

./variable-substitution-dotev/.env


BUSYBOX_VERSION=musl

In this state, try docker-compose up again.

% docker-compose up
Pulling variable-substitution-dotenv (busybox:musl)...
musl: Pulling from library/busybox
7c3804618ebb: Pull complete
Digest: sha256:605de95bca536139f324abdecf88dcab492c8457346e7fc92e37dff6e263f341
Status: Downloaded newer image for busybox:musl
Recreating variable-substitution-dotenv_variable-substitution-dotenv_1 ... done
Attaching to variable-substitution-dotenv_variable-substitution-dotenv_1
variable-substitution-dotenv_variable-substitution-dotenv_1 exited with code 0

You can see that musl was successfully pulled and up.

In this way, using the .env file makes it possible to pass variables to docker-compose without polluting the environment variables of the shell execution environment.

By the way, the official document has the following notes.

Note when using docker stack deploy The .env file feature only works when you use the docker-compose up command and does not work with docker stack deploy.

Free translation

Note: For those who use docker stack deploy This function using the .env file works only with the docker-compose up command. It doesn't work with docker stack deploy.

Since it is a note when using docker stack deploy, I think that it is written as only works when you use the docker-compose up command, but in reality it is not only the docker-compose up command, but docker-compose up `Work for commands.

Specifically, it also works with the docker-compose build and docker-compose config commands.

As a test, if you execute docker-compose config in the current state, it will be as follows. (docker-compose config is a command to check the final yml to be executed.)

% docker-compose config
services:
  variable-substitution:
    image: busybox:musl
version: '3'

You can see that the $ {BUSYBOX_VERSION} part is interpolated to busybox: musl.

Then, rewrite ./variable-substitution-dotenv/.env to the following contents and execute docker-compose config to get the following.

./variable-substitution-dotenv/.env


BUSYBOX_VERSION=latest

% docker-compose config
services:
  variable-substitution:
    image: busybox:latest
version: '3'

Similarly, you can see that the $ {BUSYBOX_VERSION} part is interpolated to busybox: latest.

It was confirmed that variables can be passed to docker-compose by using the .env file.

Variable replacement function using .env file makes it easy to misunderstand and how to deal with it

Of course, the following .env, Dockerfile and docker-compose.yml configurations cannot be build.

./variable-substitution-dotenv-dockerfile-not-work/.env

./variable-substitution-dotenv-dockerfile-not-work/.env


BUSYBOX_VERSION="latest"

./variable-substitution-dotenv-dockerfile-not-work/Dockerfile

./variable-substitution-dotenv-dockerfile-not-work/Dockerfile


FROM busybox:${BUSYBOX_VERSION}

./variable-substitution-dotenv-dockerfile-not-work/docker-compose.yml

./variable-substitution-dotenv-dockerfile-not-work/docker-compose.yml


version: '3'
services:
  variable-substitution-dotenv-dockerfile-not-work:
    build:
      context: ./

Run.

% docker-compose build --no-cache
Building variable-substitution-dotenv-dockerfile-not-work
Step 1/1 : FROM busybox:${BUSYBOX_VERSION}
ERROR: Service 'variable-substitution-dotenv-dockerfile-not-work' failed to build : invalid reference format

It is natural if you read the operation check and the official document so far, but it is in the following state.

--The BUSYBOX_VERSION variable inbusybox: $ {BUSYBOX_VERSION}used at the beginning of Dockerfile is not declared as a Dockerfile as abuild variable. --The mechanism for passing variables to docker-compose using the .env file, of course, only passes values up to docker-compose. --In other words, the variable has not passed to Dockerfile

To be able to build, you need to have the following configuration.

./variable-substitution-dotenv-dockerfile-work/.env

./variable-substitution-dotenv-dockerfile-work/.env


BUSYBOX_VERSION="latest"

./variable-substitution-dotenv-dockerfile-work/Dockerfile

./variable-substitution-dotenv-dockerfile-work/Dockerfile


ARG BUSYBOX_VERSION
FROM busybox:${BUSYBOX_VERSION}

./variable-substitution-dotenv-dockerfile-work/docker-compose.yml

./variable-substitution-dotenv-dockerfile-work/docker-compose.yml


version: '3'
services:
  variable-substitution-dotenv-dockerfile-work:
    build:
      context: ./
      args:
        BUSYBOX_VERSION: ${BUSYBOX_VERSION}

Run.

% docker-compose build --no-cache
Building variable-substitution-dotenv-dockerfile-work
Step 1/2 : ARG BUSYBOX_VERSION
Step 2/2 : FROM busybox:${BUSYBOX_VERSION}
latest: Pulling from library/busybox
9758c28807f2: Pull complete
Digest: sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Status: Downloaded newer image for busybox:latest
 ---> f0b02e9d092d

Successfully built f0b02e9d092d
Successfully tagged variable-substitution-dotenv-dockerfile-work_variable-substitution-dotenv-dockerfile-work:latest

--Declare the BUSYBOX_VERSION variable using the ARG instruction in Dockerfile --Declare the BUSYBOX_VERSION variable in theargs:clause of docker-compose.yml --Also, in the args: clause of docker-compose.yml, assign the BUSYBOX_VERSION variable of .env to the BUSYBOX_VERSION variable declaration. --Set the value of the BUSYBOX_VERSION variable in the .env file

Now, by changing the value of BUSYBOX_VERSION defined in .env, you can change the value without polluting the environment variables of the shell execution environment.

However, in the above example, the variable name BUSYBOX_VERSION is used in various places (.env, Dockerfile and docker-compose.yml), which is a little difficult to understand.

To put it a little more clearly and explicitly, .env, Dockerfile and docker-compose.yml are:

./variable-substitution-dotenv-dockerfile-work-easy-to-read/.env

./variable-substitution-dotenv-dockerfile-work-easy-to-read/.env


DOCKER_COMPOSER_BUSYBOX_VERSION="latest"

./variable-substitution-dotenv-dockerfile-work-easy-to-read/Dockerfile

./variable-substitution-dotenv-dockerfile-work-easy-to-read/Dockerfile


ARG BUSYBOX_VERSION="latest"
FROM busybox:${BUSYBOX_VERSION}

./variable-substitution-dotenv-dockerfile-work-easy-to-read/docker-compose.yml

./variable-substitution-dotenv-dockerfile-work-easy-to-read/docker-compose.yml


version: '3'
services:
  variable-substitution-dotenv-dockerfile-work-easy-to-read:
    build:
      context: ./
      args:
        BUSYBOX_VERSION: ${DOCKER_COMPOSER_BUSYBOX_VERSION}

--Declare the BUSYBOX_VERSION variable with the ARG instruction in Dockerfile, and declare to use latest as the default value if no value is passed from docker-compose. --Declare the BUSYBOX_VERSION variable in theargs:clause of docker-compose.yml --Also, in the args: clause of docker-compose.yml, assign the DOCKER_COMPOSER_BUSYBOX_VERSION variable of .env to the BUSYBOX_VERSION variable declaration. --Set the value of the DOCKER_COMPOSER_BUSYBOX_VERSION variable in the .env file

The words "variable" and "environment variable" are often confusing, so if you look at the specific variable names, you can see what is inherited as variables and values.

Just because the name (label) is confusing, you can see the mechanism of passing variable names and values as .env-> docker-compose.yml-> Dockerfile.

Regarding the variable replacement function using the .env file (I doubt if this expression is correct ...) ** The variable replacement function using the .env file is a variable in docker-compose.yml. It may be easier to understand if you think of it as a mechanism for passing values **.

A little digression

In fact, I don't recommend the following writing style as illustrated above.

ARG BUSYBOX_VERSION="latest"
FROM busybox:${BUSYBOX_VERSION}

For example, due to the restrictions of github actions (etc.), the following cases still remain.

The first instruction in the Dockerfile file must be FROM.

https://docs.github.com/ja/free-pro-team@latest/actions/creating-actions/dockerfile-support-for-github-actions#from

In the first place, considering the uniformity of the (development | execution) environment, it is desirable to use a reliable value for the TAG value of FROM without even using "latest".

Of course, there may be cases where this is not the case, but there are problems such as unwilling major version upgrades depending on the build timing.

Usage example

For example, when you do docker-compose build, you can control the execution ofyarn run {script}by local, staging, production, etc. environment.

Suppose you have separate build environments for local, staging, and production, and you also have a .env file for each.

.env


#...Abbreviation...
DOCKER_COMPOSE_BUILD_TYPE="dev"
#...Abbreviation...

.env


#...Abbreviation...
DOCKER_COMPOSE_BUILD_TYPE="dev"
#...Abbreviation...

.env


#...Abbreviation...
DOCKER_COMPOSE_BUILD_TYPE="prod"
#...Abbreviation...

Also assume that these .envs are gitignore and are not maintained in the repository.

In this state, by preparing Dockerfile and docker-compose.yml as follows, it is possible to control each environment while maintaining a single source code.

Dockerfile

#...Abbreviation...
ARG BUILD_TYPE="local"
#...Abbreviation...
RUN yarn run ${BUILD_TYPE}
#...Abbreviation...

docker-compose.yml

docker-compose.yml


#...Abbreviation...
    build:
#...Abbreviation...
      args:
        BUILD_TYPE: ${DOCKER_COMPOSE_BUILD_TYPE}
#...Abbreviation...

In addition, you can use environment: to change the log destination, mail driver, DB driver, etc. for each environment.

If you are developing using php, you can set xdebug enabled and disabled, and you can freely change various setting values via .env of each individual.

Recommended Posts

How to use args :, environment :, env_file: and .env files with docker-compose command
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use String [] args
How to use StringBurrer and Arrays.toString.
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
How to use mssql-tools with alpine
[Docker-compose] How to use unnamed and named volumes of volumes. Bind mount
How to use MinIO with the same function as S3 Use docker-compose
How to share files with Docker Toolbox
How to use binding.pry for view files
How to use OrientJS and OrientDB together
Build and manage RStudio environment with Docker-compose
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
[Java] How to output and write files!
How to set up and use kapt
How to use environment variables in RubyOnRails
How to monitor nginx with docker-compose with datadog
How to use substring and substr methods
How to write and explain Dockerfile, docker-compose
How to use @Builder and @NoArgsConstructor together
How to build Rails 6 environment with Docker
How to deploy to AWS using NUXTJS official S3 and CloudFront? With docker-compose
[Swift] How to connect TabBar with Storyboard Reference and also use NavigationController
[Node.js express Docker] How to define Docker environment variables and load them with node.js
[Java] How to use FileReader class and BufferedReader class
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
[Ruby] How to use gsub method and sub method
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
How to use scope and pass processing (Jakarta)
How to build API with GraphQL and Rails
How to use nfs protocol version 2 with ubuntu 18.04
How to use docker compose with NVIDIA Jetson
How to get resource files out with spring-boot
How to use nginx-ingress-controller with Docker for Mac
[Rails] How to build an environment with Docker
How to build parquet-tools and merge Parquet files
How to use Eclipse on my PC with 32bit and 2GB of memory
[Rails] How to use PostgreSQL in Vagrant environment
[Java] How to use Calendar class and Date class
Use the --build-arg option of docker-compose to pass environment variables to the container built with Dockerfile
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Part 1] How to deploy Docker containers and static files with CircleCI + ECS + ECR + CloudFront
How to use Oracle JDK 9 EA with Travis CI
How to use Z3 library in Scala with Eclipse
I started MySQL 5.7 with docker-compose and tried to connect
[Docker environment] How to deal with ActiveSupport :: MessageEncryptor :: InvalidMessage
How to serialize and deserialize LocalDateTime type with GSON
How to use JDD library in Scala with Eclipse
How to build Java development environment with VS Code
How to handle TSV files and CSV files in Ruby
How to boot by environment with Spring Boot of Maven
How to install Gradle and Kotlin with SDKMAN (Mac)
Common problems with WSL and how to deal with them
How to use and apply Java's JFrame / Canvas class
How to use Map
How to use rbenv