1. 帮助命令
# 显示docker的版本信息
docker version
# 显示docker的系统信息包括镜像和容器的数量
docker info
# 帮助命令
docker xxx --help
2. 镜像命令
2.1 docker images 显示镜像
查看本地镜像
docker images [OPTIONS] [REPOSITORY[:TAG]]
名字,简写 | 默认值 | 描述 |
---|---|---|
–all , -a | 显示所有镜像(默认隐藏中间镜像) | |
–digests | 显示摘要 | |
–filter , -f | 根据提供的条件过滤输出 | |
–format | 使用Go模板打印格式化的镜像输出 | |
–no-trunc | 不截断输出 | |
–quiet , -q | 仅展示镜像ID |
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 5 months ago 13.3kB
# 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
2.2 docker search 搜索docker hub镜像
[root@bogon ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter 根据提供的条件筛选输出
--format string 格式化输出
--limit int 查询结果显示的最大条数 (默认 25)
--no-trunc 不截断输出
[root@bogon ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
名称 描述 收藏 官方的 自动化
mysql MySQL is a widely used, open-source relation… 11306 [OK]
mariadb MariaDB Server is a high performing open sou… 4294 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 838 [OK]
percona Percona Server is a fork of the MySQL relati… 550 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 299 [OK]
# 搜索收藏数不小于3000的
docker search mysql --f=STARS=3000
2.3 docker pull 拉去镜像
[root@bogon ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
# 下载镜像 docker pull 镜像名[:tag]
[root@bogon ~]# docker pull mysql
Using default tag: latest # 如果不写tag, 默认就是latest
latest: Pulling from library/mysql
e1acddbe380c: Pull complete # 分层下载, docker image的核心 联合文件系统
bed879327370: Pull complete
03285f80bafd: Pull complete
ccc17412a00a: Pull complete
1f556ecc09d1: Pull complete
adc5528e468d: Pull complete
1afc286d5d53: Pull complete
6c724a59adff: Pull complete
0f2345f8b0a3: Pull complete
c8461a25b23b: Pull complete
3adb49279bed: Pull complete
77f22cd6c363: Pull complete
Digest: sha256:d45561a65aba6edac77be36e0a53f0c1fba67b951cb728348522b671ad63f926 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址
# 等价于
docker pull mysql
docker pull docker.io/library/mysql:latest
# 指定版本下载 mysql 5.7
[root@bogon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
e1acddbe380c: Already exists # 复用的文件将会直接引用而无需重新下载
bed879327370: Already exists
03285f80bafd: Already exists
ccc17412a00a: Already exists
1f556ecc09d1: Already exists
adc5528e468d: Already exists
1afc286d5d53: Already exists
4d2d9261e3ad: Pull complete
ac609d7b31f8: Pull complete
53ee1339bc3a: Pull complete
b0c0a831a707: Pull complete
Digest: sha256:7cf2e7d7ff876f93c8601406a5aa17484e6623875e64e7acc71432ad8e0a3d7e
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.4 docker rmi 删除镜像
docker rmi -f 镜像id #删除指定镜像
docker rmi -f 镜像id 镜像id 镜像id #删除多个镜像
docker rmi -f $(docker images -aq) #删除全部镜像
3. 容器命令
下载一个centos镜像进行测试
docker pull centos
3.1 docker run 新建容器并启动
docker run [可选参数] image
# 参数说明
--name="Name" 容器名字, 用来区分容器
-d 后台方式运行
-it 使用交互模式运行, 进入容器查看内容
-p 指定容器端口 -p 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P 随机指定端口
# 启动并进入容器
[root@bogon ~]# docker run -it centos /bin/bash
[root@5df653c83fe9 /]# ls # 查看容器内的centos, 基础版本缺少很多命令
[root@5df653c83fe9 /]# exit # 从容器退回主机
3.2 docker ps 查看运行中的容器
# 参数说明
# 列出运行中的容器
-a # 列出运行中的容器+历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器的编号
3.3 退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
3.4 docker rm 删除容器
docker rm 容器id # 删除指定容器, 不能删除正在运行的容器, 强制删除 rm -f
docker rm -f $(docker ps -aq) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
3.5 启动和停止容器
docker start 容器id # 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止当前正在运行的容器
docker kill 容器id # 强制停止当前容器
4. 常用的其它命令
4.1 后台启动容器
docker run -d centos
# 问题 docker ps, 发现centos 停止了
# 常见的坑: docker 容器使用后台运行就必须要有一个前台进程, docker发现没有应用就会自动停止
4.2 docker logs 查看日志
[root@bogon ~]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps 显示时间戳
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
# 显示指定行是的日志
docker logs -tf --tail 10 容器id
4.4 docker top查看容器内部的进程信息
docker top 容器id
4.5 docker inspect 查看容器的元数据
[root@bogon ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
docker inspect 容器id
4.6 进入正在运行的容器
# 进入容器后打开新的终端, 可以在里面操作
docker exec -it 容器id /bin/bash
# 进入容器正在执行的终端, 不会启动新的进程
docker attach 容器id
4.7 docker cp 从容器拷贝文件到主机
# 将容器内的text.txt文件拷贝到主机
docker cp 容器id:/home/test.txt /home