Search code examples
dockerdocker-composedocker-multi-stage-build

Docker compose build args are not passed to dockerfile


I have a docker compose file that builds and runs a couple of dockerfiles locally. I am using multi-stage builds and I pass an argument into my docker build commands. The following works perfectly:

docker build -t local/admin-api --build-arg SONAR_TOKEN=XXXXXXXXXXXXXXX .

I am trying to replicate that in my docker compose file with:

services:
  comply:
    build:
      context: ./websites/comply/
      dockerfile: Dockerfile
      args:
        SONAR_TOKEN: xxxxxxxxxxx
    ports:
      - "8080:80"

In my dockerfile I use:

FROM kinectify.azurecr.io/buildbase:ci-631 as build

ARG PAT
ARG SONAR_PROJECT_KEY=xxxxxxx
ARG SONAR_ORGANIZATION_KEY=xxxxxxx
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_TOKEN

RUN echo "Token: ${SONAR_TOKEN} | hostURL: ${SONAR_HOST_URL}"

I am aware of the scoping of build args, and as you can see above, the arg is defined after my FROM statement. I have also tried setting the arg to an environment variable, but that doesn't seem to matter. Additionally, the SONAR_HOST_URL is being output correctly in echo statement (and in the place where I am actually using the argument). I am running the build with:

docker compose build comply

** UPDATE **

Strangely, when I run docker compose up -d --build it does pass variable. I am not sure what the difference is though.


Solution

  • This is a known issue that's been fixed in docker compose. You are likely waiting for the next release still. Using docker-compose instead of docker compose should also work while you wait for that release.