docker-compose.yml:
version: "3"
services:
ei:
build:
context: .
dockerfile: Dockerfile
args:
NODE_VERSION: 8
HELLO: 5
Dockerfile:
ARG NODE_VERSION
ARG HELLO
FROM node:$NODE_VERSION
RUN echo "-> $HELLO"
RUN echo "-> $NODE_VERSION"
Results in:
km@Karls-MBP ~/dev/ve (km/ref) $ docker-compose -f docker-compose.yml build --no-cache
vertica uses an image, skipping
Building ei
Step 1/14 : ARG NODE_VERSION
Step 2/14 : ARG HELLO
Step 3/14 : FROM node:$NODE_VERSION
---> e63de54eee16
Step 4/14 : RUN echo "-> $HELLO"
---> Running in e93d89e15913
->
Removing intermediate container e93d89e15913
---> c305b277291c
Step 5/14 : RUN echo "-> $NODE_VERSION"
---> Running in 39e8e656c0bd
-> 8
I'm scratching my head as to why this isn't working. If I change the node version number the number changes.
The defined arguments on the compose file are available on the Dockerfile but only before and on the
FROM
. After theFROM
the arguments are not available:Why is the argument
NODE_VERSION
working?The argument
NODE_VERSION
isn't working after theFROM
. The argument is only used on theFROM
(FROM node:8
). AfterFROM
there is a environment variable of the image with the same name. So you echo the environment variable of the image instead of the argument of your compose file.But you can use the default value of the argument after
FROM
:To use and show the node version defined in the arguments you need to rename this argument. So you can use the following to show all your arguments and the environment variable of the image:
Dockerfile:
docker-compose.yml:
In case you came here and your syntax and everything was fine, but the variable still wasn't passing through...
It may be the case that you're trying to override a variable that's already set by the parent image (in my case, trying to set
BUNDLE_PATH
which was already being set by theruby
parent image).If this is the case, you can simply rename the argument to something that won't conflict with the parent (ie, instead of
BUNDLE_PATH
, useARG_BUNDLE_PATH
)!See this issue for more details: https://github.com/moby/moby/issues/34494