Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

Destroying images #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matiasdecarli
Copy link
Contributor

Rigth now there is a bug when trying to destroy images.
Here is the problem

Chef::Log.debug("Destroying image: chef:#{container_name}")
image = Docker::Image.get("chef:#{container_name}")
image.delete

But in docker-api docs says that we should get the image by his ID

# Get Image from the server, with id
Docker::Image.get('df4f1bdecf40')

@matiasdecarli
Copy link
Contributor Author

Ok, I actually solved this using

target_repository = 'chef'
image = find_image(target_repository, container_name)

if image == nil
  Chef::Log.debug("Image not found!")
else
  Chef::Log.debug("Removing image!")        
  image.delete
end

But im getting an error here container.delete when the container is running, even if right before theres a container.stop

If the container is stopped, this delete the container and the first part, deletes the images. Any idea why?

This is the log

[2014-10-07T15:14:04+00:00] DEBUG: Destroying container: deploy-8223
[2014-10-07T15:14:04+00:00] DEBUG: Stopping deploy-8223
[2014-10-07T15:14:04+00:00] DEBUG: Removing deploy-8223


    ================================================================================
    Error executing action `destroy` on resource 'machine[deploy-8223]'
    ================================================================================

    Docker::Error::ServerError
    --------------------------
    Expected(200..204) <=> Actual(500 InternalServerError)

    Resource Declaration:
    ---------------------
    # In /home/vagrant/cookbooks/matiasdecarli/recipes/create_container.rb

      7: machine 'deploy-8223'  do  
      8:   action :destroy
      9: end
     10: 

    Compiled Resource:
    ------------------
    # Declared in /home/vagrant/cookbooks/matiasdecarli/recipes/create_container.rb:7:in `from_file'

    machine("deploy-8223") do
      action [:destroy]
      retries 0
      retry_delay 2
      guard_interpreter :default
      chef_server {:chef_server_url=>"https://api.opscode.com/organizations/matiasdecarli", :options=>{:client_name=>"matiasdecarli", :signing_key_filename=>"/etc/chef/client.pem"}}
      driver "docker"
      cookbook_name :matiasdecarli
      recipe_name "create_container"
    end

[2014-10-07T15:14:16+00:00] INFO: Running queued delayed notifications before re-raising exception
[2014-10-07T15:14:16+00:00] DEBUG: Re-raising exception: Docker::Error::ServerError - machine[deploy-8223] (matiasdecarli::create_container line 7) had an error: Docker::Error::ServerError: Expected(200..204) <=> Actual(500 InternalServerError)

/var/lib/gems/1.9.1/gems/docker-api-1.13.0/lib/docker/connection.rb:54:in `rescue in request'
  /var/lib/gems/1.9.1/gems/docker-api-1.13.0/lib/docker/connection.rb:37:in `request'
  /var/lib/gems/1.9.1/gems/docker-api-1.13.0/lib/docker/connection.rb:61:in `block (2 levels) in <class:Connection>'
  /var/lib/gems/1.9.1/gems/docker-api-1.13.0/lib/docker/container.rb:134:in `remove'
  /var/lib/gems/1.9.1/gems/chef-metal-docker-0.4.3/lib/chef_metal_docker/docker_driver.rb:163:in `destroy_machine'

Update: I've solved this catching the exception. Probably not the best way to do it, but it does the trick

matiasdecarli added a commit to matiasdecarli/chef-metal-docker that referenced this pull request Oct 7, 2014
@jkeiser jkeiser added this to the 1.0 milestone Nov 6, 2014
@matiasdecarli
Copy link
Contributor Author

I can make a pull request about this @jkeiser

@jkeiser
Copy link
Contributor

jkeiser commented Nov 7, 2014

Sounds great!

@jkeiser jkeiser added Developing and removed Waiting labels Nov 7, 2014
@matiasdecarli
Copy link
Contributor Author

Converted into PR

begin
Chef::Log.debug("Removing #{container_name}")
container.delete
rescue Docker::Error::ServerError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the docs, this is hiding a 500 (meaning there is an issue with the Docker code). Is there a corresponding bug filed in Docker itself?

I'd feel a lot more comfortable here if we did an if container.exists? (or its equivalent) rather than catching all 500's and saying they are OK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to destroy any containers when we destroy the image? The Docker CLI tool requires -f to delete images used by live containers.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

3 participants