Search for image in Docker Hub:
[root@docker1 ~]# docker search centos | head -n2
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 4297 [OK]
Pull image from Docker Hup:
[root@docker1 ~]# docker pull centos
List available docker images:
[root@docker1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 452a96d81c30 3 weeks ago 79.6 MB
docker.io/hello-world latest e38bc07ac18e 5 weeks ago 1.85 kB
docker.io/centos latest e934aafc2206 6 weeks ago 199 MB
List how many containers are running now:
[root@docker1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Show ALL containers even the stopped ones:
[root@docker1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6ce4aa30164d ubuntu "bash" 17 minutes ago Exited (0) 16 minutes ago kickass_payne
33f58ebbb82e hello-world "/hello" 17 minutes ago Exited (0) 17 minutes ago clever_beaver
Run docker container and attach to its console, then terminate it once you logged out:
root@docker1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 452a96d81c30 3 weeks ago 79.6 MB
docker.io/hello-world latest e38bc07ac18e 5 weeks ago 1.85 kB
docker.io/centos latest e934aafc2206 6 weeks ago 199 MB
[root@docker1 ~]# docker help run | grep -E -e "[[:space:]]-i" -e "[[:space:]]-t" -e "--name"
-i, --interactive Keep STDIN open even if not attached
--name string Assign a name to the container
-t, --tty Allocate a pseudo-TTY
[root@docker1 ~]# docker run --name centos1 -i -t centos /bin/bash
[root@766f948a89d6 /]# hostname
766f948a89d6
[root@766f948a89d6 /]# exit
exit
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
766f948a89d6 centos "/bin/bash" 54 seconds ago Exited (0) 41 seconds ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" 23 minutes ago Exited (0) 23 minutes ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago clever_beaver 0 B (virtual 1.85 kB)
Run docker container and de-attach form its console without powering it off:
[root@docker1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 452a96d81c30 3 weeks ago 79.6 MB
docker.io/hello-world latest e38bc07ac18e 5 weeks ago 1.85 kB
docker.io/centos latest e934aafc2206 6 weeks ago 199 MB
[root@docker1 ~]# docker run --name centos2 -i -t centos /bin/bash
[root@e9675771f1f2 /]# hostname
e9675771f1f2
[root@e9675771f1f2 /]# [CTRL+p}[CTRL+1] [root@docker1 ~]# hostname
docker1.ab.lab
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 46 seconds ago Up 45 seconds centos2 0 B (virtual 199 MB)
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 53 seconds ago Up 52 seconds centos2 0 B (virtual 199 MB)
766f948a89d6 centos "/bin/bash" 9 minutes ago Exited (0) 3 minutes ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" 32 minutes ago Exited (0) 31 minutes ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" 32 minutes ago Exited (0) 32 minutes ago clever_beaver 0 B (virtual 1.85 kB)
Attach to current running container:
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 5 minutes ago Up 5 minutes centos2 0 B (virtual 199 MB)
[root@docker1 ~]# docker attach e9675771f1f2
[root@e9675771f1f2 /]# hostname
e9675771f1f2
Start non-working container:
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 14 minutes ago Up 2 minutes centos2 217 MB (virtual 416 MB)
766f948a89d6 centos "/bin/bash" 23 minutes ago Exited (0) 17 minutes ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" 46 minutes ago Exited (0) 45 minutes ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" 46 minutes ago Exited (0) 46 minutes ago clever_beaver 0 B (virtual 1.85 kB)
[root@docker1 ~]# docker start 766f948a89d6
766f948a89d6
[root@docker1 ~]# docker attach 766f948a89d6
[root@766f948a89d6 /]# hostname
766f948a89d6
Stop current working container:
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 17 minutes ago Up 5 minutes centos2 217 MB (virtual 416 MB)
766f948a89d6 centos "/bin/bash" 26 minutes ago Up 2 minutes centos1 14 B (virtual 199 MB)
[root@docker1 ~]# docker stop 766f948a89d6
766f948a89d6
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 18 minutes ago Up 5 minutes centos2 217 MB (virtual 416 MB)
Kill unresponsive container:
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 18 minutes ago Up 5 minutes centos2 217 MB (virtual 416 MB)
[root@docker1 ~]# docker kill e9675771f1f2
e9675771f1f2
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
Remove container:
Container must be Powered off
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
e9675771f1f2 centos "/bin/bash" 28 minutes ago Exited (137) 12 seconds ago centos2 217 MB (virtual 416 MB)
766f948a89d6 centos "/bin/bash" 37 minutes ago Exited (137) 26 seconds ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" 59 minutes ago Exited (0) 59 minutes ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" About an hour ago Exited (0) About an hour ago clever_beaver 0 B (virtual 1.85 kB)
[root@docker1 ~]# docker rm e9675771f1f2
e9675771f1f2
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
766f948a89d6 centos "/bin/bash" 37 minutes ago Exited (137) 50 seconds ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" About an hour ago Exited (0) 59 minutes ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" About an hour ago Exited (0) About an hour ago clever_beaver 0 B (virtual 1.85 kB)
Remove specific image:
You must remove the containers used it first
[root@docker1 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/ubuntu latest 452a96d81c30 3 weeks ago 79.6 MB
docker.io/hello-world latest e38bc07ac18e 5 weeks ago 1.85 kB
docker.io/centos latest e934aafc2206 6 weeks ago 199 MB
[root@docker1 ~]# docker rmi hello-world
Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container 33f58ebbb82e is using its referenced image e38bc07ac18e
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
766f948a89d6 centos "/bin/bash" 49 minutes ago Exited (137) 12 minutes ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" About an hour ago Exited (0) About an hour ago kickass_payne 0 B (virtual 79.6 MB)
33f58ebbb82e hello-world "/hello" About an hour ago Exited (0) About an hour ago clever_beaver 0 B (virtual 1.85 kB)
[root@docker1 ~]# docker rm 33f58ebbb82e
33f58ebbb82e
[root@docker1 ~]# docker rmi hello-world
Untagged: hello-world:latest
Untagged: docker.io/hello-world@sha256:f5233545e43561214ca4891fd1157e1c3c563316ed8e237750d59bde73361e77
Deleted: sha256:e38bc07ac18ee64e6d59cf2eafcdddf9cec2364dfe129fe0af75f1b0194e0c96
Deleted: sha256:2b8cbd0846c5aeaa7265323e7cf085779eaf244ccbdd982c4931aef9be0d2faf
Execute commands into container without logging:
[root@docker1 ~]# docker ps -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
[root@docker1 ~]# docker ps -a -s
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
766f948a89d6 centos "/bin/bash" About an hour ago Exited (137) 28 minutes ago centos1 14 B (virtual 199 MB)
6ce4aa30164d ubuntu "bash" About an hour ago Exited (0) About an hour ago kickass_payne 0 B (virtual 79.6 MB)
[root@docker1 ~]# docker start centos1
centos1
[root@docker1 ~]# docker exec centos1 cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@docker1 ~]# docker exec centos1 yum update -y
[root@docker1 ~]# docker exec centos1 cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)