|
| 1 | +# Docker 常用命令 |
| 2 | + |
| 3 | +## 帮助启动类命令 |
| 4 | + |
| 5 | +``` |
| 6 | +启动docker: systemctl start docker |
| 7 | +
|
| 8 | +停止docker: systemctl stop docker |
| 9 | +
|
| 10 | +重启docker: systemctl restart docker |
| 11 | +
|
| 12 | +查看docker状态: systemctl status docker |
| 13 | +
|
| 14 | +开机启动: systemctl enable docker |
| 15 | +
|
| 16 | +查看docker概要信息: docker info |
| 17 | +
|
| 18 | +查看docker总体帮助文档: docker --help |
| 19 | +
|
| 20 | +查看docker命令帮助文档: docker 具体命令 --help |
| 21 | +``` |
| 22 | + |
| 23 | +## 镜像命令 |
| 24 | + |
| 25 | +- docker images:列出本地主机上的镜像 |
| 26 | + |
| 27 | +  |
| 28 | + |
| 29 | + ``` |
| 30 | + 各个选项说明: |
| 31 | + |
| 32 | + REPOSITORY:表示镜像的仓库源 TAG:镜像的标签版本号 IMAGE ID:镜像ID CREATED:镜像创建时间 SIZE:镜像大小 |
| 33 | + |
| 34 | + 同一仓库源可以有多个 TAG 版本,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。 |
| 35 | + |
| 36 | + 如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,Docker 将默认使用 ubuntu:latest 镜像 |
| 37 | + |
| 38 | + OPTIONS 说明: |
| 39 | + - a:列出本地所有的镜像(含历史映像层) |
| 40 | + - q:只显示镜像 ID |
| 41 | + ``` |
| 42 | +
|
| 43 | +- docker search 某个镜像的名字:搜索镜像 |
| 44 | +
|
| 45 | + ``` |
| 46 | + 网站:https://hub.docker.com |
| 47 | + |
| 48 | + 命令: |
| 49 | + docker search [OPTIONS] 镜像名字 |
| 50 | + |
| 51 | + OPTIONS 说明: |
| 52 | + -limit:只列出 N 个镜像,默认 25 个 |
| 53 | + ``` |
| 54 | +
|
| 55 | +  |
| 56 | +
|
| 57 | +- docker pull 某个镜像的名字:下载镜像 |
| 58 | +
|
| 59 | + ``` |
| 60 | + docker pull 镜像名字[:TAG] |
| 61 | + |
| 62 | + docker pull 镜像名字 |
| 63 | + 没有 TAG 就是最新版,等价于 docker pull 镜像名字:latest |
| 64 | + ``` |
| 65 | +
|
| 66 | +  |
| 67 | +
|
| 68 | +- docker system df 查看镜像/容器/数据卷所占的空间 |
| 69 | +
|
| 70 | +  |
| 71 | +
|
| 72 | +- docker rmi 某个镜像名字 ID:删除镜像 |
| 73 | +
|
| 74 | + ``` |
| 75 | + 面试题:谈谈 Docker 虚悬镜像是什么? |
| 76 | + |
| 77 | + 是什么? |
| 78 | + 仓库名、标签都是 <none> 的镜像,俗称虚悬镜像 dangling image |
| 79 | + |
| 80 | + ``` |
| 81 | +
|
| 82 | +## 容器命令 |
| 83 | +
|
| 84 | +- docker run [OPTIONS] IMAGE [COMMAND] [ARG...] |
| 85 | +
|
| 86 | + ``` |
| 87 | + OPTIONS 说明:有些是一个减号,有些是两个减号 |
| 88 | + |
| 89 | + --name="容器新名字":为容器指定一个名称; |
| 90 | + -d: 后台运行容器并返回容器 ID,也即启动守护式容器(后台运行); |
| 91 | + |
| 92 | + -i:以交互模式运行容器,通常与 -t 同时使用; |
| 93 | + -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用; |
| 94 | + 也即启动交互式容器(前台有伪终端,等待交互); |
| 95 | + |
| 96 | + -P: 随机端口映射,大写 P |
| 97 | + -p: 指定端口映射,小写 p |
| 98 | + ``` |
| 99 | +
|
| 100 | +  |
| 101 | +
|
| 102 | + ``` |
| 103 | + 使用镜像 centos:latest 以交互模式启动一个容器,在容器内执行 /bin/bash 命令。 |
| 104 | + docker run -it centos /bin/bash |
| 105 | + |
| 106 | + 参数说明: |
| 107 | + -i: 交互式操作。 |
| 108 | + -t: 终端。 |
| 109 | + centos : centos 镜像。 |
| 110 | + /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。 |
| 111 | + 要退出终端,直接输入 exit。 |
| 112 | + ``` |
| 113 | +
|
| 114 | +  |
| 115 | +
|
| 116 | +- docker ps [OPTIONS]:列出当前所有正在运行的容器 |
| 117 | +
|
| 118 | + ``` |
| 119 | + OPTIONS说明(常用): |
| 120 | + |
| 121 | + -a:列出当前所有正在运行的容器+历史上运行过的。 |
| 122 | + -l:显示最近创建的容器。 |
| 123 | + -n:显示最近 n 个创建的容器。 |
| 124 | + -q:静默模式,只显示容器编号。 |
| 125 | + ``` |
| 126 | +
|
| 127 | +- 退出容器,两种方式: |
| 128 | +
|
| 129 | + - exit |
| 130 | +
|
| 131 | + run 进去容器,exit 退出,容器停止 |
| 132 | +
|
| 133 | + - Ctrl+p+q |
| 134 | +
|
| 135 | + run 进去容器,Ctrl+p+q 退出,容器不停止 |
| 136 | +
|
| 137 | +- docker start 容器 ID 或者容器名:启动已经停止运行的容器 |
| 138 | +
|
| 139 | +- docker restart 容器 ID 或者容器名:重启容器 |
| 140 | +
|
| 141 | +- docker stop 容器 ID 或者容器名:停止容器 |
| 142 | +
|
| 143 | +- docker kill 容器 ID 或者容器名:强制停止容器 |
| 144 | +
|
| 145 | +- docker rm 容器 ID:删除已停止的容器 |
| 146 | +
|
| 147 | + ``` |
| 148 | + 一次性删除多个容器实例: |
| 149 | + |
| 150 | + docker rm -f $(docker ps -a -q) |
| 151 | + |
| 152 | + docker ps -a -q | xargs docker rm |
| 153 | + ``` |
| 154 | +
|
| 155 | +- 启动守护式容器(后台服务器) |
| 156 | +
|
| 157 | + ``` |
| 158 | + 在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以通过 -d 指定容器的后台运行模型 |
| 159 | + |
| 160 | + docker run -d 容器名 |
| 161 | + |
| 162 | + redis 前后台启动演示: |
| 163 | + 前台交互式启动:docker run -it redis:6.0.8 |
| 164 | + 后台守护式启动:docker run -d redis:6.0.8 |
| 165 | + ``` |
| 166 | +
|
| 167 | +- docker logs 容器 ID:查看容器日志 |
| 168 | +
|
| 169 | +- docker top 容器 ID:查看容器内运行的进程 |
| 170 | +
|
| 171 | +- docker inspect 容器 ID:查看容器内部细节 |
| 172 | +
|
| 173 | +- 进入正在运行的容器并以命令行交互 |
| 174 | +
|
| 175 | + ``` |
| 176 | + docker exec -it 容器 ID bashShell |
| 177 | + |
| 178 | + docker attach 容器 ID |
| 179 | + |
| 180 | + 上述两个区别: |
| 181 | + attach 直接进入容器启动命令的终端,不会启动新的进程,用 exit 退出,会导致容器的停止。 |
| 182 | + exec 是在容器中打开新的终端,并且可以启动新的进程,用 exit 退出,不会导致容器的停止。 |
| 183 | + |
| 184 | + 推荐大家使用 docker exec 命令,因为退出容器终端,不会导致容器的停止。 |
| 185 | + |
| 186 | + 用之前的 Redis 容器实例进入服务: |
| 187 | + docker exec -it 容器 ID /bin/bash |
| 188 | + docker exec -it 容器 ID redis-cli |
| 189 | + 一般用 -d 后台启动的程序,再用 exec 进入对应容器实例 |
| 190 | + ``` |
| 191 | +
|
| 192 | +- docker cp 容器 ID:容器内路径 目的主机路径:从容器内拷贝文件到主机上 |
| 193 | +
|
| 194 | +- 导入和导出容器 |
| 195 | +
|
| 196 | + ``` |
| 197 | + export 导出容器的内容作为一个 tar 归档文件(对应 import 命令) |
| 198 | + import 从 tar 包中的内容创建一个新的文件系统再导入为镜像[对应 export] |
| 199 | + |
| 200 | + 案例: |
| 201 | + docker export 容器 ID > 文件名.tar |
| 202 | + cat 文件名.tar | docker import -镜像用户/镜像名:镜像版本号 |
| 203 | + ``` |
| 204 | +
|
| 205 | +  |
| 206 | +
|
| 207 | +## 小总结 |
| 208 | +
|
| 209 | + |
| 210 | +
|
| 211 | +``` |
| 212 | +attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像 |
| 213 | +build Build an image from a Dockerfile # 通过 Dockerfile 定制镜像 |
| 214 | +commit Create a new image from a container changes # 提交当前容器为新的镜像 |
| 215 | +cp Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中 |
| 216 | +create Create a new container # 创建一个新的容器,同 run,但不启动容器 |
| 217 | +diff Inspect changes on a container's filesystem # 查看 docker 容器变化 |
| 218 | +events Get real time events from the server # 从 docker 服务获取容器实时事件 |
| 219 | +exec Run a command in an existing container # 在已存在的容器上运行命令 |
| 220 | +export Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ] |
| 221 | +history Show the history of an image # 展示一个镜像形成历史 |
| 222 | +images List images # 列出系统当前镜像 |
| 223 | +import Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export] |
| 224 | +info Display system-wide information # 显示系统相关信息 |
| 225 | +inspect Return low-level information on a container # 查看容器详细信息 |
| 226 | +kill Kill a running container # kill 指定 docker 容器 |
| 227 | +load Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save] |
| 228 | +login Register or Login to the docker registry server # 注册或者登陆一个 docker 源服务器 |
| 229 | +logout Log out from a Docker registry server # 从当前 Docker registry 退出 |
| 230 | +logs Fetch the logs of a container # 输出当前容器日志信息 |
| 231 | +port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口 |
| 232 | +pause Pause all processes within a container # 暂停容器 |
| 233 | +ps List containers # 列出容器列表 |
| 234 | +pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像 |
| 235 | +push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器 |
| 236 | +restart Restart a running container # 重启运行的容器 |
| 237 | +rm Remove one or more containers # 移除一个或者多个容器 |
| 238 | +rmi Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除] |
| 239 | +run Run a command in a new container # 创建一个新的容器并运行一个命令 |
| 240 | +save Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load] |
| 241 | +search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像 |
| 242 | +start Start a stopped containers # 启动容器 |
| 243 | +stop Stop a running containers # 停止容器 |
| 244 | +tag Tag an image into a repository # 给源中镜像打标签 |
| 245 | +top Lookup the running processes of a container # 查看容器中运行的进程信息 |
| 246 | +unpause Unpause a paused container # 取消暂停容器 |
| 247 | +version Show the docker version information # 查看 docker 版本号 |
| 248 | +wait Block until a container stops, then print its exit code # 截取容器停止时的退出状态值 |
| 249 | +``` |
| 250 | +
|
0 commit comments