I'm playing with docker software following this link: . There are two points which confused me.

1: to avoid typing sudo each time, the author mentioned that can add the user into Docker user group by the following command:

$ sudo usermod -aG docker $USER 

I tried this in my computer, instead of using $USER, I use $myusername, my user name is my login user name in my computer. Then it failed... haha. I guess I need to use $USER instead.

2: I tried to run a ubuntu docker container with

$ sudo docker container run -it ubuntu bash 

Then I do not know how to kill it.... haha... because I do not know the container ID and it seemed like I was in the ubuntu mode and can not get back. The following is the output.

:~$ sudo docker container run -it ubuntu bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 7ddbc47eeb70: Pull complete c1bbdc448b72: Pull complete 8c3b70e39044: Pull complete 45d437916d57: Pull complete Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d Status: Downloaded newer image for ubuntu:latest root@c3bfdf5922ea:/# ls bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr root@c3bfdf5922ea:/# cd lib64 root@c3bfdf5922ea:/lib64# ls ld-linux-x86-64.so.2 root@c3bfdf5922ea:/lib64# cd .. root@c3bfdf5922ea:/# cd home root@c3bfdf5922ea:/home# ls root@c3bfdf5922ea:/home# mkdir jack root@c3bfdf5922ea:/home# ls jack root@c3bfdf5922ea:/home# cd .. root@c3bfdf5922ea:/# cd sys root@c3bfdf5922ea:/sys# ls block bus class dev devices firmware fs hypervisor kernel module power root@c3bfdf5922ea:/sys# cd kernel root@c3bfdf5922ea:/sys/kernel# ls boot_params fscaps kexec_crash_size notes security uevent_seqnum cgroup iommu_groups kexec_loaded profiling slab vmcoreinfo config irq livepatch rcu_expedited tracing debug kexec_crash_loaded mm rcu_normal uevent_helper root@c3bfdf5922ea:/sys/kernel# cd root@c3bfdf5922ea:~# sudo docker container kill bash: sudo: command not found root@c3bfdf5922ea:~# sudo docker container kill 1 bash: sudo: command not found root@c3bfdf5922ea:~# docker container kill 0 bash: docker: command not found root@c3bfdf5922ea:~# sudo docker container kill 0 bash: sudo: command not found root@c3bfdf5922ea:~# quit bash: quit: command not found root@c3bfdf5922ea:~# quit() > > sudo docker container kill bash: syntax error near unexpected token `sudo' 

EDIT: for question 2, I pressed control + d and accidentally get back to the original ubuntu terminal. Yeah. But still got no lucks when trying to kill the ubuntu container. The following is what I tried out.

root@c3bfdf5922ea:~# exit :~$ docker container kill 0 Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post dial unix /var/run/docker.sock: connect: permission denied :~$ sudo docker container kill 0 [sudo] password for devel: Error response from daemon: Cannot kill container: 0: No such container: 0 :~$ sudo docker container kill 1 Error response from daemon: Cannot kill container: 1: No such container: 1 :~$ sudo docker container kill "docker container kill" requires at least 1 argument. See 'docker container kill --help'. Usage: docker container kill [OPTIONS] CONTAINER [CONTAINER...] Kill one or more running containers :~$ sudo docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 775349758637 3 days ago 64.2MB hello-world latest fce289e99eb9 10 months ago 1.84kB :~$ sudo docker container kill 775349758637 Error response from daemon: Cannot kill container: 775349758637: No such container: 775349758637 

EDIT AGAIN: for queston 1, I tried the command with $USER and it did not work. For question 2, I tried the command with sudo docker container ls -all, and I guess the number after root is the container ID, and tried again with no lucks.

:~$ sudo docker container ls -all CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c3bfdf5922ea ubuntu "bash" 33 minutes ago Exited (0) 11 minutes ago adoring_villani 

more trials:

:~$ sudo usermod -aG docker $USER :~$ docker container run -it ubuntu bash docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post dial unix /var/run/docker.sock: connect: permission denied. See 'docker run --help'. :~$ sudo docker container run -it ubuntu bash root@ff9c3f3e1a2c:/# sudo docker container kill ff9c3f3e1a2c bash: sudo: command not found root@ff9c3f3e1a2c:/# docker container kill ff9c3f3e1a2c bash: docker: command not found root@ff9c3f3e1a2c:/# 
1

1 Answer

The image id isn't the same as the container id. Use the docker ps command to list containers:

docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 841eff335225 ubuntu "bash" 4 seconds ago Exited (0) 2 seconds ago myubuntu 

(-a also lists exited containers)

In most docker commands you can use the id (841eff335225) or the name (myubuntu). The id itself is the beggining of the container "long id" (any length will do, but short lengths may be ambiguous, 6+ is usually enough).

You can assign a name (must be unique) to a container by using --name <somename> in the docker run command. Otherwise the container gets a random name (adjective+science/computer celebrity).

Notes

  • The docker command should of course be used outside the container... so exit the container before killing it, or kill it from another terminal.
  • docker ps -q returns just the ids, so this can be used to generate parameters to other Docker commands: docker kill $(docker ps -q) will kill all running containers.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy