Home > Software engineering >  Dockerfile inheriting ENV from base image with unexpected result
Dockerfile inheriting ENV from base image with unexpected result

Time:10-15

I have three Dockerfiles with two of them providing base images to their siblings as follows:

# Dockerfile.a (for building image-a)
FROM debian:latest
# ...

# Dockerfile.b (for building image-b)
ARG INDEX
FROM image-a
# ...
ENV INDEX=$INDEX
# ...

# Dockerfile.c (for building image-c)
ARG INDEX
FROM image-b
# ...
ENV INDEX=$INDEX
# ...

Builds are done with for ABC in a b c; do podman build -f Dockerfile.$ABC -t $image-$ABC:latest --build-arg INDEX=1 ...; done".

What I currently observe is that in a running instance of image-c $INDEX is empty, i.e. does not equal 1. Am I making an obvious mistake here? Is the repeated use of INDEX both as ARG and ENV and across the chain of inheritance perhaps interfering with the normal semantics of ARG INDEX ... ENV INDEX=$INDEX in Dockerfile.c somehow?

CodePudding user response:

Use ARG before FROM if you need to parametrize your image. In order to use the same build argument later, your have to define the ARG again after FROM:

ARG INDEX
FROM image-b-${INDEX} // just example
ARG INDEX
ENV INDEX=${INDEX}
  • Related