Categories
Azure Deployment Github actions

Handle argumented Docker contrainer registry paths

In our a recent setup I’ve created, we are using Azure Container Registry to hold a base image, which we use when building our main project.

I use a docker-compose.yml and Dockerfile in our setup and github actions for deployment.

The base image is deployed to two container registries, because of access rights on the different environments.

Basically we have a develop and a production environment.

The develop environment also has a staging setup.

When using our base image, we need to have this in both the develop and production container registries. This means, that our Dockerfile needs to have two different urls to the base image, since they cannot access the other environments container registry.

Usually the develop environment has the following setup:

# Base path for image
FROM uniquedevrepo.azurecr.io/projectbase:php7.4

# other docker stuff here...

# expose the webserver
EXPOSE 8080 443

And the production environment had the following setup:

# Base path for image
FROM uniqueprodrepo.azurecr.io/projectbase:php7.4

# other docker stuff here...

# expose the webserver
EXPOSE 8080 443

This meant, that when we merged code from development into production, we’d get an access error to the base image, if forget to change the url.

This caused a lot of confusion, when deploying, if the developers were not used to our setup.

To handle this, I changed our workfile to pass an argument with the base image container registry url, so Dockerfile will not be changed all the time.

name: Build and deploy Develop

on:
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

  workflow_dispatch:


jobs:
  build:
    name: build and deploy develop
    runs-on: ubuntu-latest
    environment: develop

    steps:
      # azure login steps here...

      - name: 'Build and push image'
        uses: azure/docker-login@v1
      - run: |
          docker compose build --build-arg CRPATH=uniquedevrepo.azurecr.io
          # other actions to run here... :)

Notice the “–build-arg CRPATH=uniquedevrepo.azurecr.io” which is passed to Dockerfile. (skipping docker-compose.yml). We can then simply use the ARG command to get the argument and use it:

# tell Dockerfile that we are going to use the passed argument CRPATH
ARG CRPATH

# Base path for image - using the CRPATH argument.
FROM ${CRPATH}/projectbase:php7.4

# other docker stuff here...

# expose the webserver
EXPOSE 8080 443

This means that I only have to maintain my github actions workflow for each environment and no longer worry about the Dockerfile having the wrong container repositroy path for our base image.

Leave a Reply

Your email address will not be published. Required fields are marked *