How to set environment variable for my docker image using azure pipeline variable?

Viewed 834

I have a azure pipeline that I want to use to deploy my rails app. The app has a Dockerfile and a docker-compose file.

I am trying to set the RAILS_MASTER_KEY as a secret variable in my pipeline and then reference it as environment variable in my docker compose file.

I can confirm that the agent is setting the variable correctly using echo in the pipeline yaml. However, The environment variable is not getting passed/set properly to my docker-compose and ultimately Dockerfile.

I have troubleshoot this for days reading azure docs, stack overflow and its not working.

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#secret-variables

Here is my code:

azure-pipelines.yaml:

steps:
  - script: echo "test $(RAILS_MASTER_KEY)"
  - script: echo "test $(testvar)"
  - task: DockerCompose@0
    inputs:
      containerregistrytype: 'Azure Container Registry'
      azureSubscription: 'de-identified'
      azureContainerRegistry: '{"loginServer":""de-identified"", "id" : "de-identified"}'
      dockerComposeFile: '**/docker-compose.yml'
      dockerComposeFileArgs: |
        RAILS_MASTER_KEY=$(RAILS_MASTER_KEY)
        testvar=$(testvar)
      action: 'Build services'
    env:
      RAILS_MASTER_KEY: $(RAILS_MASTER_KEY)

docker-compose.yml:

version: "3.3"
services:
  web:
    build: .
    command: command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0' && echo ${RAILS_MASTER_KEY}"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    environment:
      RAILS_MASTER_KEY: ${RAILS_MASTER_KEY}
      testvar: ${testvar}

Dockerfile:

FROM ruby:3.1.0-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    postgresql-client \
    git \
  && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

WORKDIR /app
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock
ENV BUNDLER_VERSION 2.3.5
ENV RAILS_ENV production
ENV RAILS_MASTER_KEY ${RAILS_MASTER_KEY}
ENV testvar ${testvar}
ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true
RUN echo "----------------key is 1. ${RAILS_MASTER_KEY}"
RUN echo "----------------key is 11. $( testvar )"
RUN echo "----------------key is 12. $testvar"
RUN echo "----------------key is 2. $[variables.RAILS_MASTER_KEY]"
RUN echo "----------------key is 4. $(RAILS_MASTER_KEY)"
RUN gem install bundler -v $BUNDLER_VERSION
RUN bundle config set --local without 'development test'
RUN bundle install
COPY . .
RUN rm -f /app/tmp/pids/server.pid
EXPOSE 3000
CMD ["bundle", "exec", "rails", "s", "-e", "production", "-b", "0.0.0.0"]

As you can see, I have echo trying when trying to debug this so please ignore all echo statements. Any help/guide will be appreciated. Thank you.

1 Answers

From the above snippets, it seems you're using the dockerComposeFileArgs to specify environment variables. One option that comes in handy for similar situations (and for local debugging purposes) is to use ARG into Dockerfile. e.g.

FROM ruby:3.1.0-slim

RUN ...

ARG RAILS_MASTER_KEY
ENV RAILS_MASTER_KEY=$RAILS_MASTER_KEY

...

so you'll have the possibility to specify the RAILS_MASTER_KEY value at build-time using secret variables from Azure DevOps

Here you can find some useful posts talking about ENV and ARGS keywords:

Related