Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active January 31, 2016 08:25
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save adamw/792f8c22abb09699b6d5 to your computer and use it in GitHub Desktop.
# docker/recipes/setup.rb:
package "docker" do
package_name "lxc-docker-0.11.1"
action :install
end
# docker/recipes/kill_containers.rb:
script "kill_all_containers" do
interpreter "ruby"
user "root"
code <<-EOH
`docker ps -q`.split("\n").each do |container_id|
`docker stop \#{container_id}`
end
`docker ps -a -q`.split("\n").each do |container_id|
`docker rm \#{container_id}`
end
EOH
end
# docker/recipes/myapp.rb: (one recipes for each container/application to run)
pull_and_run_app(:myapp)
# docker/libraries/common.rb:
def pull_image(name, image)
script "pull_app_#{name}_image" do
interpreter "bash"
user "root"
code <<-EOH
docker pull #{image}
EOH
end
end
def run(name, image, cmdline, env)
envs = env.map { |k, v|
"-e #{k}=#{v}"
}.join(" ")
log "Docker environment: #{envs}"
script "run_app_#{name}_container" do
interpreter "bash"
user "root"
code <<-EOH
docker run -d --name=#{name} #{envs} #{image} #{cmdline.join(" ")}
EOH
end
end
def pull_and_run_app(name)
data = node[name]
image = data[:image]
pull_image(name, image)
run(name, image, data[:cmdline], data[:env])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment