Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all the processes running inside a Docker container?

Tags:

docker

logging

After having logged into the container using the command -

docker exec -it <container_name>

How do I check for all the processes running inside the container? Is "ps aux" the correct way to do it? Are there any better alternatives/approaches?

like image 494
Always_Beginner Avatar asked Oct 28 '16 04:10

Always_Beginner


People also ask

How do I see what processes are running in a docker container?

Find the running container's ID by using the docker ps command. Find the PID number of the first process in the running container by running the docker inspect command. Enter the running container by using the nsenter command.

Can you see the processes running inside a container from the outside?

Yes this is quite normal, the pid inside the container, or, atleast the MAIN pid will always be 1. But since docker uses the kernel on the host, and not its own, you will see it in ps command on the host.


3 Answers

You can use dedicated command top to list process in docker container, regardless of the operating system in container.

docker top <container>
like image 183
Slawomir Jaranowski Avatar answered Oct 11 '22 00:10

Slawomir Jaranowski


It is possible to show all the processes running inside a container without login to terminal by using the following command. Of course, it is just like how one can see by using ps -eaf, so just add it to docker exec.

bash $ sudo docker exec -it test1 ps -eaf
PID   USER     TIME   COMMAND
    1 root       0:00 sh
    7 root       0:00 sh
   60 root       0:00 /bin/sh
   67 root       0:00 /bin/sh
   84 root       0:00 ps -eaf

Like it was mentioned, if you are already inside of a container, then just use ps -eaf command to see the running processes.

By the way, it is recommended to have one user application / process per container.

like image 27
Rao Avatar answered Oct 10 '22 22:10

Rao


Extending from the answer of @Slawomir

And With ps option, docker top [--help] CONTAINER [ps OPTIONS]

docker top <container_id> -eo pid,cmd

like image 11
sais Avatar answered Oct 10 '22 23:10

sais