Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Remote API Filter Exited

Tags:

docker

I see in the Docker Remote API Docs that filter can be used to filter on status but I'm unsure how to form the request:

https://docs.docker.com/reference/api/docker_remote_api_v1.16/#list-containers

GET /containers/json?filters=status[exited] ?????

How should this be formatted to display ONLY exited containers?

like image 896
Ken J Avatar asked Jan 20 '15 20:01

Ken J


People also ask

How do I Dockerize API in Python?

You need to set up a working directory and then copy the contents from the local directory to the docker directory. Then, you need to run the install command so that it installs all the dependencies. Finally, you need to give the command when the container is instantiated.

How do I find my docker Engine API URL?

It depends on your host, but look for /etc/default/docker or /var/lib/boot2docker/profile (for Docker Machine hosts using a boot2docker VM). Then get the IP address of the machine hosting your Docker daemon. (With a Docker Machine created host, that would be: docker-machine ip <yourmachine> .)

How do you check whether the docker container is running or not?

Another way to check for a running Docker daemon is by inspecting its process ID file. The daemon writes its process ID to /var/run/docker. pid each time it starts up. When this file exists, Docker should be running and ready for CLI connections.


2 Answers

jwodder is correct on the filter but I wanted to go through this step by step as I wasn't familiar with the Go data types.

The Docker API documentation refers to using a map[string][]string for a filter, which is a Go map (hash table)

  • map[string] defines a map with keys of type string

  • []string is the type definition for the values in the map. A slice [] is an array without fixed length. Then the slice is made up of string values.

So the API requires a hash map of arrays containing strings. This Go Playground demonstrates marshalling the Go filter data:

mapS := map[string][]string{ "status":[]string{"exited"} }

Into JSON:

{ "status": [ "exited" ] }

So adding that JSON to the Docker API request you get:

GET /containers/json?all=1&filters={%22status%22:[%22exited%22]}

all=1 is included to report exited containers (like -a on the command line).

It might be easier for non Go people if they just documented the JSON structure for the API : /

like image 113
Matt Avatar answered Sep 19 '22 17:09

Matt


The most elegant way to use docker with curl and don't bother with encoding I found in this answer. Basically, it's tell curl to use data as query parameter and encode it. To get exited container the query may look like:

curl -G -XGET "http://localhost:5555/containers/json" \
    -d 'all=1' \
    --data-urlencode 'filters={"status":["exited"]}' | python -m json.tool
like image 45
Anton Bessonov Avatar answered Sep 20 '22 17:09

Anton Bessonov