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.
If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .
Pass variables into Dockerfile through Docker Compose during build. If you want to pass variables through the docker-compose process into any of the Dockerfiles present within docker-compose. yml , use the --build-arg parameter for each argument to flow into all of the Dockerfiles.
A Dockerfile cannot invoke the docker-compose up command or reference a docker-compose. yaml file. This docker-compose. yaml file for the Apache httpd web server describes how to map volumes, configure ports and name the Docker container when it runs.
Docker-compose command doesn't override Dockerfile CMD.
The defined arguments on the compose file are available on the Dockerfile but only before and on the FROM
. After the FROM
the arguments are not available:
An
ARG
declared before aFROM
is outside of a build stage, so it can't be used in any instruction after aFROM
. - from docker docs
Why is the argument NODE_VERSION
working?
The argument NODE_VERSION
isn't working after the FROM
. The argument is only used on the FROM
(FROM node:8
). After FROM
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 the default value of an
ARG
declared before the firstFROM
use anARG
instruction without a value inside of a build stage. - from docker docs
ARG NODE_VERSION FROM node:$NODE_VERSION ARG HELLO RUN echo "-> $HELLO" RUN echo "-> $NODE_VERSION"
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:
ARG CUSTOM_NODE_VERSION FROM node:$CUSTOM_NODE_VERSION ARG CUSTOM_NODE_VERSION ARG HELLO RUN echo "-> $HELLO" #output: 5 RUN echo "-> $NODE_VERSION" #output: 8.9.4 RUN echo "-> $CUSTOM_NODE_VERSION" #output: 8
docker-compose.yml:
version: "3" services: ei: build: context: . dockerfile: Dockerfile args: CUSTOM_NODE_VERSION: 8 HELLO: 5
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 the ruby
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
, use ARG_BUNDLE_PATH
)!
ARG ARG_BUNDLE_PATH ENV BUNDLE_PATH=$ARG_BUNDLE_PATH
See this issue for more details: https://github.com/moby/moby/issues/34494
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With