Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Conditional Stopping of Docker Containers #71

Closed
mickelsonm opened this issue Feb 15, 2014 · 9 comments
Closed

[Question] Conditional Stopping of Docker Containers #71

mickelsonm opened this issue Feb 15, 2014 · 9 comments

Comments

@mickelsonm
Copy link

I was wondering how to conditionally stop a docker container. I have a deployment process that I am writing that needs to stop a running docker container if it is running or if it is not running, then it should cleanly exit and continue on.

I tried something like this:

    docker_container "#{app.name}" do
        action :stop
        not_if('sudo docker ps | grep "#{app.name}" | wc -l') 
    end

But that didn't seem to work. I am really new to using Chef and Ruby in general, so any help or step in the right direction would be appreciated. Thanks!

@bflad
Copy link
Contributor

bflad commented Feb 15, 2014

docker_container "name" do
  # ...
  action :stop
end

already does the behavior you mentioned to make writing your cookbooks easy. Here's the code in case you're more curious: https://github.com/bflad/chef-docker/blob/master/providers/container.rb#L96

Let me know if you have additional questions or if its not behaving as expected.

@bflad bflad closed this as completed Feb 15, 2014
@mickelsonm
Copy link
Author

I just tried this:

docker_container "#{app.name}" do
action :stop
end

It doesn't appear to be working as I would expect. Here's the output I got from the test:

Recipe: deployment::none
  * service[webapp] action stop
================================================================================
Error executing action `stop` on resource 'service[webapp]'
================================================================================


Errno::ENOENT
-------------
No such file or directory - /sbin/status webapp


Resource Declaration:
---------------------
# In /home/deployer/chef-solo/cookbooks-2/docker/providers/container.rb

238:   service service_name do
239:     case new_resource.init_type
240:     when 'systemd'
241:       provider Chef::Provider::Service::Systemd
242:     when 'upstart'
243:       provider Chef::Provider::Service::Upstart
244:     end
245:     supports :status => true, :restart => true, :reload => true



Compiled Resource:
------------------
# Declared in /home/deployer/chef-solo/cookbooks-2/docker/providers/container.rb:238:in `service_action'

service("webapp") do
  provider Chef::Provider::Service::Upstart
  action [:stop]
  supports {:status=>true, :restart=>true, :reload=>true}
  retries 0
  retry_delay 2
  service_name "webapp"
  pattern "webapp"
  cookbook_name :"deployment"
  recipe_name "none"
end

@bflad
Copy link
Contributor

bflad commented Feb 16, 2014

Which OS are you on? No such file or directory - /sbin/status seems to imply Upstart isn't installed correctly.

@mickelsonm
Copy link
Author

Debian 7, Linux app3 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux

@bflad
Copy link
Contributor

bflad commented Feb 16, 2014

Ah yeah, Debian uses SysV (init_type sysv in this cookbook) by default. Although the docker_container sysv script is there, no one has contributed an init script for the daemon yet (guess I'll add this to my to-do list tonight). If you're stuck on using Upstart, I believe you need to install the Debian upstart package. I'm not sure if this cookbook should handle that for you by default as it seems outside its duties.

@bflad
Copy link
Contributor

bflad commented Feb 16, 2014

@mickelsonm
Copy link
Author

Thanks for the good information. I am a bit hesitant on adding in upstart when I am planning on using runit, which goes along with issue #72 . Any chance you could incorporate runit into the list for docker functionalities? Thanks.

@bflad
Copy link
Contributor

bflad commented Feb 16, 2014

I'm looking at all the runit documentation now. Should have an initial runit setup hopefully tonight.

@mickelsonm
Copy link
Author

Thanks man! Definitely looking forward to it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants