- How to using env variables in Dockerfile?
- not args
This is my Dockerfile now:
FROM node:12
I want Dockerfile:
FROM node:$version
How can I do it? If it is not possible, then I will create a feature request for Docker.
This is my Dockerfile now:
FROM node:12
I want Dockerfile:
FROM node:$version
How can I do it? If it is not possible, then I will create a feature request for Docker.
You need to add as ARG
s under a different place from the ENV
variables. You can use them in the compose, just a little differently.
ARG
s will be used during the build only, if you need them to available for the build & again while running the container for some reason you will have to set both an ARG
as well as an ENV
variable. {{{During the Build
stage they will be called in the exact same manner}}}. ENV are set to replace defaults when running a container, so the build's ENV TZ=Europe/London
will be replaced by the Compose's Environment: TZ=America/New_York
services:
my_node_version:
image: my/node
build:
context: .
dockerfile: ../node.Dockerfile
args:
- VERSION=12
- NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
environment:
- ONLY_USED_IN_IMAGE=MyImageWorks
- NEEDED_IN_BUILD_AND_RUN=SomeStrangeUseCase
- OVERRIDE_BUILD_ENV="Will replace the ENV from the Build"
I'm pretty sure that fulfills all the intent from the question (you asked to not use args
I assume because you wanted them set in the docker-compose.yaml
rather than the dockerfile). You can do this, you just have to set them in a different way.
ENV
cannot be used in the build from the Compose (ENV
variables set in the Dockerfile just set the defaults that the container will use if none are declared in the OS, the Compose, or anywhere Docker gets them from), so that simply isn't a possibility, so if you need it to be ENV
specifically for some reason then the answer is simply "You can't" end of discussion. But I don't see anything that could possibly accomplish, & no reason using ARGs wouldn't accomplish exactly what you want. You just need to place them in a different section, but they should function as needed