3

I'm having docker containers in Linux server, I need know how to get details of applications, services and its status inside the containers. Which command do I need to execute to get those details.

For example:

A container contains tomcat server. I need to get the details of tomcat server and its status(whether the service is running or not).

I don't know is there any command available to get those details.

Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Kavi Chinna
  • 237
  • 2
  • 7
  • 18

5 Answers5

4

you can use the docker exec command to execute any command you need inside the container.

For instance, to list all running processes inside a container:

docker exec <my container> ps aux

or to display the content of a file

docker exec <my container> cat /etc/resolv.conf

Those commands will be executed with the user defined in your image. You can override it with the -u option:

docker exec -u root <my container> ls -l

If you need to run multiple commands, or use environment variables that exists within the container, use the following trick:

docker exec <my container> sh -c 'echo ~; echo "PATH: $PATH"'
Thomasleveil
  • 95,867
  • 15
  • 119
  • 113
4

LIST all containers:

docker stats $(docker ps|grep -v "NAMES"|awk '{ print $NF }'|tr "\n" " ") --no-stream

LIST containers with memory usage more than 99%:

docker stats $(docker ps|grep -v "NAMES"|awk '{ print $NF }'|tr "\n" " ") --no-stream | sort -k 8n | awk '(NR>1) && ($8 > 99 ) '

awh112
  • 1,466
  • 4
  • 22
  • 34
Jay T
  • 41
  • 1
3

Try docker top:

docker top --help

Usage: docker top [OPTIONS] CONTAINER [ps OPTIONS]

Display the running processes of a container

  --help=false       Print usage

For example:

$ docker run -itd --name top-test debian
abf71bd18ac8d93684be17326f7a3be2cbea9103eca75391201d8e7a1ecde6e6
$ docker top top-test
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                1155                1001                0                   09:50               pts/1               00:00:00            /bin/bash

Example with postgresql:

$ docker run -d --name pg postgres
1798f94c8ce9fe279036ada6fed119f60b7c2363c5ab3a4752cd3a404e3ffd92
$ docker top pg
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
999                 3916                1001                0                   13:23               ?                   00:00:00            postgres
999                 3993                3916                0                   13:23               ?                   00:00:00            postgres: checkpointer process
999                 3994                3916                0                   13:23               ?                   00:00:00            postgres: writer process
999                 3995                3916                0                   13:23               ?                   00:00:00            postgres: wal writer process
999                 3996                3916                0                   13:23               ?                   00:00:00            postgres: autovacuum launcher process
999                 3997                3916                0                   13:23               ?                   00:00:00            postgres: stats collector process

Docker top is actually just running ps behind the scenes, so we can pass args:

$ docker top pg -e -o pid,cmd
PID                 CMD
3916                postgres
3993                postgres: checkpointer process
3994                postgres: writer process
3995                postgres: wal writer process
3996                postgres: autovacuum launcher process
3997                postgres: stats collector process

In general, you will find only a single process is running. Welcome to the world of containers.

Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
  • I don't want processes details, I need services details. Okay if from your point, In my container contains more than two services for example Tomcat, MSSQL server and etc.. I need to get all the services details, from your result how can I find Which process is tomcat and which process is MSSQL? – Kavi Chinna Sep 03 '15 at 11:10
  • It's just raw output from ps. Scroll the output to the right to see the command name. I've added an example with postgres for a bit more context. I don't really know what you mean by services. You can also get process "trees" if that helps. – Adrian Mouat Sep 03 '15 at 13:31
  • Services means tomcat, mssql, etc., which are running inside docker. – Kavi Chinna Sep 04 '15 at 05:57
  • what is the meaning of CMD in your output? Is it mean the name of the services which is running in the container? – Kavi Chinna Sep 04 '15 at 05:57
  • This is basic linux/unix stuff you're asking. It's the name of the command used to launch the process http://www.binarytides.com/linux-ps-command/. That will list everything that is running in the container, yes. Be careful of using the word service, as it can mean different things. Learn what a process is instead. – Adrian Mouat Sep 04 '15 at 07:48
  • Is it possible to use the below command inside the docker container? "service --status-all". – Kavi Chinna Sep 04 '15 at 12:43
  • This is why I kept asking what you meant by "service". In this case, you are talking about start-up services, as provided by upstart or systemd etc. You can install something like supervisord or runit to do this, but it does not come installed by default. Anyway, I really think you're better off understanding ps. – Adrian Mouat Sep 04 '15 at 14:10
  • service means tomcat is a service and mssql is a service. My question is how to get those service details from docker containers? Usually, we cant get the services details in linux by using the command "service --status-all", but how to service details which are running in the docker container? – Kavi Chinna Sep 11 '15 at 09:24
  • Like I said, "service" means you are running upstart, systemd or similar. Most Docker images don't include this, therefore there is no "service" command. Either use ps or docker top instead, which will tell you the same info, or install a process manager yourself. – Adrian Mouat Sep 11 '15 at 09:41
  • No, you must understand @KaviChinna in context here. He/she is speaking about Docker services, not upstart or systems or similar. How would you get hold of docker service status (say, of a swarm) from within a container? – Rads Dec 03 '20 at 05:46
1

The command

docker inspect

looks like what you want, check the doc

https://docs.docker.com/reference/commandline/inspect/

For getting docker information from inside a container, see

Docker, how to get container information from within the container?

Community
  • 1
  • 1
user2915097
  • 30,758
  • 6
  • 57
  • 59
  • No, Its not what I need. It's giving details about the container. I need to get details about what are the services and applications container contains. – Kavi Chinna Sep 03 '15 at 07:55
  • if you `docker exec -it your_container bash` your are inside your container, I guess you can enter the needed commands – user2915097 Sep 03 '15 at 08:19
  • No, @user2915097 - how would you get to know about all docker services from within one of the containers of a docker service? – Rads Dec 03 '20 at 05:47
  • check this answer for more info https://serverfault.com/questions/1030732/how-to-know-how-the-docker-container-is-started – St. Jan Apr 12 '23 at 11:04
0

I think the simplest answer (since from the question the OP seems to be familiar with the applications, services, and status already) is to enter the docker container and run those familiar commands in the context where they work.

That is, if your container is called app go to the folder on your machine where you have installed docker and run ./launcher enter app

This will put you inside the container and you can run service apache2 status for example to see the status of Apache2 running on your container.