# How can environment variables be extracted from the Docker HOST to pass through to a Docker container-agent?

**URL:** <https://community.jenkins.io/t/how-can-environment-variables-be-extracted-from-the-docker-host-to-pass-through-to-a-docker-container-agent/21776>\
**Category:** Ask a question\
**Tags:** question, docker, pipeline\
**Created:** [October 13, 2024, 12:44pm UTC](https://community.jenkins.io/t/how-can-environment-variables-be-extracted-from-the-docker-host-to-pass-through-to-a-docker-container-agent/21776 "2024-10-13T12:44:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bb484](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@bb484](https://community.jenkins.io/u/bb484)\
**Post date:** [October 13, 2024, 12:44pm UTC](https://community.jenkins.io/t/how-can-environment-variables-be-extracted-from-the-docker-host-to-pass-through-to-a-docker-container-agent/21776/1 "2024-10-13T12:44:47Z")

</div>

Jenkins setup:  
Jenkins: 2.462.3

I have a multi-branch pipeline where the stages will be executed in a Docker agent. Because the Docker agent configuration is the same regardless of the stage I would like to specify the Docker agent at the top level/pipeline level, thus preventing repeatedly specifying it at the stage level with all the configuration that it requires.

My top-level agent looks like:

```auto
agent {
    docker {
        image "maven"
        args "-v /var/run/docker.sock:/var/run/docker.sock --group-add 999"
    }
}

```

Of note is that I mount the _host’s_ Docker socket into the container AND that I assign the additional group ‘999’ (which is the host’s `docker` gid) to the process in the container so it may use the host’s Docker socket.

This approach works fine however the Docker gid is hard-coded and I would like to extract it dynamically, for example the Docker-host-agent may be a $lave (Jenkins ‘community’ owners, please stop censoring non-offensive words and grow up!) with a different `gid` for the `docker` user. Because it’s the top-level agent it’s not possible to run bash script (as far as I can tell) to extract the `gid`. e.g. I need to execute in bash the script:

```auto
getent group docker | cut --delimiter ':' --field 3

```

What I need is the ability to setup some environment variables using a bash script _before_ the _pipeline_ Docker-agent starts and the script _must_ execute on the Docker _host_ where the agent will run.

Plugins such as [Envinject](https://plugins.jenkins.io/envinject/) and [Environment script](https://plugins.jenkins.io/environment-script/) seem to offer the functionality I need but they don’t seem to be available for use in a multi-branch pipeline job.

My work around at the moment is to specify the docker agent at each stage but have a first stage which runs on a ‘native’ agent to extract the `gid`.
