1

Both ARGs an ENVs are available to a Dockerfile at build time, but apparently Docker Compose allows only to specify ARGs in service.build.args. ENVs specified in service.environment apparently are not visible at build time (which also makes sense given this path).

So if my build depends on ENVs (as well as ARGs) and if I build with docker-compose build, how can I provide the build-time ENVs inside my docker-compose.yaml?

rookie099
  • 2,201
  • 2
  • 26
  • 52

1 Answers1

1

There's no way to externally pass environment variables into a Dockerfile, whether via docker build or the Compose build: block. You can only specify arguments.

If you really need to specify an environment variable at build time, you can pass it as an argument and then set the environment variable in the Dockerfile

ARG FOO
ENV FOO ${FOO}

You must rebuild the image if you ever change one of these things. That makes this technique not work well for deployment-specific settings like user IDs, host names, etc. It's also okay for container-side port numbers and filesystem paths to be fixed properties of the image.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • I have tried that (as also suggested [here](https://stackoverflow.com/questions/33935807/how-to-define-a-variable-in-a-dockerfile)), but it does not seem to work. What I observe that `ARG FOO` apparently influences the build process (with a non-empty value) as intended, but that `ENV FOO ${FOO}` does not seem to work in the sense that `docker inspect` shows only an empty value for environment variable `$FOO`. Would could I still be doing wrong here? – rookie099 Jul 28 '20 at 13:35
  • So I found the issue. It was a mistake on my part. Dockerfile contains two stages `ARG FOO` was up in 1st stage, `ENV FOO ${FOO}` was down in 2nd stage. Had to add another `ARG FOO` to 2nd stage. – rookie099 Jul 28 '20 at 13:44