acr icon indicating copy to clipboard operation
acr copied to clipboard

Option to pass environment Variables in acr run

Open LMSSonos opened this issue 5 years ago • 13 comments

I try to use az acr run --registry myregistry --platform "windows" --set myvar=123 --cmd "myregistry.azurerc.io/test:latest" /dev/null - in the test container I output all environment variables, but I can't see the myvar.

If I use a yaml to run the task and set the env it works as expected. Is there any way to pass the variables via command line?

LMSSonos avatar Apr 27 '20 19:04 LMSSonos

@LMSSonos we recommend to use yaml file to achieve this. With the yaml file, you can still run the similar command like

az acr run --registry myregistry --platform "windows" --set myvar=123 -f myatask.yaml /dev/null

@SteveLasker

northtyphoon avatar Apr 27 '20 20:04 northtyphoon

Hi @LMSSonos, There's a balance of what can be achieved with the command line, vs. what is supported in .yaml files. We do not have a goal of consistency here, so it's just a matter of where we drew the line. Env variables was an example of this. Can you help us understand the bigger picture for how you're using tasks to help us understand the context of the environment variable? Is this coming from a CI/CD system, or a quick task executed by a developer for remote builds?

SteveLasker avatar Apr 27 '20 20:04 SteveLasker

@northtyphoon thanks for the hint. I wanted to use without yaml file

@SteveLasker the thing is that we wanted to use the service to run scheduled jobs or onetimers through the API. we are packing tools like a data import or a email sender tool into a docker container and want to schedule them this way (or execute single times), so we get rid of server infrastructure.

LMSSonos avatar Apr 28 '20 15:04 LMSSonos

@SteveLasker, I have a similar problem. I have .Net core containers in a registry and working just fine in ACI. Some of these I'd like to put on a simple schedule, and it looks like acr task might do that, but I need to be able to send environment variables. The moment I add a .yaml file, it stops working altogether. This isn't a build pipeline, I just want to start a container on a schedule.

Foozinator avatar Aug 11 '20 22:08 Foozinator

@Foozinator what is your yaml file look like? what error did you encounter?

northtyphoon avatar Aug 18 '20 15:08 northtyphoon

@northtyphoon, I ended up putting basic environment variables in a configuration file and using App Configuration resource for the rest. I was able to get the acr task schedule working at that point.

Foozinator avatar Sep 04 '20 16:09 Foozinator

Edit: Resolved. Hi, I need to pass a WINDOWS_VERSION variable that is defined in the YAML file.

Command az acr run --registry logeto -f build.yml --platform windows --set WINDOWS_VERSION=1809-rc1 .

YAML file build.yml

version: v1.1.0
steps:
  - id: web
    build: -t $Registry/web:$ID -f web/dockerfile --build-arg WINDOWS_VERSION={{.Values.WINDOWS_VERSION}} web
    when: ["-"]
  - id: service
    build: -t $Registry/service:$ID -f web/dockerfile --build-arg WINDOWS_VERSION={{.Values.WINDOWS_VERSION}} service
    when: ["-"]

TBurda avatar Sep 08 '20 09:09 TBurda

@LMSSonos As @TBurda https://github.com/Azure/acr/issues/395#issuecomment-688744627 shows, combine --set with --build-arg, but you need to tweak your dockerfile to turn the ARG into ENV.

Here is complete example:

  • HelloWorld.yaml
version: v1.1.0
steps:
  - build: |
      --platform linux \
      --tag {{.Run.Registry}}/hello-world:{{.Values.BaseImage}}-{{.Values.BaseImageTag}} \
      --file HelloWorld.dockerfile \
      --build-arg BASE_IMAGE={{.Values.BaseImage}} \
      --build-arg BASE_IMAGE_TAG={{.Values.BaseImageTag}} \
      .
  - cmd: |
      {{.Run.Registry}}/hello-world:{{.Values.BaseImage}}-{{.Values.BaseImageTag}}
  • HelloWorld.dockerfile
ARG BASE_IMAGE
ARG BASE_IMAGE_TAG

FROM ${BASE_IMAGE}:${BASE_IMAGE_TAG}
ARG BASE_IMAGE
ARG BASE_IMAGE_TAG
ENV BASE_IMAGE=${BASE_IMAGE}
ENV BASE_IMAGE_TAG=${BASE_IMAGE_TAG}
RUN echo "*** Hello from image built from ${BASE_IMAGE}:${BASE_IMAGE_TAG} ***" > /hello.txt
CMD ["cat", "/hello.txt"]
  • Build and run
az acr run --registry mycr --file .\HelloWorld.yaml --set BaseImage=alpine --set BaseImageTag=3 .

and the *** Hello from image built from alpine:3 *** line below is based on the values passed by --set via --build-arg and down to the container environment variables:

Packing source code into tar to upload...
Uploading archived source code from 'C:\Users\mateuszl\AppData\Local\Temp\cli_source_archive_98ecc48cd5ec402fb24b39bcc8bff73b.tar.gz'...
Sending context (605.000 Bytes) to registry: mycr...
Queued a run with ID: db24
Waiting for an agent...
2022/08/06 10:57:44 Downloading source code...
2022/08/06 10:57:45 Finished downloading source code
2022/08/06 10:57:45 Alias support enabled for version >= 1.1.0, please see https://aka.ms/acr/tasks/task-aliases for more information.
2022/08/06 10:57:45 Creating Docker network: acb_default_network, driver: 'bridge'
2022/08/06 10:57:45 Successfully set up Docker network: acb_default_network
2022/08/06 10:57:45 Setting up Docker configuration...
2022/08/06 10:57:46 Successfully set up Docker configuration
2022/08/06 10:57:46 Logging in to registry: mycr.azurecr.io
2022/08/06 10:57:46 Successfully logged into mycr.azurecr.io
2022/08/06 10:57:46 Executing step ID: acb_step_0. Timeout(sec): 600, Working directory: '', Network: 'acb_default_network'
2022/08/06 10:57:46 Scanning for dependencies...
2022/08/06 10:57:47 Successfully scanned dependencies
2022/08/06 10:57:47 Launching container with name: acb_step_0
Sending build context to Docker daemon  3.072kB
Step 1/9 : ARG BASE_IMAGE
Step 2/9 : ARG BASE_IMAGE_TAG
Step 3/9 : FROM ${BASE_IMAGE}:${BASE_IMAGE_TAG}
3: Pulling from library/alpine
530afca65e2e: Pulling fs layer
530afca65e2e: Verifying Checksum
530afca65e2e: Download complete
530afca65e2e: Pull complete
Digest: sha256:7580ece7963bfa863801466c0a488f11c86f85d9988051a9f9c68cb27f6b7872
Status: Downloaded newer image for alpine:3
 ---> d7d3d98c851f
Step 4/9 : ARG BASE_IMAGE
 ---> Running in a1004011d81b
Removing intermediate container a1004011d81b
 ---> b903c33a50bc
Step 5/9 : ARG BASE_IMAGE_TAG
 ---> Running in 15a8b21dd1df
Removing intermediate container 15a8b21dd1df
 ---> 6ac5e37fe20d
Step 6/9 : ENV BASE_IMAGE=${BASE_IMAGE}
 ---> Running in 3f0004250588
Removing intermediate container 3f0004250588
 ---> 4331b5e9bbe5
Step 7/9 : ENV BASE_IMAGE_TAG=${BASE_IMAGE_TAG}
 ---> Running in 119e523db025
Removing intermediate container 119e523db025
 ---> 8aeeac9d366d
Step 8/9 : RUN echo "*** Hello from image built from ${BASE_IMAGE}:${BASE_IMAGE_TAG} ***" > /hello.txt
 ---> Running in 5d2bf4e43ec6
Removing intermediate container 5d2bf4e43ec6
 ---> 4858c61a2f10
Step 9/9 : CMD ["cat", "/hello.txt"]
 ---> Running in 35bbe5f1ca1a
Removing intermediate container 35bbe5f1ca1a
 ---> d2beb3d93229
Successfully built d2beb3d93229
Successfully tagged mycr.azurecr.io/hello-world:alpine-3
2022/08/06 10:57:56 Successfully executed container: acb_step_0
2022/08/06 10:57:56 Executing step ID: acb_step_1. Timeout(sec): 600, Working directory: '', Network: 'acb_default_network'
2022/08/06 10:57:56 Launching container with name: acb_step_1
*** Hello from image built from alpine:3 ***
2022/08/06 10:57:56 Successfully executed container: acb_step_1
2022/08/06 10:57:56 Step ID: acb_step_0 marked as successful (elapsed time in seconds: 9.211384)
2022/08/06 10:57:56 Populating digests for step ID: acb_step_0...
2022/08/06 10:57:57 Successfully populated digests for step ID: acb_step_0
2022/08/06 10:57:57 Step ID: acb_step_1 marked as successful (elapsed time in seconds: 0.558312)
2022/08/06 10:57:57 The following dependencies were found:
2022/08/06 10:57:57
- image:
    registry: mycr.azurecr.io
    repository: hello-world
    tag: alpine-3
  runtime-dependency:
    registry: registry.hub.docker.com
    repository: library/alpine
    tag: "3"
    digest: sha256:7580ece7963bfa863801466c0a488f11c86f85d9988051a9f9c68cb27f6b7872
  git: {}


Run ID: db24 was successful after 14s

mloskot avatar Aug 06 '22 11:08 mloskot

Hello. I am trying to set environment variables for a 'cmd' task that I am running from an ACR YAML I have built, but the environment variables are not making it through to the .NET application I am running in the container. I have had a lot of trouble finding the right way to do this in the documentation.. does this look right? I have tried a few other ways and they don't get accepted when I try to run the Task.

- cmd: $Registry/myimage:latest --env ["mySetting1=myValue1", "mySetting2=myValue2"]

All I am looking to do is set the Environment config variables for the .NET Application I am running after the build and push is completed in the YAML.

philip-young avatar Sep 27 '22 21:09 philip-young

Edit: Resolved. Hi, I need to pass a WINDOWS_VERSION variable that is defined in the YAML file.

Command az acr run --registry logeto -f build.yml --platform windows --set WINDOWS_VERSION=1809-rc1 .

YAML file build.yml

version: v1.1.0
steps:
  - id: web
    build: -t $Registry/web:$ID -f web/dockerfile --build-arg WINDOWS_VERSION={{.Values.WINDOWS_VERSION}} web
    when: ["-"]
  - id: service
    build: -t $Registry/service:$ID -f web/dockerfile --build-arg WINDOWS_VERSION={{.Values.WINDOWS_VERSION}} service
    when: ["-"]

It works, why don't they put in the documentation, I have been looking for this for a whole day

BenoityipMSFT avatar Sep 29 '22 12:09 BenoityipMSFT

Hi! I'd love this feature; I'm trying to pass environment variables to a cmd step:

az acr task run --name testtask --registry tracerimages -f run_task.yaml --set ACR_LOCATION=westus

$ cat run_task.yaml
version: v1.1.0
steps:
  - cmd: tracerimages.azurecr.io/tracer:latest
  env:
    - ACR_LOCATION={{.Values.ACR_LOCATION}}

But there's no ACR_LOCATION env var available in my container.

adamf avatar Dec 28 '22 04:12 adamf

--build-arg is still not working for me in yaml file. I tried passing even a hardcoded value in --build-arg , yet its not picked up in dockerfile for me. Someone please help.

pdalvi1893 avatar Apr 04 '24 14:04 pdalvi1893