Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #96: See How Long a Container Ran For

docker-tips-and-tricks.jpg

This could be handy if you're running multi-hour commands and want to see how long they took afterwards.

Prefer video? Here’s a recorded version of this tip on YouTube which covers this topic.

You can run docker container inspect <container> to get a JSON response back which includes a bunch of attributes about the container. Near the top of the output there’s a State object which includes StartedAt and FinishedAt.

It’s import to mention that if you run your container with --rm then when the container is stopped you won’t have the <container> available, so if you plan to measure how long a container took to run you’ll want to avoid using that flag. You can find your stopped containers with docker container ls --all.

Here’s a quick way to parse out just the started and finished attributes:

docker container inspect <container> | grep dAt
          "StartedAt": "2023-12-09T18:24:38.502596208Z",
          "FinishedAt": "2023-12-09T18:52:07.907443462Z",

Alternatively if you plan to script this out or maybe create an alias you can use Go’s template filtering to control how these attributes are output:

docker container inspect <container> --format "{{ .State.StartedAt }},{{ .State.FinishedAt }}"

2023-12-09T18:24:38.502596208Z,2023-12-09T18:52:07.907443462Z

Overall this could be useful if you have 1 or more long running tasks that you plan to measure but you don’t want to wait around for hours to see exactly when it finishes. Or, maybe you forgot to use the time command, or you have too many commands running in parallel with & and time’s output will be too confusing to know which one is for which command.

This happened to me recently where I was doing a database dump / import on a 450 GB database. If you’re curious, it ended up taking 2.5 hours to dump and 3.5 hours to import. I ran it on a 4 CPU core / 8 GB of memory / SSD VPS.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments