How to kill process inside container? Docker top command

68,596

Solution 1

When I reproduce your situation I see different PIDs between docker top <container> and docker exec -it <container> ps -aux. When you do docker exec the command is executed inside the container => should use container's pid. Otherwise you could do the kill without docker straight from the host, in your case: sudo kill -9 25055.

Solution 2

check this:

ps | grep -i a66 | tr -s ' '|cut -f2 -d' '|
{
    while read line;
    do kill -9 $line;
    done
}

to understand this start from executing commands from left till end of each pipe (|)

Simpler option:

kill $(pidof a66) 
Share:
68,596
Timur Fayzrakhmanov
Author by

Timur Fayzrakhmanov

Updated on April 26, 2021

Comments

  • Timur Fayzrakhmanov
    Timur Fayzrakhmanov about 3 years

    I have simple example from official guide at docker website.

    I run the following:

    sudo docker run -d ubuntu:latest /bin/sh -c "while true; do echo hello world; sleep 1; done"
    a66asdasdhqie123...
    

    Then take some output from created container:

    sudo docker logs a66
    hello
    hello
    hello
    ...
    

    Then I lookup the running processes of a container:

    sudo docker top a66
    UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
    root                25055               15152               0                   20:07               ?                   00:00:00            /bin/sh -c while true; do echo hello world; sleep 1; done
    root                25295               25055               0                   20:10               ?                   00:00:00            sleep 1
    

    Next I try to kill the first process of container:

    sudo docker exec a66 kill -9 25055
    

    However after I make it nothing changes. Process still works and output "hello" every second. What do I wrong?