Dockerfile. ARG and ENV statement differences

1 minute read

The ARG and ENV statements use for define variables in to Dockerfile. It may be difficult to understand the difference. The difference is when variables will be utilized.

If you need build-time customization, ARG is best choice. From docker reference:

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the –build-arg = flag.

If you need run-time customization (to run the same image with different settings), ENV is well-suited. What you should know from docs:

The ENV instruction sets the environment variable to the value . The environment variables set using ENV will persist when a container is run from the resulting image.

To define them as the command line instructions use next (for ARG var or ):

ARG (for ARG var): docker build --build-arg var=xxx

ENV (for ENV var): docker run --env var=yyy

The best way define them is write a docker-compose file with defined next section:

version: "3"
      context: .
        var: xxx
      var: yyy

To run container with build image use next command

docker-compose up --build


I hope this explanation has helped you to learn a bit more about using dockerfile statements, and will save you time to understand the basis of Docker.