Pass Environment Variables from Shippable to Docker

464 Views Asked by At

I am using Shippable for two reasons: to automate the build of my docker images and to pass encrypted environment variables. I am able to automate the builds but I can't pass the variables.

I start with entering the environment variable to the Shippable text box in the project settings:

SECRET_KEY=123456

I click the 'encrypt' button and then shippable returns:

- secure : hash123abc...

I put this hash into my shippable.yml file. It looks like:

language: python

python:
    - 2.7

build_image: myusername/myimagename

env:

 - secure : hash123abc...

build:
 post_ci:
  - docker login -u myusername -p mypassword
  - docker build -t myusername/myimagename:latest .
  - docker push myusername/myimagename:latest

integrations:
 hub:
  - integrationName : myintegrationname
    type: docker
    branches:
     only:
      - master

The automated build works! But if I try:

sudo docker run myusername/myimagename:latest echo $SECRET_KEY

I get nothing.

My Dockerfile which sets the environment variables (in this case SECRET_KEY) looks like this:

FROM python:2.7.11

RUN apt-get update

RUN apt-get install -y git

RUN get clone https://github.com/myusername/myrepo.git

ENV SECRET_KEY=$SECRET_KEY

It might be helpful to explain MY logic as I see it. Because my thinking may be the issue if it's not in the code:

The shippable project build is triggered (by a repo push or manually). In shippable.yml it does some things:

  • builds the initial image
  • sets the SECRET_KEY environment variable
  • builds the new image based on the Dockerfile
    • the Dockerfile: -- sets the env variable SECRET_KEY to the SECRET_KEY set by the .yml two steps earlier
  • pushes the image

I'm thinking that now I've set an environment variable in my image I can now access it. But I get nothing. What's the issue here?

1

There are 1 best solutions below

0
On

Thanks @Alex Hall for working this out with me!

It turns out that passing environment variables with Docker in this setting must be done with a simple flag to start. So in my shippable.yml I changed:

- docker build -t myusername/myimagename:latest .

to

- docker build --build-arg SECRET_KEY=$SECRET_KEY -t myusername/myimagename:latest .

Then in my Dockerfile I added:

ARG SECRET_KEY

RUN echo $SECRET_KEY > env_file

Lo and behold the key was in env_file