Rancher API: Delete container

I would like to delete running container via the API, I didn’t see a such action:

/v1/projects/1a5/services/1s19/instances

{
“update”: “…/v1/projects/1a5/containers/1i222/?action=update”,
“stop”: “…/v1/projects/1a5/containers/1i222/?action=stop”,
“restart”: “…/v1/projects/1a5/containers/1i222/?action=restart”,
“migrate”: “…/v1/projects/1a5/containers/1i222/?action=migrate”,
“logs”: “…/v1/projects/1a5/containers/1i222/?action=logs”,
“setlabels”: “…/v1/projects/1a5/containers/1i222/?action=setlabels”,
“execute”: “…/v1/projects/1a5/containers/1i222/?action=execute”,
}

Couple of things:

Are you looking to delete a container or a service? In your API, it looks like you are deleting a container within a service based on the fact that you are looking at actions within /v1/projects/1a5/services/1s19/instances,. The service will spin up another container to replace the one that you have deleted.

If you want to delete a specific container, I’d recommend using the UI to navigate to the API endpoint by using the “View in API” option from the dropdown in the UI. Similarly, if you want to delete the service, you should use the “View in API” option from the dropdown.

As for deleting, you need to stop the container first. And then you will be able to purge it. The actions below are available for stopped containers.

"actions": {
"restore": "…/v1/projects/1a5/containers/1i1/?action=restore",
"purge": "…/v1/projects/1a5/containers/1i1/?action=purge",
"setlabels": "…/v1/projects/1a5/containers/1i1/?action=setlabels",
}

This hits a couple warts in the API in the list of things that need to be fixed before we really encourage people to build against it. If you are building anything against the API, you should be familiar with #384 and #1052 for things we are planning on changing. Some of them will be breaking changes, but we will not be bumping the version or supporting backwards compatibility until after a 1.0/“GA” release. You can at least follow along and have an idea what is coming to be prepared for it (i.e. make “project” vs “environment” a variable you can change easily in one place). After 1.0 we will either not break things, or bump to /v2 and continue to support the old behavior in /v1 through a deprecation life cycle TBD.

Regarding your specific issue:

  • The actions map shows you only the actions that are allowed from the current state of the resource. This tells you which things you can do right now.
  • The action you’re looking for is remove, but it is not available on a running container, it needs to be in stopped state first.
    • This is really dumb, you should always be able to delete any resource and we should make it go through whatever lifecycle it needs to accomplish that. We will fix this someday in #384.
  • I realize they show up and look like actions you should use, but the presence of the remove, update, and create actions is an inadvertent side-effect of our API framework. These shouldn’t actually show up in the actions list, their functionality should always work, not depend on the state of the resource.
    • The correct way to do those actions is with the appropriate HTTP methods (Create=POST, Update=PUT, and Remove=DELETE).
    • Their visibility and ability to call them as actions will be removed in #1052.
    • We have ironically kept them around precisely because of the other issue… Remove isn’t always allowed so the UI uses the presence of the remove action to as a signal that it should show the option. So you may need to use it to do the same for now.
    • But definitely call DELETE {the_resource.links.self}, not POST {the_resource.actions.remove}.

Thanks you,

curl -XDELETE http://infra.admin.******.com:8080/v1/projects/1a5/containers/1i3977

works like a charm. Can’t wait for a RAML documentation of Rancher API, and beautiful Ruby/Php/NodeJS SDKs :wink:

Found a nice Php project:

1 Like