Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in Dockerfile via Azure DevOps

In my projects Docker file I have some environment variables, like this:

ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=Password
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433 

And I would like to pass the password here as an environment variable set in my pipeline.

In Azure DevOps I have two pipelines. One for building the solution and one for building and pushing docker images to DockerHub. There are options to set variables in both these pipelines: enter image description here enter image description here I have set the password in both pipelines and edited my password in the Dockerfile to look like this:

ENV SA_PASSWORD=$(SA_PASSWORD)

But that does not seem to be working. What is the correct way of passing environment variables from Azure DevOps into a Docker image?

Also, is this a safe way of passing secrets? Is there any way someone could read secrets from a Docker image?

Thanks!

like image 729
PalBo Avatar asked Jan 27 '20 14:01

PalBo


2 Answers

You can set an ARG var_name and reference ENV to the ARG variables. Then you can replace those variables when docker build the image $ docker build --build-arg var_name=$(VARIABLE_NAME)

For example the add ARG in dockerfile, and have the ENV variable refer to it:

ARG SECRET
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=$SECRET
ENV MSSQL_PID=Developer
ENV MSSQL_TCP_PORT=1433 

You can use dock build task and dock push task separately, as buildandpush command cannot accept arguments. And set a variable SECRET in your pipeline.

enter image description here

The set the Build Arguments SECRET= $(SECRET) to replace the ARG SECRET

enter image description here

You can also refer to a similar thread.

like image 122
Levi Lu-MSFT Avatar answered Oct 17 '22 23:10

Levi Lu-MSFT


I am using the Replace Tokens extension for exactly tasks like this: https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens

However, putting secrets into your Dockerfile might not be the best idea. Usually you would provide secrets or generally runtime configuration as environment variables when you actually execute the container.

like image 3
silent Avatar answered Oct 17 '22 22:10

silent