lukeaus
lukeaus

Reputation: 12265

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

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?

Upvotes: 51

Views: 27830

Answers (1)

lukeaus
lukeaus

Reputation: 12265

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.

Upvotes: 102

Related Questions