帮助命令
docker version # 显示docker的版本信息
docker info # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help # 万能命令
帮助文档的地址:https://docs.docker.com/engine/reference/commandline/docker/
搜索镜像地址:https://hub.docker.com/
镜像命令
* docker images 查看所有本地的主机上的镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
# 可选项
-a, --all # 列出所有镜像
-q, --quiet # 只显示id
* docker search 搜索镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12373 [OK]
mariadb MariaDB Server is a high performing open sou… 4762 [OK]
# 可选项,通过搜索来过滤
--filter=STARS=3000 # 搜索出来的镜像就是STARS大于3000的
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12373 [OK]
mariadb MariaDB Server is a high performing open sou… 4762 [OK]
* docker pull 下载镜像
# 下载镜像 docker pull 镜像名:tag[:tag]
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker pull mysql
Using default tag: latest # 如果不写tag(tag指定版本),默认就是latest(最新版本)
latest: Pulling from library/mysql
f003217c5aae: Pull complete # 分层下载,docker image的核心 联合文件系统
65d94f01a09f: Pull complete
43d78aaa6078: Pull complete
a0f91ffbdf69: Pull complete
59ee9e07e12f: Pull complete
04d82978082c: Pull complete
70f46ebb971a: Pull complete
db6ea71d471d: Pull complete
c2920c795b25: Pull complete
26c3bdf75ff5: Pull complete
9ec1f1f78b0e: Pull complete
4607fa685ac6: Pull complete
Digest: sha256:1c75ba7716c6f73fc106dacedfdcf13f934ea8c161c8b3b3e4618bcd5fbcf195 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 下面两个命令等价
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker pull mysql:5.7
^[[15~5.7: Pulling from library/mysql
f003217c5aae: Already exists # 重复依赖不再下载
65d94f01a09f: Already exists
43d78aaa6078: Already exists
a0f91ffbdf69: Already exists
59ee9e07e12f: Already exists
04d82978082c: Already exists
70f46ebb971a: Already exists
ba61822c65c2: Pull complete
dec59acdf78a: Pull complete
0a05235a6981: Pull complete
c87d621d6916: Pull complete
Digest: sha256:1a73b6a8f507639a8f91ed01ace28965f4f74bb62a9d9b9e7378d5f07fab79dc
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f26e21ddd20d 9 days ago 450MB
mysql latest 667ee8fb158e 9 days ago 521MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
* docker rmi 删除镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f 镜像id # 删除指定的镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f 镜像id 镜像id # 删除多个镜像
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker rmi -f $(docker images -aq) # 删除全部的镜像
容器命令
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
docker pull centos
* 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字 tomcat01 tomcat02,用来区分容器
-d 后台方式运行,ja nohup
-it 使用交互方式进行,进入容器查看内容
-p 指定容器的端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口 (常用)
-p 容器端口
容器端口
-P 随机指定端口(大写)
# 测试,启动并进入容器
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker run -it centos /bin/bash
[root@3222c9af3e6a /]# ls # 查看容器内的centos,基础版本,很多命令都是不完善的
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@3222c9af3e6a /]# exit
exit
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# ls
12311.txt.gz 1.txt 222.txt 3.txt a1.txt a3.txt a5.txt a7.txt a9.txt bzip2.txt.bz2 gzip.txt.gz mulu1 testzip
123123.txt.bz2 2222.txt 2.txt a10.txt a2.txt a4.txt a6.txt a8.txt bzip2.txt gzip.txt muli1 mulu2
* 列出所有的运行的容器
# docker ps 命令
# 列出当前正在运行的容器
-a # 列出当前正在运行的容器,带出历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZ2zei1yo0mi0ze1ngipksZ ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3222c9af3e6a centos "/bin/bash" 5 minutes ago Exited (0) 2 minutes ago tender_lederberg
2307467c5bcd feb5d9fea6a5 "/hello" 6 weeks ago Exited (