2

I'm trying to find long-running docker containers.

First running docker ps --format '{{.RunningFor}}' returns something like

About a minute ago
11 minutes ago

which is completely useless for machine processing. I couldn't find any way to change this, and the same value is in JSON output.

Next I tried docker ps --format '{{.CreatedAt}}'. This returns values like 2019-01-03 12:49:46 +0000 UTC.

Problem is this isn't ISO 8601 nor any other common format. What's worse, trying to parse it with date gives:

date --date="$DATE"
date: invalid date ‘2019-01-03 12:49:46 +0000 UTC’

I expect there has to be a better way to get an epoch milliseconds value, than processing the value manually with awk etc, but I can't find it. Best thing would be a function like {{epoch .CreatedAt}} (similar to {{lower .Name}})

  • Best workaround so far: echo $DATE | sed 's/ +0000 UTC/+00:00/' makes it date read it – Jakub Bochenski Jan 3 at 16:29
2

There is docker inspect <container-name> which gives you detailed infos about one container.

You might be interested in the value of State.StartedAt

 "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1234,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-12-20T12:41:54.281709415Z",
            "FinishedAt": "2018-12-20T12:41:28.781748517Z"
        },

To inspect all running containers you could execute:

docker container ls --format="{{.Names}}" | xargs -n1 docker container inspect

JSON output: Having jq installed (apt-get install jq), you can filter just the container name and the StartedAt with

docker container ls --format="{{.Names}}" | xargs -n1 docker container inspect | jq '.[] | {name: .Name, uptime: .State.StartedAt}'

CSV output: (semicolon separated, sorted by age)

docker container ls --format="{{.Names}}" | xargs -n1 docker container inspect --format='{{.Name}};{{.State.StartedAt}}' | sort -k2,1

EDIT:

using docker ps (This is assuming that creation time = start time. Which doesn't work if you stop and start containers. docker ps only gives RunningFor and Status, both in human readable "5 weeks ago" style. It's more reliable to use docker inspect method to get the actual runtime.)

docker ps --format="{{.CreatedAt}} {{.Names}}" | sort -k1,1

EDIT2:

It turns out, that you can do all this with go and the docker api. Here is a simple demo I just coded:

You could grab the binary link: Or change the code to your liking and compile it by yourself link

The result will look like this:

container_age for API v1.37
DURATION ID         NAME
50h      1234567890 /jenkins
362h     1234567891 /elasticsearch
  • That's not ideal, but it works. So there is no way to convert the values returned from docker ps? – Jakub Bochenski Jan 4 at 9:20
  • 1
    added EDIT part to my post, using docker ps – Michael D. Jan 4 at 10:29
  • problem is the docker ps command still gives the non-parsable date format of 2019-01-04 10:47:54 +0000 UTC – Jakub Bochenski Jan 4 at 10:59
  • Thanks for the go program, I wish I could upvote the answer more than once. However in practice it's easier to use docker cli + xargs (which are already installed) than deploying and managing a custom utility like this. However it's a nice point that you can always go the API way if all else fails – Jakub Bochenski Jan 4 at 15:35
0

In case it's useful to someone here is a bash script I put together based on the anwser by Michael D.

        before=$(date -d"$SINCE" +%s)

        while read -r id started
        do
          milis=$(date -d"$started" +%s)
          if (( before > milis ))
          then
            echo "$id"
          fi
        done < <(docker ps -a --format="{{.ID}}" | xargs -n1 docker container inspect --format='{{.ID}} {{.State.StartedAt}}') \
        | xargs docker rm -f -v

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.