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:

  • Kitchen-Docker and Systemd
  • Dynamically Pull Docker Image using chef recipe
  • Error response from daemon: Container f88566c370dd is not running
  • Consul cluster automatic bootstrap on docker
  • Disable docker container log configuration in Chef
  • How to access private Docker repository in OpsWorks ECS Layer?
  • 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?

  • where does docker attach volume on host machine when its not specified?
  • Docker: How to live sync host folder with container folder?
  • Docker, how to get container information from within the container?
  • Which application is the docker binary from?
  • Limit useable host resources in Docker compose without swarm
  • Docker interactive mode and executing script
  • 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.

    #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.

    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
    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.

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