Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a variable to a Dockerfile from a docker-compose.yml file

Tags:

docker

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?

like image 392
lukeaus Avatar asked Apr 20 '16 08:04

lukeaus


People also ask

How do you pass variables from Docker Compose to Dockerfile?

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.

Can Dockerfile work with Docker compose?

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.

Can I use ENV variable in Dockerfile?

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.


1 Answers

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.

like image 152
lukeaus Avatar answered Sep 23 '22 00:09

lukeaus