docker-compose build args not passing to Dockerfil

2019-03-15 06:54发布

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.

2条回答
祖国的老花朵
2楼-- · 2019-03-15 07:34

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 a FROM is outside of a build stage, so it can't be used in any instruction after a FROM. - 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 first FROM use an ARG 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
查看更多
贪生不怕死
3楼-- · 2019-03-15 07:36

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

查看更多
登录 后发表回答