smeeb
smeeb

Reputation: 29477

Using Chef to configure Docker image?

I am interested to see if I can use Chef to configure a Docker container (sample Dockerfile here).

Say in my image, I wish to have an env var set, so my Dockerfile might contain an entry like so:

ENV MY_DB_URL http://devdb01.example.com

But in my QA/test environment, I might want it to look like:

ENV MY_DB_URL http://testdb01.example.com

In production, the value might be http://db01.example.com, etc. The point is that the values stored in my Dockerfile need to be different, depending on the environment (and possibly other conditions). This seems like a perfect job for a Chef template.

The problem is that the Dockerfile needs to be kept with my app's source code in VCS. So how can Chef be used to "inject" the Dockerfile with all the right values?

Upvotes: 2

Views: 3816

Answers (3)

skmichaelson
skmichaelson

Reputation: 510

The chef-docker cookbook might be of use to you. It provides resources so you can create containers via Chef. Here's an example from repo's README.

docker_container 'env' do
  repo 'debian'
  env ["PATH=#{node[:my][:path][:attribute]}", "FOO=#{node[:my][:foo][:attribute]}"]
  command 'env'
  action :run_if_missing
end

The code above sets environment variables in your container based on the node attributes you have set, which can be overridden in each environment.

Upvotes: 0

Paul Becotte
Paul Becotte

Reputation: 9977

This is actually the genius of Docker- you can override most of the configuration setup in the container at launch time. For example, our app depends on a variable APP_ENV to determine which environment it is running in (for things like database connection parameters and whatnot). Running locally we run it with

docker run -d -e APP_ENV=local ourCompany/ourApp

while in staging it gets run with

docker run -d -e APP_ENV=staging ourCompany/ourApp

You can easily set this up using Docker-compose.

#base.yml
web:
  image: ourCompany/ourApp:latest

#staging.yml
web:
  extends: 
    file: base.yml
    service: web
  environment:
    - APP_ENV=staging

Then you can launch the base app with docker-compose -f base.yml up -d, or the staging version with docker-compose -f staging.yml up -d

Trust me- WAY less complicated than using Chef in your build system and building different versions of your image for different environments.

Upvotes: 4

booyaa
booyaa

Reputation: 4557

If the code VCS is environment agnostic, then your release script needs to be responsible for updating the MY_DB_URL ENV command. Easiest way to do this is to stick a place holder in the VCS Dockerfile i.e. ENV MY_DB_URL %MY_DB_URL% and then replace it correct value for during the deployment.

Upvotes: 2

Related Questions