Run a container
Ex. docker container run <image> <app>
Ex. docker container run -it ubuntu /bin/bash
-it will connect to current terminal
Stop container: docker container stop
Start container: docker container start
# Originally there are 8 containers
isaac@isaac-KVM:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71c2bac3e63f ubuntu "/bin/bash" 2 minutes ago Exited (127) 30 seconds ago dreamy_neumann
5a6744d26f4d ubuntu "/bin/sh" 3 minutes ago Exited (0) 2 minutes ago nostalgic_swanson
0f0ddaa9c4f4 nginx "nginx -g 'daemon of…" 3 minutes ago Exited (0) 3 minutes ago cranky_turing
5a8dfb31e1b5 nginx "nginx -g 'daemon of…" 4 minutes ago Exited (0) 4 minutes ago quirky_newton
20a69a2f40b0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago sharp_zhukovsky
28a62f8da2d0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago affectionate_curran
4df4b2d4c69e ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago gracious_jones
81977c50304b ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago cocky_poincare
isaac@isaac-KVM:~$
# Start new container
isaac@isaac-KVM:~$ docker container run -it ubuntu /bin/bash
root@1bbcd62b047b:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@1bbcd62b047b:/# exit
exit
# Now there are 9 containers
isaac@isaac-KVM:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bbcd62b047b ubuntu "/bin/bash" 9 seconds ago Exited (0) 4 seconds ago blissful_turing
71c2bac3e63f ubuntu "/bin/bash" 2 minutes ago Exited (127) About a minute ago dreamy_neumann
5a6744d26f4d ubuntu "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago nostalgic_swanson
0f0ddaa9c4f4 nginx "nginx -g 'daemon of…" 4 minutes ago Exited (0) 4 minutes ago cranky_turing
5a8dfb31e1b5 nginx "nginx -g 'daemon of…" 5 minutes ago Exited (0) 5 minutes ago quirky_newton
20a69a2f40b0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago sharp_zhukovsky
28a62f8da2d0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago affectionate_curran
4df4b2d4c69e ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago gracious_jones
81977c50304b ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago cocky_poincare
# No container alive, because the latest process was killed by exit
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
isaac@isaac-KVM:~$
isaac@isaac-KVM:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bbcd62b047b ubuntu "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago blissful_turing
71c2bac3e63f ubuntu "/bin/bash" 14 minutes ago Exited (127) 12 minutes ago dreamy_neumann
5a6744d26f4d ubuntu "/bin/sh" 15 minutes ago Exited (0) 15 minutes ago nostalgic_swanson
0f0ddaa9c4f4 nginx "nginx -g 'daemon of…" 15 minutes ago Exited (0) 15 minutes ago cranky_turing
5a8dfb31e1b5 nginx "nginx -g 'daemon of…" 17 minutes ago Exited (0) 16 minutes ago quirky_newton
20a69a2f40b0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago sharp_zhukovsky
28a62f8da2d0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago affectionate_curran
4df4b2d4c69e ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago gracious_jones
81977c50304b ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago cocky_poincare
isaac@isaac-KVM:~$ docker container rm 81977c50304b
81977c50304b
isaac@isaac-KVM:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1bbcd62b047b ubuntu "/bin/bash" 12 minutes ago Exited (0) 11 minutes ago blissful_turing
71c2bac3e63f ubuntu "/bin/bash" 14 minutes ago Exited (127) 13 minutes ago dreamy_neumann
5a6744d26f4d ubuntu "/bin/sh" 15 minutes ago Exited (0) 15 minutes ago nostalgic_swanson
0f0ddaa9c4f4 nginx "nginx -g 'daemon of…" 16 minutes ago Exited (0) 15 minutes ago cranky_turing
5a8dfb31e1b5 nginx "nginx -g 'daemon of…" 17 minutes ago Exited (0) 17 minutes ago quirky_newton
20a69a2f40b0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago sharp_zhukovsky
28a62f8da2d0 ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago affectionate_curran
4df4b2d4c69e ubuntu:latest "/bin/bash" 2 months ago Exited (0) 2 months ago gracious_jones
isaac@isaac-KVM:~$
Kill the only one process in container also kill the container
Ctrl - PQ: exit container without terminating it
Ex. Reattach to container terminal docker container exec -it {containerid} bash
After reattach terminal and execute ps command, will see 2 processes. Because docker container exec command create new one, means exit this time will not kill container
# There is no container
dockeruser@isaac-KVM:~$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# After run, one bash process in container
dockeruser@isaac-KVM:~$ docker container run -it ubuntu /bin/bash
root@1653d3c07542:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 6.2 0.0 18504 3452 pts/0 Ss 17:33 0:00 /bin/bash
root 11 0.0 0.0 34396 2856 pts/0 R+ 17:33 0:00 ps aux
# Ctrl + PQ to exit terminal
root@1653d3c07542:/# dockeruser@isaac-KVM:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1653d3c07542 ubuntu "/bin/bash" 28 seconds ago Up 27 seconds flamboyant_ganguly
# Reattach container
dockeruser@isaac-KVM:~$ docker container exec -it 1653d3c07542 bash
# Find there are 2 processes
root@1653d3c07542:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.2 0.0 18504 3452 pts/0 Ss+ 17:33 0:00 /bin/bash
root 12 5.5 0.0 18504 3428 pts/1 Ss 17:34 0:00 bash
root 21 0.0 0.0 34396 2868 pts/1 R+ 17:34 0:00 ps aux
# So container will not terminate because there are more than one process
root@1653d3c07542:/# exit
exit
dockeruser@isaac-KVM:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1653d3c07542 ubuntu "/bin/bash" 2 minutes ago Up 2 minutes flamboyant_ganguly
dockeruser@isaac-KVM:~$
Delete container
1. docker container stop {name}
2. docker container rm {name}
Or: docker container rm {name} -f
Ex. Check docker is running: docker version
isaac@isaac-KVM:~$ docker version
Client:
Version: 18.09.7
API version: 1.39
Go version: go1.10.4
Git commit: 2d0083d
Built: Wed Jul 3 12:13:54 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.09.7
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 2d0083d
Built: Mon Jul 1 19:14:14 2019
OS/Arch: linux/amd64
Experimental: false
# add user
isaac@isaac-KVM:~$ sudo adduser dockeruser
[sudo] password for isaac:
Adding user `dockeruser' ...
Adding new group `dockeruser' (1001) ...
Adding new user `dockeruser' (1001) with group `dockeruser' ...
Creating home directory `/home/dockeruser' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for dockeruser
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
isaac@isaac-KVM:~$
# change to dockeruser, but unable to execute docker version
isaac@isaac-KVM:~$ su - dockeruser
Password:
dockeruser@isaac-KVM:~$ docker version
Client:
Version: 18.09.7
API version: 1.39
Go version: go1.10.4
Git commit: 2d0083d
Built: Wed Jul 3 12:13:54 2019
OS/Arch: linux/amd64
Experimental: false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/version: dial unix /var/run/docker.sock: connect: permission denied
dockeruser@isaac-KVM:~$
# it's because not in docker group, so need add to docker group (Notice this action need to be did by sudouser)
dockeruser@isaac-KVM:~$ usermod -aG docker dockeruser
usermod: Permission denied.
usermod: cannot lock /etc/passwd; try again later.
dockeruser@isaac-KVM:~$ sudo usermod -aG docker dockeruser
[sudo] password for dockeruser:
dockeruser is not in the sudoers file. This incident will be reported.
dockeruser@isaac-KVM:~$ su - isaac
Password:
isaac@isaac-KVM:~$ usermod -aG docker dockeruser
usermod: Permission denied.
usermod: cannot lock /etc/passwd; try again later.
isaac@isaac-KVM:~$ sudo usermod -aG docker dockeruser
# after add to docker user group, dockeruser can use docker version
isaac@isaac-KVM:~$ su - dockeruser
Password:
dockeruser@isaac-KVM:~$ docker version
Client:
Version: 18.09.7
API version: 1.39
Go version: go1.10.4
Git commit: 2d0083d
Built: Wed Jul 3 12:13:54 2019
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.09.7
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 2d0083d
Built: Mon Jul 1 19:14:14 2019
OS/Arch: linux/amd64
Experimental: false
dockeruser@isaac-KVM:~$