Docker Commands one must be aware of
List running containers -
root@kmaster-rj:~# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
List all containers -
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a50ac4059f3b nginx “nginx -g ‘daemon of…” 24 hours ago Exited (0) 17 hours ago flamboyant_heyrovsky
List Images -
root@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB
Create a Container and run a command -
root@kmaster-rj:~# docker container run ubuntu cat /etc/os-release
NAME=”Ubuntu”
VERSION=”20.04 LTS (Focal Fossa)”
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME=”Ubuntu 20.04 LTS”
VERSION_ID=”20.04"
HOME_URL=”https://www.ubuntu.com/"
SUPPORT_URL=”https://help.ubuntu.com/"
BUG_REPORT_URL=”https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL=”https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
Stop containers -
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1351faa3bdb ubuntu “bash” About a minute ago Up About a minute hardcore_lehmannroot@kmaster-rj:~# docker container stop e1351faa3bdb
e1351faa3bdb
Start a container -
root@kmaster-rj:~# docker container start e1351faa3bdb
e1351faa3bdb
Remove a container -
root@kmaster-rj:~# docker container rm e1351faa3bdb
e1351faa3bdb
Remove all the containers -
root@kmaster-rj:~# docker container rm $(docker container ls -aq)
b12eb5305e3b
0aa3471d9118
d8274deaf681
Run Container in detached mode -
root@kmaster-rj:~# docker container run -d ubuntu
29cd722f013c755f18edd34559b2ef9e7218d5e36cd6acf014d003691aa53b0broot@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29cd722f013c ubuntu “/bin/bash” 5 seconds ago Exited (0) 5 seconds ago sleepy_fermiroot@kmaster-rj:~# docker container run -d -it nginx bash
71734b5b071f119de2f8402c79199c82a262ce2962424bd996317d22f99c618croot@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71734b5b071f nginx “bash” 4 seconds ago Up 2 seconds 80/tcp epic_goldwasser
29cd722f013c ubuntu “/bin/bash” 51 seconds ago Exited (0) 50 seconds ago sleepy_fermi
Go inside a running container -
root@kmaster-rj:~# docker exec -it 71734b5b071f /bin/bash
root@71734b5b071f:/#
Inspect a running container -
To check IP of container, hostname, exposed service ports, status etc etc ..
root@kmaster-rj:~# docker inspect 71734b5b071f
Running Ngnix container and access it ->
root@kmaster-rj:~# docker container run -d -it nginx
9c7d401c7eb4b9dfee5989f458d62cc6a6fd4e115dc58cb6e35eb9ac53712195root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9c7d401c7eb4 nginx “nginx -g ‘daemon of…” 3 seconds ago Up 1 second 80/tcp practical_shannonroot@kmaster-rj:~# curl 172.17.0.2
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
Check the logs of Container ->
root@kmaster-rj:~# docker container logs 9c7d401c7eb4
172.17.0.1 — — [03/Jun/2020:10:29:40 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.58.0” “-”
172.17.0.1 — — [03/Jun/2020:10:30:29 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.58.0” “-”
Check the processes running inside a container ->
root@kmaster-rj:~# docker container top 9c7d401c7eb4
UID PID PPID C STIME TTY TIME CMD
root 4429 4406 0 10:29 pts/0 00:00:00 nginx: master process nginx -g daemon off;
systemd+ 4479 4429 0 10:29 pts/0 00:00:00 nginx: worker process
On host machine if you do ps -aux you will see container processes list as well.
root@kmaster-rj:~# ps -aux | grep nginx
root 4429 0.1 0.1 10632 5120 pts/0 Ss+ 10:29 0:00 nginx: master process nginx -g daemon off;
systemd+ 4479 0.0 0.0 11088 2604 pts/0 S+ 10:29 0:00 nginx: worker process
To check CPU and Memory usage of docker container -
root@kmaster-rj:~# docker container stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
06e4d249ed5a focused_boyd 0.00% 2.219MiB / 3.852GiB 0.06% 726B / 0B 0B / 0B 2
98a0491ba483 brave_lamport 0.00% 2.125MiB / 3.852GiB 0.05% 726B / 0B 0B / 0B 2
9c7d401c7eb4 practical_shannon 0.00% 2.215MiB / 3.852GiB 0.06% 2.37kB / 2.55kB 4.69MB / 0B 2
Expose the service or do port forwarding ->
root@kmaster-rj:~# docker run -d -p 3600:80 — name mywebserver nginx
65405cf4963c59308ccd3d219ee9140bd8a8213740a2829b3c2a2e27ea05e0fd
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 5 seconds ago Up 3 seconds 0.0.0.0:3600->80/tcp mywebserverroot@kmaster-rj:~# netstat -tulpn | grep 3600tcp6 0 0 :::3600 :::* LISTEN 5356/docker-proxy
Verify on Host Windows Machine -
Administrator@DESKTOP-IEH4IQ8 MINGW64 ~
$ curl.exe 172.42.42.200:3600
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
Install software's inside a container ->
root@kmaster-rj:~# docker container exec -it 65405cf4963c /bin/bash
root@65405cf4963c:/# apt-get install nmap
Rename container ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 7 minutes ago Up 7 minutes 0.0.0.0:3600->80/tcp mywebserverroot@kmaster-rj:~# docker container rename 65405cf4963c newwebserver
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 7 minutes ago Up 7 minutes 0.0.0.0:3600->80/tcp newwebserver
Restart a Container ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 8 minutes ago Up 8 minutes 0.0.0.0:3600->80/tcp newwebserverroot@kmaster-rj:~# docker container restart 65405cf4963c
65405cf4963croot@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 8 minutes ago Up 1 second 0.0.0.0:3600->80/tcp newwebserver
Attaching to a running container ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
65405cf4963c nginx “nginx -g ‘daemon of…” 8 minutes ago Up 1 second 0.0.0.0:3600->80/tcp newwebserver
root@kmaster-rj:~# docker container attach 654
172.42.42.1 — — [03/Jun/2020:11:00:39 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36” “-”
172.42.42.1 — — [03/Jun/2020:11:00:40 +0000] “GET / HTTP/1.1” 304 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36” “-”
Attach to a ubuntu container -
root@kmaster-rj:~# docker run -d -it ubuntu /bin/bash
2352d0afe1e087c1dc7a906df2ca15848d19147fdd132494f022617bf13d3960root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2352d0afe1e0 ubuntu “/bin/bash” 2 seconds ago Up 1 second focused_lehmann
65405cf4963c nginx “nginx -g ‘daemon of…” 12 minutes ago Exited (0) 24 seconds ago newwebserverroot@kmaster-rj:~# docker container attach 235
root@2352d0afe1e0:/#
Kill a container ->
root@kmaster-rj:~# docker container kill 2352d0afe1e0
2352d0afe1e0root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2352d0afe1e0 ubuntu “/bin/bash” 3 minutes ago Exited (137) 1 second ago focused_lehmann
65405cf4963c nginx “nginx -g ‘daemon of…” 16 minutes ago Exited (0) 3 minutes ago newwebserver
Pause a running container ->
root@kmaster-rj:~# docker container pause 2352d0afe1e0root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2352d0afe1e0 ubuntu “/bin/bash” 5 minutes ago Up 36 seconds (Paused) focused_lehmann
65405cf4963c nginx “nginx -g ‘daemon of…” 18 minutes ago Exited (0) 5 minutes ago newwebserverroot@kmaster-rj:~# docker container exec -it 235 /bin/bash
Error response from daemon: Container 235 is paused, unpause the container before exec
Unpause a container ->
root@kmaster-rj:~# docker container unpause 235
235
root@kmaster-rj:~# docker container exec -it 235 /bin/bashroot@2352d0afe1e0:/# read escape sequence
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2352d0afe1e0 ubuntu “/bin/bash” 7 minutes ago Up 2 minutes focused_lehmann
Kill all stopped containers ->
root@kmaster-rj:~# docker container stop $(docker container ls -aq)
2352d0afe1e0
65405cf4963croot@kmaster-rj:~# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
2352d0afe1e087c1dc7a906df2ca15848d19147fdd132494f022617bf13d3960
65405cf4963c59308ccd3d219ee9140bd8a8213740a2829b3c2a2e27ea05e0fdTotal reclaimed space: 17.42MBroot@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
To know the container port mapping ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a3090216b4b nginx “nginx -g ‘daemon of…” 21 seconds ago Up 19 seconds 0.0.0.0:3600->80/tcp mywebserverroot@kmaster-rj:~# docker container port mywebserver
80/tcp -> 0.0.0.0:3600
Copy files/directories from host machine to container ->
root@kmaster-rj:~# docker container cp copydir/ 8263421a3d64:/tmp
Verify inside the container ->
root@8263421a3d64:/tmp# ls -la /tmp/
total 20
drwxrwxrwt 1 root root 4096 Jun 3 11:36 .
drwxr-xr-x 1 root root 4096 Jun 3 11:36 ..
drwxr-xr-x 2 root root 4096 Jun 3 11:36 copydir
Check the differences between the base docker image and a container ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8263421a3d64 ubuntu “/bin/bash” 5 minutes ago Up 5 minutes boring_matsumoto
6a3090216b4b nginx “nginx -g ‘daemon of…” 20 minutes ago Up 20 minutes 0.0.0.0:3600->80/tcp mywebserverroot@kmaster-rj:~# docker container diff boring_matsumoto
C /tmp
A /tmp/a
A /tmp/b
A /tmp/c
A /tmp/copydir
A /tmp/test
Export a container and save it as image file ->
root@kmaster-rj:~# docker container export 7b05b6a5301d > myubuntu_tree_git.tar
or
root@kmaster-rj:~# docker container export 7b05b6a5301d -o myubuntu_tree_git1.tarroot@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu_tree_git latest f8547a2d9aaa 3 minutes ago 222MB
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB
Import the container exported file as an image ->
root@kmaster-rj:~# docker image import myubuntu_tree_git.tar myubuntu_tree_gitroot@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myubuntu_tree_git latest f8547a2d9aaa 3 minutes ago 222MB
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MBroot@kmaster-rj:~# docker container run -it myubuntu_tree_git /bin/bash
Create an Image out of a Running Container ->
root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e25409eb8a06 ubuntu “/bin/bash” About a minute ago Up About a minute thirsty_bhaskararoot@kmaster-rj:~# docker container commit — author “Rakesh Jain” — message “This is test commit” e25409eb8a06 my_test_image
sha256:5aa5926556348bb66317e37220235729d1778337831e99b859ceca08d6de6499root@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
my_test_image latest 5aa592655634 3 seconds ago 73.9MB
myubuntu_tree_git latest f8547a2d9aaa 9 minutes ago 222MB
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MBroot@kmaster-rj:~# docker container stop e25409eb8a06
e25409eb8a06root@kmaster-rj:~# docker container rm $(docker container ls -aq)
e25409eb8a06root@kmaster-rj:~# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESroot@kmaster-rj:~# docker container run -it my_test_image /bin/bash
root@8be8fb7acb94:/# cd /tmp
root@8be8fb7acb94:/tmp# ls
1 2 3 4 5 6 7 8 9
Docker Push Image to dockerhub ->
root@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
my_test_image latest 5aa592655634 6 minutes ago 73.9MB
myubuntu_tree_git latest f8547a2d9aaa 15 minutes ago 222MB
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB
ubuntu 14.04 6e4f1fe62ff1 5 months ago 197MB
root@kmaster-rj:~# docker image tag myubuntu_tree_git rakeshrhcss/myubuntu_tree_gitroot@kmaster-rj:~# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
my_test_image latest 5aa592655634 11 minutes ago 73.9MB
myubuntu_tree_git latest f8547a2d9aaa 20 minutes ago 222MB
rakeshrhcss/myubuntu_tree_git latest f8547a2d9aaa 20 minutes ago 222MB
nginx latest 9beeba249f3e 2 weeks ago 127MB
ubuntu latest 1d622ef86b13 5 weeks ago 73.9MB
ubuntu 14.04 6e4f1fe62ff1 5 months ago 197MBroot@kmaster-rj:~# docker push rakeshrhcss/myubuntu_tree_git
The push refers to repository [docker.io/rakeshrhcss/myubuntu_tree_git]
f5409e861143: Preparing
denied: requested access to the resource is deniedroot@kmaster-rj:~# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don’t have a Docker ID, head over to https://hub.docker.com to create one.
Username: rakeshrhcss
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeededroot@kmaster-rj:~# docker push rakeshrhcss/myubuntu_tree_git
The push refers to repository [docker.io/rakeshrhcss/myubuntu_tree_git]
f5409e861143: Pushing [=============================> ] 129.5MB/221.6MB
f5409e861143: Pushed
latest: digest: sha256:5fb6d00c74cd5f4bcae3cac36556bdfa28063fbb97b83565f9eb89d5ee4b0aa9 size: 528
Hope you like the tutorial. Please let me know your feedback in the response section.
Happy learning!