二、基本命令

1、镜像命令

#1、显示当前所有镜像列表 docker images
[k@localhost ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED       SIZE
hello-world   latest    48b5124b2768   9 years ago   1.84kB
--可选项
[k@localhost ~]$ docker images --help
Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -q, --quiet           Only show image IDs
  
#2、搜索镜像 docker search [镜像名]


#3、下载镜像 docker pull [镜像名:tag版本号,不写默认latest]
[k@localhost ~]$ docker pull mysql #默认下载最新版本
[k@localhost ~]$ docker pull mysql:5.7 #指定5.7版本
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete
1c56c3d4ce74: Pull complete 
e9f03a1c24ce: Pull complete 
68c3898c2015: Pull complete 
6b95a940e7b6: Pull complete 
90986bb8de6e: Pull complete 
ae71319cb779: Pull complete 
ffc89e9dfd88: Pull complete 
43d05e938198: Pull complete 
064b2d298fba: Pull complete 
df9a4d85569b: Pull complete 
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

#pull下载后多了几个不同版本的镜像
[k@localhost ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    094aec64961b   4 weeks ago    950MB
hello-world   latest    e2ac70e7319a   2 months ago   10.1kB
mysql         5.7       5107333e08a8   2 years ago    501MB
hello-world   <none>    48b5124b2768   9 years ago    1.84kB

#4、删除镜像docker rmi [镜像名/id]
#查询镜像
[k@localhost ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    094aec64961b   4 weeks ago    950MB
hello-world   latest    e2ac70e7319a   2 months ago   10.1kB
mysql         5.7       5107333e08a8   2 years ago    501MB
mysql         5.7.30    9cfcce23593a   6 years ago    448MB
hello-world   <none>    48b5124b2768   9 years ago    1.84kB

#根据镜像id删除
[k@localhost ~]$ docker rmi 094aec64961b 

Untagged: mysql:latest
Untagged: mysql@sha256:c11782aa2a96624c1efc121768641d96954faa136d6aa82751b032d8c426ffbc
Deleted: sha256:094aec64961bf03283c2c3a4dd8805dc3d61ff83dbc37eaabc30d9d3e379449c
Deleted: sha256:38d574f17a57e769357d69e74d53edbcbdb2d78a958277dc09b4285350fd42fd
Deleted: sha256:84ac56e5749bb8d340f7d5c2a24ec34f39df34b8d4f3ea1392ab2f864231f74d
Deleted: sha256:4c2b00fcefba1d9debd3c4201c6b3e466e7984a6ca56d66633ed29c80c073b0d
Deleted: sha256:9ddcf0455c2a040479c69f26c4b4c7dd66d122cc6cb7e2f8a3445e8e5ce6a2a2
Deleted: sha256:e5219dab862ca0e712d77b178601faacba5b3629b925718935129f964febd596
Deleted: sha256:049554491ccd42b2d489fc58daa7dab1e0bf8120f05dff548baf47105072eeea
Deleted: sha256:f9ea723e60fdb5efac0d14da4ed2a7866e855b970cf99fc597b0300ee40ac542
Deleted: sha256:d2005b38631b62fa7a2676f489f3e8c269141ee8c4199c2bddcfb55698e22223
Deleted: sha256:59381a7b3a1375bee10c390c241482012e23f0ff2dbbfd4375f4a2858fe489c5
Deleted: sha256:83471d529b3dfc662ce4c626e90031a0d4017d6009b548ec0d9cf3b069089b30
#已删除最新版本
[k@localhost ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    e2ac70e7319a   2 months ago   10.1kB
mysql         5.7       5107333e08a8   2 years ago    501MB
mysql         5.7.30    9cfcce23593a   6 years ago    448MB
hello-world   <none>    48b5124b2768   9 years ago    1.84kB

2、容器命令

#1、获取镜像
[k@localhost ~]$ docker pull ubuntu

[k@localhost ~]$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    094aec64961b   4 weeks ago    950MB
ubuntu        latest    30ba44506a6d   7 weeks ago    100MB
hello-world   latest    e2ac70e7319a   2 months ago   10.1kB
mysql         5.7       5107333e08a8   2 years ago    501MB
mysql         5.7.30    9cfcce23593a   6 years ago    448MB

#2、启动容器 docker run -it ubuntu /bin/bash
[k@localhost ~]$ docker run -it ubuntu
	#-d:后台运行容器,例如 docker run -d ubuntu。
	#-it:以交互式终端运行容器,例如 docker exec -it container_name bash。
	#-t:为镜像指定标签,例如 docker build -t my-image .
	#-p:指定端口
	#从k@localhost 变成 root@e5edf880ded9 即进入容器 用户@容器id
root@e5edf880ded9:/# ls  
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@e5edf880ded9:/# exit

#3、退出/停止容器 
exit退出并停止容器
ctrl+P+Q  退出不停止容器 即后台运行
#4、列出运行中的容器 docker ps
[k@localhost ~]$ docker ps #默认表示运行中的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[k@localhost ~]$ docker ps -a #-a表示所有运行过的容器
CONTAINER ID   IMAGE          COMMAND                   CREATED          STATUS                      PORTS     NAMES
e5edf880ded9   ubuntu         "/bin/bash"               8 minutes ago    Exited (0) 7 minutes ago              jovial_wozniak
9c4ce211af12   ubuntu         "-it"                     8 minutes ago    Created                               nervous_gates
216e85414f64   mysql          "docker-entrypoint.s…"   25 minutes ago   Exited (1) 25 minutes ago             vibrant_torvalds
148b91da7f83   mysql          "docker-entrypoint.s…"   25 minutes ago   Exited (1) 25 minutes ago             nifty_chatelet
deb5258918f6   mysql          "docker-entrypoint.s…"   26 minutes ago   Exited (1) 26 minutes ago             naughty_hawking
dea785b6d18f   hello-world    "/hello"                  31 minutes ago   Exited (0) 31 minutes ago             suspicious_mirzakhani
c4f787f1db83   48b5124b2768   "/hello"                  19 hours ago     Exited (0) 19 hours ago               nifty_hodgkin
5de29d6df56f   48b5124b2768   "/hello"                  19 hours ago     Exited (0) 19 hours ago               elastic_hellman
9c198263e45d   48b5124b2768   "/hello"                  2 days ago       Exited (0) 2 days ago                 wonderful_feynman
9b91129e2824   48b5124b2768   "/hello"                  2 days ago       Exited (0) 2 days ago                 silly_shamir

#5、删除容器
docker rm 容器id

#6、启动和停止容器
docker start 容器id #启动
docker restart 容器id #重启
docker stop 容器id #停止当前运行的
docker kill 容器id #强制停止

#7、打印日志
docker logs -tf 容器名

#8、进入容器 docker exec -it df6b56eafeb9 /bin/bash
[k@localhost ~]$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
df6b56eafeb9   ubuntu    "/bin/bash"   8 seconds ago   Up 7 seconds             objective_borg

[k@localhost ~]$ docker exec -it df6b56eafeb9 /bin/bash
root@df6b56eafeb9:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@df6b56eafeb9:/# 

#9、进入容器docker attach 容器id
[k@localhost ~]$ docker attach df6b56eafeb9
posted @ 2026-06-15 14:40  浦西和迪克的爱情故事  阅读(9)  评论(0)    收藏  举报