AWS codebuild not passing down environment variables?

We require environment variables during the codebuild process. Codebuild allows you to setup environment variables in the advanced settings, which I’ve done.

Now when codebuild runs it doesn’t seem to be passing these environment variables down.

  • Using pm2 Inside of an Auto-Scaling Environment
  • running a docker loop device on aws
  • Is it possible to launch privileged docker containers on Amazon elasticbeanstalk?
  • AWS S3 Elastic Beanstalk Docker - /dev/fus: Operation not permitted
  • Deploying Cassandra on ECS?
  • docker/boot2docker in amazon ec2
  • I printed out process.env & here’s what I got:

    NAME: '037fga72',
    [Container] 2017/02/08 01:55:03 NPM_CONFIG_LOGLEVEL: 'info',
    [Container] 2017/02/08 01:55:03 PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
    [Container] 2017/02/08 01:55:03 PWD: '/usr/src/app',
    [Container] 2017/02/08 01:55:03 SHLVL: '1',
    [Container] 2017/02/08 01:55:03 HOME: '/root',
    [Container] 2017/02/08 01:55:03 _: '/usr/local/bin/knex' } 'process.env variables...'

    There are none of my envrionment variables.

    I setup DB_PASS, DB_USER, DB_NAME, DB_HOST – None of these are printed out.

    I tried creating a new codebuild & adding the environment variables but no luck.

    Note that it’s building a docker container & it fails when I try to connect to my postgres database because the environment variables aren’t passed down (password, host, etc.)


    In my Dockerfile, I’m running the following bash file:

    echo "running"
    function mytest {
        local status=$?
        if [ $status -ne 0 ]; then
            knex migrate:rollback 
          echo "Rolling back knex migrate $1" >&2
          exit 1
        return $status
    mytest knex migrate:latest 

    What this is doing is running the knex migration (a js script) – If it fails we rollback the migration & exit the build.

  • Amazon ECS private DockerHub repo: Unable to decode provided docker credentials error
  • Amazon ECS error: cannot create a task definition with no containers
  • Docker: ECS vs Beanstalk vs Docker Cloud
  • Use PredictionIO in production
  • Error connecting to RDS Postgreql DB from inside Docker container
  • How to deploy mongoDB Docker image to Elastic Beanstalk?
  • 2 Solutions collect form web for “AWS codebuild not passing down environment variables?”

    Docker does not pass down host-level environment variables to containers at build time, so CodeBuild’s environment variables would not be provided by default. However, using build arguments you can chain the environment variables to your container.

    For example, take the following Dockerfile:

    FROM ubuntu:14.04
    ARG foo
    ENV MYVAR=$foo
    RUN echo $MYVAR

    You can set the value of MYVAR in the container with: docker build -t <tag> --build-arg foo=bar .

    Here’s the example output during docker build:

    Sending build context to Docker daemon 2.048 kB
    Step 1 : FROM ubuntu:14.04
     ---> b969ab9f929b
    Step 2 : ARG foo
     ---> Using cache
     ---> a6c71d477a6c
    Step 3 : ENV MYVAR $foo
     ---> Running in 49efc25c81d9
     ---> ecfc651713b8
    Removing intermediate container 49efc25c81d9
    Step 4 : RUN echo $MYVAR
     ---> Running in 2fc43629aa44
     ---> 86dd113f6c7b
    Removing intermediate container 2fc43629aa44
    Successfully built 86dd113f6c7b

    For your specific use case, provide the environment variable value in your docker build --build-arg flag (e.g. --build-arg db_user=$DB_USER), then set the value of the arg via ENV in your Dockerfile.

    Definitely the environment variables that you mentioned did not have all the environment variables that are expected to be available for the builds running on CodeBuild (including custom env vars that you have).

    Is it possible to share more information about your build? Are you starting a new shell (eg. bash) in your build commands (possibly replacing existing environment “/usr/bin/env”)? What is your project configuration (docker image specifically)?

    Also can you try overriding the command for one of the projects and simply run ‘env’ to validate that you are seeing the expected environment variables. This is just to eliminate if environment is the issue or the commands.


    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.