当前位置: 首页 > news >正文

2-Docker常用命令

1. Docker 帮助启动类命令

1.1 启动 docker: systemctl start docker

[root@localhost ~]# systemctl start docker

img

1.2 停止 docker: systemctl stop docker

[root@localhost ~]# systemctl stop docke

img

1.3 重启 docker: systemctl restart docker

[root@localhost ~]# systemctl restart docker

img

1.4 查看 docker 状态: systemctl status docker

[root@localhost ~]# systemctl status docker

img

1.5 开机启动: systemctl enable docker

[root@localhost ~]# systemctl enable docker

img

1.6 查看 docker 概要信息: docker info

[root@localhost ~]# docker info

img

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

[root@localhost ~]# docker --help

img

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

[root@localhost ~]# docker cp --help

img

img

2. Docker 镜像命令

2.1 docker images 列出本地主机已经存在/拉取到的镜像

[root@localhost ~]# docker images

img

docker images 各个选项说明:

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

对于同一仓库源可以有多个 TAG 版本,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像,如果你不指定一个镜像的版本标签,例如:你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像(latest 表示最新的版本)

OPTIONS 说明:

  1. -a : 列出本地所有的镜像(含历史映像层)
[root@localhost ~]# docker images -a

img

  1. -q : 只显示镜像ID
[root@localhost docker]# docker images -q

img

  1. aq: 和 -q 一样
[root@localhost docker]# docker images -aq

img

2.2 docker search 某个XXX镜像名字,从库当中查询某个镜像

仓库的官方地址:https://hub.docker.com 国外的网站,可能无法访问。

docker search [OPTIONS] 镜像名字

img

img

OPTIONS说明:

  • --limit : 只列出N个镜像,默认25个
docker search --limit 5 redis

2.3 docker pull 某个XXX镜像名字 ,下载/拉取某个镜像

docker pull 镜像名字[:TAG]

img

[root@localhost docker]# docker pull ubuntu

img

img

2.4 docker system df 查看镜像/容器/数据卷所占的空间

[root@localhost docker]# docker system df

img

2.5 docker rmi 某个XXX镜像名字ID

img

面试题:谈谈docker虚悬镜像是什么?

仓库名、标签都是<none> 的镜像,俗称虚悬镜像 dangling image

img

3. Docker 容器命令

注意:有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)

3.1 新建+启动容器 docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

OPTIONS说明(常用):有些是一个减号,有些是两个减号

--name="容器新名字" 为容器指定一个名称;
-d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行);

-i:以交互模式运行容器,通常与 -t 同时使用;
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;也即启动交互式容器(前台有伪终端,等待交互);

-it : 表示启用一个控制台进行交互

-P: 随机端口映射,大写P
-p: 指定端口映射,小写p

img

使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。

[root@localhost docker]# docker run -it ubuntu

img

img

[root@localhost docker]# docker run -it ubuntu /bin/bash

img

参数说明:

  • -i: 交互式操作。
  • -t: 终端。
  • ubuntu:ubuntu镜像。
  • /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。
  • --name : 指明容器实例的名称。
  • 要退出终端,直接输入 exit:
[root@localhost ~]# docker run -it --name="myu2" ubuntu bash

img

3.2 docker ps [OPTIONS] 列出当前所有正在运行的容器

[root@localhost ~]# docker ps

img

OPTIONS说明(常用):

-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q : 静默模式,只显示容器编号。

[root@localhost ~]# docker ps -a
[root@localhost ~]# docker ps -l
[root@localhost ~]# docker ps -n 2
[root@localhost ~]# docker ps -q

img

3.3 退出容器(两种方式,两种方式都是本身已经进入到容器当中了)

img

  • exit :退出容器的前提是,本身已经已经进入到容器当中了。exit 方式退出了容器,容器本身也将会停止
root@31b5c8683200:/# exit

img

img

  • ctrl+p+q:run进去容器,ctrl+p+q退出,容器不会停止

img

3.4 启动已停止运行的容器 docker start 容器ID或者容器名

[root@localhost ~]# docker start 7ae682c779cb

img

3.5 重启容器 docker restart 容器ID或者容器名

3.6 停止容器 docker stop 容器ID或者容器名

3.7 强制停止容器 docker kill 容器ID或容器名

3.8 删除已停止的容器 docker rm 容器ID

img

[root@localhost ~]# docker rm 357021879d41

img

[root@localhost ~]# docker rm -f compassionate_wilson

img

3.9 容器重点:掌握

前提:有镜像才能创建容器,这是根本前提(下载一个Redis6.0.8镜像演示)

[root@localhost ~]# docker pull redis:6.0.8

img

img

3.9.1 启动守护式容器(后台服务器)

在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以过-d 指定容器的后台运行模式。

img

**前台交互式启动:**docker run -d 容器名

[root@localhost ~]# docker run -d redis:6.0.8

img

使用镜像redis:6.0.8以后台模式启动一个容器
docker run -d redis:6.0.8

问题:然后docker ps -a 进行查看, 会发现容器已经退出很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail ),就是会自动退出的。

这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如service nginx start 但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用, 这样的容器后台启动后,会立即自杀因为他觉得他没事可做了。

所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行,常见就是命令行模式,表示我还有交互操作,别中断,O(∩_∩)O哈哈~

后台守护式启动: docker run -d redis:6.0.8

[root@localhost ~]# docker run -t redis:6.0.8

img

3.9.2 查看容器日志 docker logs 容器ID

[root@localhost ~]# docker logs 3aa02dfe9ea

img

3.9.3 查看容器内运行的进程 docker top 容器ID

[root@localhost ~]# docker top 3aa02dfe9ea5

img

3.9.4 查看容器内部细节 docker inspect 容器ID

[root@localhost ~]# docker inspect 3aa02dfe9ea5

img

3.9.5 进入“正在运行”的容器并以命令行交互 exec 和 attach

img

[root@localhost docker]# docker exec -it 7ae682c779cb /bin/bash

img


[root@localhost docker]# docker attach 7ae682c779cb

img

exec attach 两者都是重新进入正在运行的容器实例,两个区别是:

  • attach 直接进入容器启动命令的终端,不会启动新的进程,用exit退出,会导致容器的停止。

img

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

img

推荐大家使用 docker exec 命令,因为退出容器终端,不会导致容器的停止。

img

root@localhost docker]# docker exec -it 3aa02dfe9ea5 /bin/bash

img


[root@localhost ~]# docker exec -it 3aa02dfe9ea5 redis-cli

img

一般先用docker run -d 容器ID 后台启动的程序,再用 docker exec -it 容器ID /bin/bash 进入对应容器实例

3.9.6 从容器实例内拷贝文件到主机上—> docker cp 容器ID:容器内路径 目的主机路径

公式:docker cp 容器ID:容器内路径 目的主机路径

首先,进入到 ubuntu 实例当中。在 ubuntu 容器实例的 /tmp 路径当中创建一个a.txt 文件。

img

注意:是在主机的位置上执行该命令,不是在容器实例当中执行的。最好是先创建文件夹,再执行该指令,存放的文件夹要先创建出来。

img

[root@localhost demo02]# docker cp 4b6b2842843b:/tmp/a.txt /home/linux/demo02

3.9.7 导入(import )和导出(export)容器(全部信息)

img

注意:这是在主机当中执行的命令,不是在容器实例当中的。

  • export 导出容器的内容留作为一个 tar 归档文件[对应import命令]
root@localhost demo02]# docker export 4b6b2842843b  > temp02.ta

img

  • import 从tar包中的内容创建一个新的文件系统再导入为镜像[对应export]

img

ubuntu 实例删除后,我们将根据我们上面导出的 temp02.tar 文件信息,创建一个新的含有上面我们刚刚删除的 ubuntu 容器实例的全部内容(在其中的tmp文件当中,存在一个我们创建的一个a.tx 文件)。

[root@localhost demo02]# cat temp02.tar | docker import - rainbowsea/ubuntu:3.7

img

img

4. 总结:

  • -- :两个横杠,表示匹配的是,全部单词的指令
  • - : 单个横杠,表示匹配的是模糊匹配的指令
  • 同时注意:哪些指令是在容器实例当中执行的 ,哪些指令是在主机当中执行的指令

常用命令:

img

  • 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’s 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 # 从当前 Docker registry 退出
  • 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 版本号
    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 # 截取容器停止时的退出状态值

相关文章:

  • 30--当路由器学会“反侦察“:华为URPF协议配置全解
  • 2022 年 6 月青少年软编等考 C 语言七级真题解析
  • 2025年渗透测试面试题总结- 某58同城-安全工程师扩展(题目+回答)
  • 当AI助理接管云计算-走向智能运维的新时代
  • Spring AI Alibaba示例项目深度解析:helloworld子模块详解(经典解读版)
  • 2025年渗透测试面试题总结- 某深某服-漏洞研究员实习扩展(题目+回答)
  • Spring AI Alibaba示例项目深度解析:dashscope-audio子模块详解
  • Android学习之Material Components
  • PyTorch构建自定义模型
  • 从2G到5G:认证体系演进与网元架构变迁深度解析
  • 使用 iPerf 测试内网两台机器之间的传输速度
  • 2025大唐杯仿真4——信令流程
  • 调用阿里云API实现运营商实名认证
  • 现代科幻赛博朋克风品牌海报电子竞技设计无衬线英文字体 Glander – Techno Font
  • 论文导读 | SOSP23 | Gemini:大模型 内存CheckPoint 快速故障恢复
  • 2025年渗透测试面试题总结-某一线实验室实习扩展(题目+回答)
  • [ctfshow web入门] 零基础版题解 目录(持续更新中)
  • 树莓派5中部署 开源 RF-DETR 实时目标检测模型
  • MySQL窗口函数学习
  • [WUSTCTF2020]CV Maker1
  • 甘肃省作风建设年活动有网站/seo上海优化
  • 虚拟机如何做网站/免费b站推广网址有哪些
  • wordpress无法批量管理/宁波网站制作优化服务公司
  • 主营商城网站建设/百度推广费用可以退吗
  • 那些空号检测网站是怎么做的/百度推广怎么操作流程
  • 上海建站网/简述获得友情链接的途径