docker的基本命令
查看docker信息
docker version #查看docker版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #万能命令
帮助文档地址:https://docs.docker.com/engine/reference/commandline/
查看镜像命令:
docker images #查看所有本地的主机上的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
解释
REPOSITORY | 镜像的仓库源 |
---|---|
TAG | 镜像的标签 |
IMAGE ID | 镜像的id |
CREATED | 镜像的创建时间 |
SIZE | 镜像的大小 |
可选项
-a , --all #列出所有镜像
-q , --quiet # 只显示镜像的id
docker search 搜索镜像
docker search [镜像名]
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql/mysql-server Optimized MySQL Server Docker images. Create… 907 [OK]
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 92
centos/mysql-57-centos7 MySQL 5.7 SQL database server 92
可选项 ,通过STARS来过滤
–filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@localhost ~]# docker search mysql --filter=STARS=900
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql/mysql-server Optimized MySQL Server Docker images. Create… 907 [OK]
[root@localhost ~]#
docker pull 下载镜像
docker pull mysql #下载mysql镜像
docker pull 镜像名[:tag]
#如果不写tag就会默认使用最新的镜像latest
#docker pull mysql:5.7 指定下载mysql5.7的镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost ~]#
等价于它:
docker pull mysql
docker pull docker.io/library/mysql:latest
#查看镜像就可以看到下载的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 months ago 448MB
mysql latest 3218b38490ce 2 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@localhost ~]#
docker删除镜像
docker rmi -f 【镜像名/id】
# rm 删除 i 镜像 f 全部删除
#可以同时删除多个镜像,中间用空格隔开镜像名就行
docker rmi -f mysql
docker rmi -f $(docker images -aq)
#$(docker images -aq)查询所有镜像的id
#递归删除所有镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 months ago 448MB
mysql latest 3218b38490ce 2 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@localhost ~]# docker rmi -f c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 3218b38490ce 2 months ago 516MB
hello-world latest feb5d9fea6a5 5 months ago 13.3kB
[root@localhost ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
Deleted: sha256:8805862fcb6ef9deb32d4218e9e6377f35fb351a8be7abafdf1da358b2b287ba
Deleted: sha256:872d2f24c4c64a6795e86958fde075a273c35c82815f0a5025cce41edfef50c7
Deleted: sha256:6fdb3143b79e1be7181d32748dd9d4a845056dfe16ee4c827410e0edef5ad3da
Deleted: sha256:b0527c827c82a8f8f37f706fcb86c420819bb7d707a8de7b664b9ca491c96838
Deleted: sha256:75147f61f29796d6528486d8b1f9fb5d122709ea35620f8ffcea0e0ad2ab0cd0
Deleted: sha256:2938c71ddf01643685879bf182b626f0a53b1356138ef73c40496182e84548aa
Deleted: sha256:ad6b69b549193f81b039a1d478bc896f6e460c77c1849a4374ab95f9a3d2cea2
Untagged: hello-world:latest
Untagged: hello-world@sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#
容器命令
我们有镜像才可以创建容器,下一个centOS镜像来测试
docker pull centos
新建容器并启动
docker run --help #查看帮助命令
docker run [可选参数] image
参数说明
--name="Name" 容器的名字 tomcat01 tomcat02,用来区分容器
-d 后台运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口 大P
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 5 months ago 231MB
[root@localhost ~]# docker run -it centos /bin/bash
[root@95e0d1d00068 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@95e0d1d00068 /]# exit
exit
[root@localhost ~]#
启动并进入容器
docker run -it centos /bin/bash #启动并进入容器
退出容器
exit #退出容器
docker ps #查看当前运行的容器
docker ps -a #查看当前运行的容器+历史运行过的容器
docker ps -a -n=? #显示最近创建的?个容器容器
docker ps -a -n=1
docker ps -q #只显示容器的编号
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eba63bd352bb centos “/bin/bash” 19 seconds ago Up 18 seconds recursing_varahamihira
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eba63bd352bb centos “/bin/bash” 58 seconds ago Up 57 seconds recursing_varahamihira
95e0d1d00068 centos “/bin/bash” 3 minutes ago Exited (0) 3 minutes ago reverent_cartwright
ea46497c694c feb5d9fea6a5 “/hello” 6 hours ago Exited (0) 6 hours ago wizardly_kowalevski
[root@localhost ~]# docker ps -aq
eba63bd352bb
95e0d1d00068
ea46497c694c
[root@localhost ~]# docker ps -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eba63bd352bb centos “/bin/bash” 12 minutes ago Up 12 minutes recursing_varahamihira
95e0d1d00068 centos “/bin/bash” 15 minutes ago Exited (0) 14 minutes ago reverent_cartwright
[root@localhost ~]#
删除容器(不能删除正在运行的容器)
docker rm 容器id
#删除所有容器
docker rm -f $(docker ps -aq)
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重新启动容器
docker stop 容器id #停止当前正在运行的容器
docker kill 容器id #强制停止当前容器