skip to Main Content

When I run a docker container with docker run

docker run -d --memory=100m --cpus=0.5 -e SECRENT_ENV=$SECRET_ENV -e name=name --network=nginx-proxy image-name

via blueocean in jenkins and set an environment variable inside blue ocean

SECRET_ENV=SECRET_DATA

the environment variable is committed to the repo inside the jenkinsfile in plain text.

How can I pass an environment variable from my host machine (that jenkins is running on) to the docker run command.

extra info:
– running on centos 7
– trying to run a nodejs app
– Jenkins is itself running inside a docker container
– tried to set the environment variable on host machine via ./bashrc export SECRET_ENV=secret_data

2

Answers


  1. First Thing, You can consume host Environment variables by passing it through the Docker run command.

    export SECRENT_ENV=some-secret export the ENV on the Host machine or you can consume from ~/.bashrc as you do seems fine.

    docker run --rm -it -e SECRENT_ENV=$SECRENT_ENV alpine ash -c "echo $SECRENT_ENV"
    

    Here The problem is you are running Jenkins inside docker, so the rest of the container will only able to consume the Environment variable that is defined inside Jenkins Container. To make all environment variables to Jenkins you need to update Jenkins run command.

    docker run --rm -it -v /home/centos/.bashrc:/root/.bashrc alpine ash -c "echo $USER"
    

    This will return Host user name that is centos so one should avoid this as all the environment variable of Jenkins container is override by host env.

    So you need to pass only desired ENV to Jenkins or you can use env file to Jenkins.

    docker run --name jenkins -e SECRENT_ENV=123 -e SECRENT_ENV2=1234 -dit jenkins/jenkins
    

    Now update ENV in the Jenkins file.

    SECRET_ENV=${env.SECRET_ENV} 
    

    jenkins-pipeline-environment-variables

    Login or Signup to reply.
  2. You can a create a environment variable in Jenkins configuration page (http://localhost:8080/jenkins/configure) and then search for Environment variables.

    And add the required variable and use in your Jenkins pipeline, please see below:-

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search