[root@sail ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 months ago 13.3kB [root@sail ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 months ago 13.3kB [root@sail ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 months ago 13.3kB
只显示镜像的 ID
1 2
[root@sail ~]# docker images -q feb5d9fea6a5
显示所有镜像的 ID
1 2
[root@sail ~]# docker images -aq feb5d9fea6a5
对镜像进行过滤
1 2 3 4 5 6 7 8
[root@sail ~]# docker images java REPOSITORY TAG IMAGE ID CREATED SIZE [root@sail ~]# docker images hello-world REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 months ago 13.3kB [root@sail ~]# docker images hello-world:latest REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 2 months ago 13.3kB
[root@sail ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11789 [OK] mariadb MariaDB Server is a high performing open sou… 4488 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 882 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 92 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 89 centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 53 prom/mysqld-exporter 44 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] tutum/mysql Base docker image to run a MySQL database se… 35 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK] mysql/mysql-router MySQL Router provides transparent routing be… 23 centos/mysql-56-centos7 MySQL 5.6 SQL database server 21 arey/mysql-client Run a MySQL client from a docker container 19 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 17 [OK] genschsa/mysql-employees MySQL Employee Sample Database 8 [OK] yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 3 [OK] jelastic/mysql An image of the MySQL database server mainta… 2 centos/mysql-80-centos7 MySQL 8.0 SQL database server 2 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK] vitess/mysqlctld vitess/mysqlctld 1 [OK]
搜索出 Stars 大于 3000 的
1 2 3 4
[root@sail ~]# docker search mysql -f=STARS=3000 NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 11789 [OK] mariadb MariaDB Server is a high performing open sou… 4488 [OK]
[root@sail ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 5.0071510791e92 5 days ago 110MB redis latest aea9b698d7d1 6 days ago 113MB mysql 5.7738e7101490b 6 days ago 448MB mysql latest bbf6571db497 6 days ago 516MB hello-world latest feb5d9fea6a5 2 months ago 13.3kB
[root@sail ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
运行镜像
docker run
语法
1
docker run [参数] 镜像名
参数
--name:指定容器的名称,如果正在运行该名称的容器,会报错。
-rm:用完即删除,通常用来测试。
d:后台方式运行。
it:使用交互方式运行,可以进入容器查看内容。
e:指定运行环境。
p:随机指定端口。
p
:指定容器的端口,如:
1
-p8080:8080
。还可以有以下写法:
p ip:主机端口:容器端口
p 主机端口:容器端口
p 容器端口
运行 centos 镜像
1 2 3 4 5 6 7 8
[root@sail ~]# docker run -it centos /bin/bash Unable to find image 'centos:latest' locally # 检索本地镜像,发现没有该镜像,则去仓库中搜索。 latest: Pulling from library/centos # 开始从仓库中拉取 a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest [root@81c83ea42dc0 /]# ls # 由于是以交互方式运行,且进入 /bin/bash 中,此时的路径即为 centos 容器中的 /bin/bash bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
由此可以看出,容器就是一个微型的 Linux 系统。它只保留了最核心的功能和最基本的命令,方便进行操作。
Docker容器
查看容器
docker ps
语法
1
docker ps [参数]
参数
a:查看所有容器(包括正在运行的和已经停止的)。
n:显示最近创建的容器,设置显示个数。
q:只显示容器的编号。
查看正在运行的容器
1
[root@sail ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1aaf76d85b9e centos "/bin/bash" About a minute ago Up About a minute intelligent_proskuriakova
查看所有容器
1 2 3 4 5 6 7
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aaf76d85b9e centos "/bin/bash" About a minute ago Up About a minute intelligent_proskuriakova 7ac04abd5a1f centos "/bin/bash"3 minutes ago Exited (0) 3 minutes ago elegant_euler bbb87116d848 centos "/bin/bash"8 minutes ago Exited (0) 3 minutes ago focused_boyd 81c83ea42dc0 centos "/bin/bash"28 minutes ago Exited (0) 19 minutes ago zealous_proskuriakova 52918b3ce8f6 feb5d9fea6a5 "/hello"11 days ago Exited (0) 11 days ago friendly_ramanujan
显示最近创建的 2 个容器
1 2 3 4
[root@sail ~]# docker ps -a -n=2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aaf76d85b9e centos "/bin/bash"5 hours ago Up 5 hours intelligent_proskuriakova 7ac04abd5a1f centos "/bin/bash"5 hours ago Exited (0) 5 hours ago elegant_euler
[root@sail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
由此可见,这样退出后容器也会停止。
Ctrl + P + Q
如果想退出后容器不停止,可以使用 Ctrl + P + Q 快捷键退出。
1 2 3 4
[root@sail ~]# docker run -it centos /bin/bash [root@1aaf76d85b9e /]# [root@sail ~]# docker ps # 此时即为使用 Ctrl + P + Q 快捷键的效果 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aaf76d85b9e centos "/bin/bash" 8 seconds ago Up 8 seconds intelligent_proskuriakova
删除容器
docker rm
语法
1
docker rm [参数] 容器 [容器...]
参数
f:强制删除。
删除指定容器(不能删除正在运行的容器)
1 2 3 4 5 6
[root@sail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aaf76d85b9e centos "/bin/bash"5 hours ago Up 5 hours intelligent_proskuriakova
[root@sail ~]# docker rm 1aaf76d85b9e Error response from daemon: You cannot remove a running container 1aaf76d85b9ee5002411c1ea390fca05819f19dc400e85127731d37455cb0acc. Stop the container before attempting removal or force remove
强制删除指定容器
1 2 3 4 5 6
[root@sail ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1aaf76d85b9e centos "/bin/bash"5 hours ago Up 5 hours intelligent_proskuriakova
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash"33 seconds ago Exited (0) 7 seconds ago centos03
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash" About a minute ago Up 1 second centos03
停止容器
docker stop
查看所有的容器,容器状态为运行
1 2 3
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash" About a minute ago Up 1 second centos03
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash"4 minutes ago Exited (0) 38 seconds ago centos03
停止所有正在运行的容器。
1
$ sudo docker stop $(sudo docker ps -aq)
重启容器
docker restart
查看所有的容器,容器状态为关闭
1 2 3
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash"4 minutes ago Exited (0) 38 seconds ago centos03
[root@sail ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 569026bc0955 centos "/bin/bash" 8 minutes ago Exited (137) 15 seconds ago centos03