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:

  • How to pass Chef data bag secret to a docker container?
  • Installing chef-server in a docker container
  • Using chef-zero + docker + kitchen with multiple nodes recipe
  • How to run multiple supporting apps (tomcat) on my local mac which I don't modify?
  • How to implement the “One Binary” principle with Docker
  • Want to ssh into a running docker container running inside CentOs Image

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


    In production, the value might be, 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?

  • No suitable driver found (jdbc:pgsql)
  • Node.js process, running in Docker container hangs without exiting or throwing when out of memory
  • Dockerfile - how do I pass passwords from a file into ENV variables?
  • Kubernetes, Flannel and exposing services
  • Run docker mongo with read-only fs
  • jenkins pipeline after run unit test karma script do not stop
  • 3 Solutions collect form web for “Using Chef to configure Docker image?”

    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.

      image: ourCompany/ourApp:latest
        file: base.yml
        service: web
        - 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.

    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.

    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

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

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