50

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?

3

1 Answer 1

101

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.