侧边栏壁纸
  • 累计撰写 101 篇文章
  • 累计创建 89 个标签
  • 累计收到 9 条评论

docker初级篇 - docker常用命令

bearjun
2022-07-02 / 0 评论 / 0 点赞 / 1,266 阅读 / 4,613 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-07-03,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

前面一节,我们已经讲了docker的安装和卸载,那安装好的docker怎么使用呢?这节我们一起来看看吧!

docker命令分类

docker的命令大致分为以下三类:

  • 帮助启动类命令:大致为docker的启动、停止、重启、状态和使用说明等等
  • 镜像命令:主要对镜像的操作,例如搜索、拉取、查看、删除等等
  • 容器命令:主要对容器的操作,例如:启动、停止、删除、进入容器内部等等

docker命令一览图

image.png
所有的命令大致为上图所述。
下面列了一张表,小伙伴们一起来看看吧。

命令英语说明中文说明
attachAttach to a running container当前 shell 下 attach 连接指定运行镜像
buildBuild an image from a Dockerfile通过 Dockerfile 定制镜像
commitCreate a new image from a container changes提交当前容器为新的镜像
cpCopy files/folders from the containers filesystem to the host path从容器中拷贝指定文件或者目录到宿主机中
createCreate a new container创建一个新的容器,同 run,但不启动容器
diffInspect changes on a container's filesystem查看 docker 容器变化
eventsGet real time events from the server从 docker 服务获取容器实时事件
execRun a command in an existing container在已存在的容器上运行命令
exportStream the contents of a container as a tar archive导出容器的内容流作为一个 tar 归档文件[对应 import ]
historyShow the history of an image展示一个镜像形成历史
imagesList images列出系统当前镜像
importCreate a new filesystem image from the contents of a tarball从tar包中的内容创建一个新的文件系统映像[对应export]
infoDisplay system-wide information显示系统相关信息
inspectReturn low-level information on a container查看容器详细信息
killKill a running containerkill 指定 docker 容器
loadLoad an image from a tar archive从一个 tar 包中加载一个镜像[对应 save]
loginRegister or Login to the docker registry server注册或者登陆一个 docker 源服务器
logoutLog out from a Docker registry server从当前 Docker registry 退出
logsFetch the logs of a container输出当前容器日志信息
portLookup the public-facing port which is NAT-ed to PRIVATE_PORT查看映射端口对应的容器内部源端口
pausePause all processes within a container暂停容器
psList containers列出容器列表
pullPull an image or a repository from the docker registry server从docker镜像源服务器拉取指定镜像或者库镜像
pushPush an image or a repository to the docker registry server推送指定镜像或者库镜像至docker源服务器
restartRestart a running container重启运行的容器
rmRemove one or more containers移除一个或者多个容器
rmiRemove one or more images移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
runRun a command in a new container创建一个新的容器并运行一个命令
saveSave an image to a tar archive保存一个镜像为一个 tar 包[对应 load]
searchSearch for an image on the Docker Hub在 docker hub 中搜索镜像
startStart a stopped containers启动容器
stopStop a running containers停止容器
tagTag an image into a repository给源中镜像打标签
topLookup the running processes of a container查看容器中运行的进程信息
unpauseUnpause a paused container取消暂停容器
versionShow the docker version information查看 docker 版本号
waitBlock until a container stops, then print its exit code截取容器停止时的退出状态值

注意:可以通过以上命令快速查询对应指令。

帮助启动类命令

# 启动docker: 
systemctl start docker

# 停止docker: 
systemctl stop docker

# 重启docker: 
systemctl restart docker

# 查看docker状态: 
systemctl status docker

# 开机启动: 
systemctl enable docker

# 查看docker概要信息: 
docker info

# 查看docker总体帮助文档: 
docker --help

# 查看docker命令帮助文档: 
docker 具体命令 --help

以上命令比较简单,不做详细说明。

镜像命令

镜像命令比较重要。一下会做详细的说明。

docker images

列出本地镜像。

# 基本语法
docker images [-a][-q]

# 可选参数:
[-a] : 列出本地所有的镜像(含历史映像层),差不多all的意思
[-q] : 只显示镜像ID。

image.png

  • REPOSITORY:表示镜像的仓库源
  • TAG:镜像的标签版本号
  • IMAGE ID:镜像ID
  • CREATED:镜像创建时间
  • SIZE:镜像大小

注意:同一仓库源可以有多个 TAG版本,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。

从docker远程仓库搜索镜像

# 基本语法
docker search [OPTIONS] [--limit number]

# 可选参数:
[OPTIONS] : 镜像的名称:版本号,如果不加,则为最新的版本
[--limit number] :只搜索指定条数的镜像,类似于数据库的limit

image.png

  • NAME: 镜像的名称(镜像的包名称 / 镜像名称)
  • DESCRIPTION: 镜像的说明
  • STARS: 点赞数
  • OFFICIAL: 是否官方
  • AUTOMATED: 是否是自动构建的

docker pull

拉取/下载某个镜像

# 基本语法
docker pull 镜像名字[:TAG]

# 可选参数:
[:TAG] : 版本号,如果不加,则为最新的版本

image.png

docker system df

查看镜像/容器/数据卷所占的空间

# 基本语法
docker system df

image.png

docker rmi

删除镜像

# 基本语法
docker rmi  -f 镜像ID

# 删除多个
docker rmi -f 镜像名1:TAG 镜像名2:TAG

# 删除全部
docker rmi -f $(docker images -qa)

image.png

容器命令

容器命令包括容器的运行,删除,进入内容,复制等一些很重要也很常见的操作。

docker run

新建并启动一个容器。

# 基本语法
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

# 可选参数[OPTIONS],以下参数可多选:
--name="容器新名字" : 为容器指定一个名称;
-d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行);
-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;也即启动交互式容器(前台有伪终端,等待交互);
-P: 随机端口映射,大写P
-p: 指定端口映射,小写p

image.png

docker ps

列出当前所有正在运行的容器

# 基本语法
docker ps [OPTIONS]

# 可选参数[OPTIONS],以下参数可多选:
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器
-n:显示最近n个创建的容器
-q :静默模式,只显示容器编号

image.png

exit/ctrl+p+q

退出容器

# 基本语法
## run进去容器,exit退出,容器停止
exit 

## run进去容器,ctrl+p+q退出,容器不停止
ctrl+p+q

image.png

docker start/restart/stop

启动已停止运行的容器/重启容器/强制停止容器

# 基本语法
docker start/restart/stop 容器ID或者容器名

image.png

docker kill/rm

强制停止容器/删除已停止的容器

# 基本语法
## 强制停止容器
docker kill 容器ID或者容器名

## 删除已停止的容器
docker rm -f 容器ID或者容器名

### 一次性删除多个容器实例
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm

image.png

注意:这两个命令只能停止或者删除已经存在的容器。

docker logs

查看容器日志

# 基本语法
docker logs 容器ID

image.png

docker top

查看容器内运行的进程

# 基本语法
docker top 容器ID

image.png

docker inspect

查看容器内部细节

# 基本语法
docker inspect 容器ID

image.png

docker cp

从容器内拷贝文件到主机上。从容器内部拷贝到主机

# 基本语法
docker cp  容器ID:容器内路径 目的主机路径

image.png

export/import

导入和导出容器

# 基本语法
## 导出:导出容器的内容留作为一个tar归档文件[对应import命令]
docker export 容器ID > 文件名.tar.gz

## 导入:从tar包中的内容创建一个新的文件系统再导入为镜像[对应export]
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号

image.png

注意事项

前面说了,当我们运行一些容器的时候,我们需要返回一个可以输入终端的操作界面。

注意点1

启动交互式容器(前台命令行):我们启动容器通过添加-it获取交互式容器。例如启动ubuntu
启动守护式容器(后台服务器):我们启动容器通过添加-d获取守护式容器。类似于启动jar的守护式,关闭shell窗口后,应用不停止,继续运行。

注意点2

例如我们启动了一个ubuntu,并且采用的数交互式启动,那我们关闭了交互式的命令窗口,再怎么打开呢?

  • docker exec -it 容器ID bashShell
  • docker attach 容器ID

image.png
通过上图可以看出:

  • attach 直接进入容器启动命令的终端,不会启动新的进程用exit退出,会导致容器的停止。
  • exec 是在容器中打开新的终端,并且可以启动新的进程用exit退出,不会导致容器的停止。

需要注意的是:使用ctrl + Q + P,容器是不会退出的。推荐大家使用docker exec命令,因为退出容器终端,不会导致容器的停止。

0

评论区