文章目录
1 帮助命令
docker version: 查看docker的版本
docker info: 查看docker的基本信息,镜像地址,镜像数量等等
docker --help
dokcer 命令 --help
2 镜像命令
2.1 docker images
查看的镜像
使用帮助命令查看用法
docker images --help
命令格式
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images: 查看所有的镜像
Options:
-a, --all Show all images (default hides intermediate images) 显示出所有的镜像
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs :只显示镜像的id
输出
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 831691599b88 4 months ago 215MB
mysql 5.7 9cfcce23593a 4 months ago 448MB
hello-world latest bf756fb1ae65 9 months ago 13.3kB
- REPOSITORY: 镜像的仓库源(镜像的名字)
- TAG: 镜像的标签
- IMAGE ID : 镜像id
- CREATED: 镜像创建时间
- SIZE 镜像的大小
2.2 docker search
搜索镜像
使用帮助命令查看用法
docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
基本使用
搜索Mysql
docker search mysql
NAME DESCRIPTION STARS(收藏) OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10072
可选项: 过滤
- 搜索收藏大于3000的
docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10072 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3692 [OK]
2.3 docker pull
拉取镜像(下载镜像)
使用帮助命令查看用法
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
演示一: 拉取mysql
docker pull mysql
Using default tag: latest : 默认使用最新的
latest: Pulling from library/mysql
bb79b6b2107f: Pull complete # 分层下载
49e22f6fb9f7: Pull complete
842b1255668c: Pull complete
9f48d1f43000: Pull complete
c693f0615bce: Pull complete
8a621b9dbed2: Pull complete
0807d32aef13: Pull complete
9eb4355ba450: Pull complete
6879faad3b6c: Pull complete
164ef92f3887: Pull complete
6e4a6e666228: Pull complete
d45dea7731ad: Pull complete
Digest: sha256:86b7c83e24c824163927db1016d5ab153a9a04358951be8b236171286e3289a4 # 签名信息
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest ## 真实地址
docker pull mysql
等价于
docker pull docker.io/library/mysql:latest
演示二: 拉取mysql,并且指定版本
pull mysql:5.7
5.7: Pulling from library/mysql
bb79b6b2107f: Already exists # 注意观察这几个已经存在的,是因为在刚刚下载最新的Mysql的时候已经下载过了,这就是docker分层下载的好处,极大的节省内存
49e22f6fb9f7: Already exists
842b1255668c: Already exists
9f48d1f43000: Already exists
c693f0615bce: Already exists
8a621b9dbed2: Already exists
0807d32aef13: Already exists
6d2fc69dfa35: Pull complete
56153548dd2c: Pull complete
3bb6ba940303: Pull complete
3e1888da91a7: Pull complete
Digest: sha256:b3dc8d10307ab7b9ca1a7981b1601a67e176408be618fc4216d137be37dae10b
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.4 docker rmi
删除镜像
使用帮助命令查看用法
# 可以删除多个镜像,用逗号隔开
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
演示一:删除刚刚的mysql5.7
- 使用下面命令查看mysql5.7的镜像id:42cdba9f1b08
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 42cdba9f1b08 5 days ago 448MB
mysql latest 8e85dd5c3255 5 days ago 544MB
- 删除mysql5.7
docker rmi -f 42cdba9f1b08
总结
删除镜像
docker rmi -f 镜像id
删除多个镜像
docker rmi -f 镜像id,镜像id,镜像id
删除全部镜像
docker rmi -f $(docker images -aq)
3 容器命令
说明:有了镜像才能创建容器
3.1 准备工作
下载一个centerOS的镜像
docker pull centos
3.2 新建容器并且启动:docker run
3.2.1 介绍
docker run [可选参数] 镜像id
参数说明
--name="NAME"
指定容器的名字,可以用来区分容器-d
后台运行 nohup-it
使用交互的方式运行容器,进入容器-p
指定端口-p ip:主机端口:容器端口
-p 主机端口:容器端口
-
-p 容器端口
-P
随机指定端口
3.2.2 演示
- 运行进入centOS
➜ ~ docker run -it centos /bin/bash
[root@d67c74c36864 /]#
- 退出,此时直接退出并停止容器
exit
4ddcd0e1bd6181d53a72a9bf0970589a45df51161824e7d69b52a0a1aff77b21
3.3 docker ps
查看正在运行的容器
docker ps
查看曾经运行过的容器
docker ps -a
- 常用参数
-a
查看曾经运行过的容器+当前正在运行的容器-n=?
列出指定数量的容器
docker ps -a -n=1
-q
只显示编号
➜ ~ docker ps -a -q
d67c74c36864
004294cade9b
828ddb4b1647
3.4 退出容器
- 直接停止并退出容器
exit
- 退出容器但不停止
crtl + p + q
3.5 删除容器
- 删除容器,不能删除正在运行的容器
docker rm 容器id
测试
- 启动容器
docker run -d centos /bin/bash
- 查看容器id
➜ ~ docker ps -q
1d7f696fd7ca
- 删除容器
➜ ~ docker rm 1d7f696fd7ca
Error response from daemon: You cannot remove a running container 1d7f696fd7ca3a2dfc42adccf9c71a5e52b67d122f019ca0573cf0b997ad43f2. Stop the container before attempting removal or force remove
You cannot remove a running container
- 停止这个容器
docker stop 1d7f696fd7ca
- 再删除容器
➜ ~ docker rm 1d7f696fd7ca
1d7f696fd7ca
- 强制删除
docker rm -f 容器id
- 删除所有容器
docker rm -f $(docker ps -aq)
docker ps -a -q | xargs docker rm
3.6 启动和停止容器
# 启动容器
docker start 容器id
# 停止容器
docker stop 容器id
# 重启容器
docker restart 容器id
# 杀掉容器
docker kill 容器id
4 常用的其他命令
后台运行
- 使用
-d
参数
docker run -d centos
- 查看
docker ps
问题:为什么使用docker ps
没有看到我们启动的容器呢?说明我们容器停止了
docker容器使用后台运行,就必须有一个前台进程,docker发现没有应用,就会自动停止
查看日志
docker logs
➜ ~ 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:37) or relative (e.g. 42m for 42 minutes)
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
-f
格式化输出-t
显示时间--tail number
显示最近的number条日志
查看容器中的进程信息
docker top 容器id
查看容器中的元数据信息
docker inspect 容器id
进入当前正在运行的容器
- 方式一
docker exec -it 容器id bashShell
- 方式二
docker attach -it 容器id
docker exec
进入容器后打开一个新的终端docker attach
进入容器正在执行的终端
拷贝文件
docker cp 容器id:容器内路径 目的主机路径
➜ ~ docker rm 1d7f696fd7ca /home/test.java /home