I have several docker-compose.yml files that I want to use the same Dockerfile with, with a slight variation. So I want to pass an argument to that Dockerfile so that I can do something slightly different depending on whatever value the variable is set to.
What I have tried so far
docker-compose-A.yml file
version: '2' services: django: build: context: . dockerfile: ./docker/Dockerfile args: - SOMETHING=foo
docker-compose-B.yml file
version: '2' services: django: build: context: . dockerfile: ./docker/Dockerfile args: - SOMETHING=bar
I have a dockerfile I want to use SOMETHING in. e.g.
# Dockerfile RUN echo $SOMETHING
That doesn't work. SOMETHING is not passed to the dockerfile.
Am I doing this incorrectly or is this not the intended usage?
Is there any other way to pass a variable to a Dockerfile from a docker-compose.yml file?
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.
Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.
Basically I missed declaring the arg in the Dockerfile.
# docker-compose.yml file version: '2' services: django: build: context: . dockerfile: ./docker/Dockerfile args: - SOMETHING=foo
# Dockerfile ARG SOMETHING RUN echo $SOMETHING
Shoutout to @Lauri for showing me the light.
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