简体   繁体   中英

Is there a command where we can see how long the docker container has been running?

Is there a command where we can see how long the docker container has been running? I'm not talking about typing docker ps and looking from there. It just needs to show the runtime (like up 13 minutes).

I don't believe there's a built-in docker CLI for this specific output. But the data you need to create your own is there. You can inspect the container with:

$ docker container inspect --format '{{ .State.StartedAt }}' $container_id
2022-08-15T23:10:01.630669597Z

You can parse that into a time, subtract it from the current time for the duration, and output that with your language of choice.

If you are automating this with unknown containers, I'd validate the input to be sure the containers really are running. You can capture the entire state in JSON with:

$ docker container inspect --format '{{ json .State }}' $container_id
{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":12015,"ExitCode":0,"Error":"","StartedAt":"2022-08-15T23:10:01.630669597Z","FinishedAt":"0001-01-01T00:00:00Z"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM