镜像的基本命令
帮助命令
docker version # 显示 Docker 版本信息。
docker info # 显示 Docker 系统信息,包括镜像和容器数
docker --help # 帮助
帮助文档:https://docs.docker.com/engine/reference/commandline/docker/
镜像命令
docker images(查看主机上的镜像)
# 列出本地主机上的镜像
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
# 解释
# REPOSITORY 镜像的仓库源
# TAG 镜像的标签
# IMAGE ID 镜像的ID
# CREATED 镜像创建时间
# SIZE 镜像大小
# 同一个仓库源可以有多个 TAG,代表这个仓库源的不同版本,我们使用REPOSITORY:TAG 定义不同的镜像,如果你不定义镜像的标签版本,docker将默认使用 lastest 镜像
# 可选项
-a: 列出本地所有镜像
-q: 只显示镜像id
--digests: 显示镜像的摘要信息
docker search(搜索镜像)
浏览器搜索:https://hub.docker.com
# 搜索镜像
[root@xiaoyequ ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 9587 [OK]
# docker search 某个镜像的名称 对应DockerHub仓库中的镜像
# 可选项
--filter=stars=50 : 列出收藏数不小于 50 的镜像。
docker pull(下载镜像)
# 下载镜像
[root@xiaoyequ ~]# docker pull mysql
Using default tag: latest # 不写tag,默认是latest(最新版本)
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete # 分层下载
0bdc5971ba40: Pull complete
97ae94a2c729: Pull complete
f777521d340e: Pull complete
1393ff7fc871: Pull complete
a499b89994d9: Pull complete
7ebe8eefbafe: Pull complete
597069368ef1: Pull complete
ce39a5501878: Pull complete
7d545bca14bf: Pull complete
211e5bb2ae7b: Pull complete
5914e537c077: Pull complete
Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实位置
指定版本下载
# 指定版本下载
[root@xiaoyequ ~]# docker pull mysql:5.7
docker rmi(删除镜像)
# 删除镜像
docker rmi -f 镜像id # 删除单个
docker rmi -f 镜像id 镜像id 镜像id # 删除多个
docker rmi -f $(docker images -qa) # 删除全部
容器的基本命令
说明:有镜像才能创建容器,我们这里使用 centos 的镜像来测试,就是虚拟一个 centos
docker pull centos
docker run(新建容器并启动)
# 命令
docker run [OPTIONS] IMAGE [COMMAND][ARG...]
# 常用参数说明
--name="Name" # 给容器指定一个名字
-d # 后台方式运行容器,并返回容器的id!
-i # 以交互模式运行容器,通过和 -t 一起使用
-t # 给容器重新分配一个终端,通常和 -i 一起使用
-P # 随机端口映射(大写)
-p # 指定端口映射(小写),一般可以有四种写法
ip:hostPort:containerPort(ip:主机端口:容器端口)
ip::containerPort
hostPort:containerPort (常用)
containerPort
# 测试
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 470671670cac 4 months ago 237MB
# 使用centos进行用交互模式启动容器,在容器内执行/bin/bash命令!
[root@xiaoyequ ~]# docker run -it centos /bin/bash
[root@be4b54c57337 /]# ls # 注意地址,已经切换到容器内部了!(类似于一个小型的linux)
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@be4b54c57337 /]# exit # 使用 exit 退出容器
exit
[root@xiaoyequ ~]#
docker ps(列出所有运行的容器)
# 命令
docker ps [OPTIONS]
# 常用参数说明
-a # 列出当前所有正在运行的容器 + 历史运行过的容器
-l # 显示最近创建的容器
-n=? # 显示最近n个创建的容器
-q # 静默模式,只显示容器编号。
exit(退出容器)
exit # 容器停止退出
ctrl+P+Q # 容器不停止退出
启动停止容器
docker start (容器id or 容器名) # 启动容器
docker restart (容器id or 容器名) # 重启容器
docker stop (容器id or 容器名) # 停止容器
docker kill (容器id or 容器名) # 强制停止容器
docker rm(删除容器)
docker rm 容器id # 删除指定容器
docker rm -f $(docker ps -a -q) # 删除所有容器
docker ps -a -q|xargs docker rm # 删除所有容器
常用其他命令
docker run -d(后台启动容器)
# 命令
docker run -d 容器名
# 例子
[root@xiaoyequ ~]# docker run -d centos # 启动centos,使用后台方式启动
ee4a238a51ed3bd7131bd595c2ac716692b5f3da87466ad883a20d5d4246d47a
# 问题: 使用docker ps 查看,发现容器已经退出了!
# 解释:Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命令,就会自动退出。
# 比如,你运行了nginx服务,但是docker前台没有运行应用,这种情况下,容器启动后,会立即自杀,因为他觉得没有程序了,所以最好的情况是,将你的应用使用前台进程的方式运行启动。
docker logs(查看日志)
# 命令
docker logs -f -t --tail 容器id
# 例子:我们启动 centos,并编写一段脚本来测试玩玩!最后查看日志
[root@xiaoyequ ~]# docker run -d centos /bin/sh -c "while true;do echo xiaoyequ;sleep 1;done"
cbfe6f6cc92321101e63353c76d3c79ee032f271d68fac3c52c0bf3c8846cdd1
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbfe6f6cc923 centos "/bin/sh -c 'while t…" 8 seconds ago Up 7 seconds condescending_sanderson
# -t 显示时间戳
# -f 打印最新的日志
# --tail 数字 显示多少条!
[root@xiaoyequ ~]# docker logs -tf --tail 10 cbfe6f6cc923
2020-06-06T09:59:57.634232481Z xiaoyequ
2020-06-06T09:59:58.636842507Z xiaoyequ
2020-06-06T09:59:59.639316083Z xiaoyequ
2020-06-06T10:00:00.641746110Z xiaoyequ
2020-06-06T10:00:01.644386953Z xiaoyequ
2020-06-06T10:00:02.647106350Z xiaoyequ
2020-06-06T10:00:03.649668294Z xiaoyequ
2020-06-06T10:00:04.652318092Z xiaoyequ
2020-06-06T10:00:05.655020539Z xiaoyequ
docker top(查看容器中运行的进程信息,支持 ps 命令参数。)
# 命令
docker top 容器id
[root@xiaoyequ ~]# docker top cbfe6f6cc923
UID PID PPID C STIME TTY TIME CMD
root 11320 11303 0 17:59 ? 00:00:00 /bin/sh -c while true;do echo xiaoyequ;sleep 1;done
root 11775 11320 0 18:06 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
docker inspect(查看容器/镜像的元数据)
# 命令
docker inspect 容器id
# 测试
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dab9935c4259 centos "/bin/bash" About a minute ago Up About a minute frosty_sutherland
[root@xiaoyequ ~]# docker inspect dab9935c4259
[
{
# 完整的id,这里上面的容器id,就是截取的这个id前几位!
"Id": "dab9935c42598eef3357a4963adec4ca8d1436ff9abd3f10bb1bc7b2f91c35f1",
"Created": "2020-06-06T10:10:18.08897757Z",
"Path": "/bin/bash",
"Args": [],
# 状态
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 12171,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-06-06T10:10:18.399953427Z",
"FinishedAt": "0001-01-01T00:00:00Z"
}
......
}
]
docker exec/attach(进入正在运行的容器)
# 命令1(-it 用交互模式进入容器)
docker exec -it 容器id bashShell
# 测试1
[root@xiaoyequ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dab9935c4259 centos "/bin/bash" 4 minutes ago Up 4 minutes frosty_sutherland
[root@xiaoyequ ~]# docker exec -it dab9935c4259 /bin/bash
[root@dab9935c4259 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:10 pts/0 00:00:00 /bin/bash
root 14 0 0 10:14 pts/1 00:00:00 /bin/bash
root 27 14 0 10:15 pts/1 00:00:00 ps -ef
# 命令2
docker attach 容器id
# 测试2
[root@xiaoyequ ~]# docker attach 0f3e219a7f65
[root@0f3e219a7f65 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 10:29 pts/0 00:00:00 /bin/bash
root 14 1 0 10:32 pts/0 00:00:00 ps -ef
区别:
exec :是在容器中打开新的终端,并且可以启动新的进程 attach :直接进入容器启动命令的终端,不会启动新的进程
docker cp(从容器内拷贝文件到主机上)
# 命令
docker cp 容器id:容器内路径 目的主机路径
# 测试
# 容器内执行,创建一个文件测试
[root@xiaoyequ home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f3e219a7f65 centos "/bin/bash" 4 hours ago Up 4 hours stoic_galois
[root@xiaoyequ home]# docker attach 0f3e219a7f65
[root@0f3e219a7f65 /]# cd /home
[root@0f3e219a7f65 home]# ls
[root@0f3e219a7f65 home]# touch xiaoyequ.java
[root@0f3e219a7f65 home]# ls
xiaoyequ.java
[root@0f3e219a7f65 home]# exit
exit
# linux复制查看,是否复制成功
[root@xiaoyequ home]# docker cp 0f3e219a7f65:home/xiaoyequ.java /home/
[root@xiaoyequ home]# ls
xiaoyequ.java
小结
常用命令
attach: Attach to a running container # 当前 shell 下attach 连接指定运行镜像
build: Build an image from a Dockerfile # 通过 Dockerfile 定制镜像
commit: Create a new image from a container changes # 提交当前容器为新的镜像
cp: Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中
create: Create a new container # 创建一个新的容器,同run,但不启动容器
diff: Inspect changes on a container filesystem # 查看 docker 容器变化
events: Get real time events from the server # 从 docker 服务获取容器实时事件
exec: Run a command in an existing container # 在已存在的容器上运行命令
export: Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history: Show the history of an image # 展示一个镜像形成历史
images: List images # 列出系统当前镜像
import: Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]
info: Display system-wide information # 显示系统相关信息
inspect: Return low-level information on a container # 查看容器详细信息
kill: Kill a running container # kill 指定 docker 容器
load: Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]
login: Register or Login to the docker registry server # 注册或者登陆一个docker 源服务器
logout: Log out from a Docker registry server # 从当前 Dockerregistry 退出
logs: Fetch the logs of a container # 输出当前容器日志信息
port: Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口
pause: Pause all processes within a container # 暂停容器
ps: List containers # 列出容器列表
pull: Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像
push: Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器
restart: Restart a running container # 重启运行的容器
rm: Remove one or more containers # 移除一个或者多个容器
rmi: Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run: Run a command in a new container # 创建一个新的容器并运行一个命令
save: Save an image to a tar archive # 保存一个镜像为一个tar 包[对应 load]
search: Search for an image on the Docker Hub # 在 docker hub 中搜索镜像
start: Start a stopped containers # 启动容器
stop: Stop a running containers # 停止容器
tag: Tag an image into a repository # 给源中镜像打标签
top: Lookup the running processes of a container # 查看容器中运行的进程信息
unpause: Unpause a paused container # 取消暂停容器
version: Show the docker version information # 查看 docker 版本号
wait: Block until a container stops, then print its exit code # 截取容器停止时的退出状态值