0

Problem
Inside a Dockerfile, I want to do something like this:

ENV CONTAINER_INFO_GCC=\"$(gcc --version | head -n 1)\"

In a perfect world, at build time, this would produce this environment variable:

CONTAINER_INFO_GCC="gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"

Motivation

I want to see what is installed in my docker container with the Azure DevOps system capabilities windows.

For example, if I want to know which version of gcc is installed in my build environment, I can just take a look here: enter image description here

Question
I have tried doing something like this, without the desired effect.

RUN echo CONTAINER_INFO_GCC=\"$(gcc --version | head -n 1)\" >> /etc/environment

Is there a way to use the Dockerfile ENV command dynamically?

gberth
  • 467
  • 5
  • 13
  • For use the Dockerfile ENV command dynamically, can you explain this requirement? Can you share the ideal state? You could try to use **Run export** command to check if it could work.. (e.g. `RUN export BAZ="$(gcc --version | head -n 1)"`) – Kevin Lu-MSFT Oct 09 '20 at 08:32
  • 1
    > Can you share the ideal state Good point. I edited my question with that in mind. The run export command would not work. Here's more details: https://stackoverflow.com/questions/33379393/docker-env-vs-run-export – gberth Oct 09 '20 at 13:42
  • Hi @gberth. Yes. You are right.So your requirement is to keep the value of this env in every docker command,right? Based on my test, if you set the command in ENV, the command will not execute. – Kevin Lu-MSFT Oct 12 '20 at 09:37
  • Hi @gberth. Is there any update about this ticket? Feel free to let me know if the answers could give you some help. Just a remind of [this](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Kevin Lu-MSFT Oct 16 '20 at 08:25
  • 1
    I'm sorry for my late response @KevinLu-MSFT. Crazy week. – gberth Oct 16 '20 at 13:32
  • It's ok. Do you have any update about it? Is there a suitable way to achieve your goal? – Kevin Lu-MSFT Oct 19 '20 at 08:08
  • Updated my answer. – gberth Oct 19 '20 at 15:48

1 Answers1

1

As each dockerfile command is run, it will generate a intermediate container.

So the RUN , COPY, CMD ... command couldn't pass the environment to next container.

You need to use ENV set the Environment, but the ENV will not execute the command.

In Azure Devops, you could use Self-hosted Agent(Running in Docker) to create a variable.

Here is the steps:

Step1: Create a Self-Hosted Agent running in Docker.

Step2: In Build Pipeline, you could run the gcc --version | head -n 1

Here is a Blog about create Self-Hosted Agent running in Docker.

Update:

You could try to add the container resource to Azure Pipeline, then you could run the script on the container.

Here is a doc about this feature.

Here is the Yaml example:

resources:
  containers:
  - container: python
    image: python:3.8
 

trigger:
- none
pool:
   vmimage: ubuntu-16.04

steps:
- script: |
    gcc --version | head -n 1
    echo "##vso[task.setvariable variable=test]$(gcc --version | head -n 1)"
  displayName: 'Run a multi-line script'
  target: 
    container: python
    commands: restricted   

- script: |
    echo "$(test)"
  displayName: 'Run a multi-line script'

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $token = "PAT"
      
      $url="https://dev.azure.com/{Organization Name}/_apis/distributedtask/pools/{Pool Id}/agents/{AgentID}/usercapabilities?api-version=5.0"
      
      $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
      
      $JSON = @'
      {
         "Gcc-version":"$(test)"
      }
      '@
      
      $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method PUT -Body $JSON -ContentType application/json

Result:

enter image description here

The Rest API is used to update the Agent Capabilities.

Note: We can only manually change the user defined Capabilities.

On the other hand, you still could create a Self-hosted agent running in docker.

Then you could directly run the same script on the agent and get the tool version.

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Hi thanks for your time! My objective was to push information from the docker environment to the Azure DevOps ecosystem using environment variables. Your answer does the opposite. I have updated my question to make it clearer. Thanks for your answer. – gberth Oct 19 '20 at 15:41
  • Hi @gberth. Thanks for your feedback. I have updated the answer. Hope it could give you some help. – Kevin Lu-MSFT Oct 20 '20 at 07:08
  • 1
    I think you probably solved it! I thought the only way would be with the ENV variables, but I think your solution is better. Thanks for your time – gberth Oct 20 '20 at 18:50