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.

  • How to update docker in aws ec2
  • AWS ECS - Images from S3
  • Is it possible to use vagrant + aws provider + docker provider?
  • Cannot authenticate to Docker in Elastic Beanstalk through S3
  • How to deploy docker container image updates from AWS ECR to ECS?
  • Multi-host Docker compose application on Swarm
  • 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.)

    Edit

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

    #!/bin/bash
    echo "running"
    function mytest {
        "$@"
        local status=$?
        if [ $status -ne 0 ]; then
            knex migrate:rollback 
          echo "Rolling back knex migrate $1" >&2
          exit 1
        fi
        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.

  • Deploy image to AWS Elastic Beanstalk from private Docker repo
  • Elastic Beanstalk Docker images failing to pull
  • Error Deploying Docker Container to Elastic Beanstalk
  • How can I run a Docker container in AWS Elastic Beanstalk with non-default run parameters?
  • Deploy docker on AWS beanstalk with docker composer
  • AWS ECS agent won't start
  • 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
    bar
     ---> 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.

    Thanks!

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