Docke详解

此文章发布于9个月前,部分信息可能已经过时,请自行斟酌确认

Docker文档

文档概述

本文档的学习内容来源于 bilibili UP主: 遇见狂神说_bilibili

学习视频链接: https://www.bilibili.com/video/BV1og4y1q7M4?share_source=copy_web

本文档由晴栀同学 学习整理.

Docker官网:https://www.docker.com/

Docker官方使用文档:https://docs.docker.com/reference/

Docker官方安装文档: https://docs.docker.com/get-docker/

Docker安装

Docker的基本组成

image-20211220155201645

镜像(images):

docker镜像好比是一个模板,可以通过这个模板来创建容器服务,镜像 ===> run ==> 容器01 (提供服务)

一个镜像可以创建多个容器 (最终服务运行或者项目运行就是在容器中的).

容器(container):

docker利用容器技术,独立运行一个或者一组应用.通过镜像来创建的.

容器有 启动 停止 删除 等基本命令

目前容器可以理解为就是一个微型简易的linux系统.

仓库(repository):

仓库 就是用来存放 镜像 的地方.

仓库有 公有仓库 和 私有仓库

Docker Hub 是docker的默认仓库.(国内使用最好配置镜像加速)

阿里云 等国内大厂也有自己的容器服务器.

环境准备

学习基础

  1. 需要会基本的linux操作基础.
  2. 部署一个Centos 7 的服务器(虚拟机上的也可以)
  3. 会使用Xshell或者FinalShell等工具连接Centos 7服务器进行操作

环境查看

# 系统内核 3.1.以上
[root@VM-0-8-centos ~]# uname -r
3.10.0-1127.19.1.el7.x86_64
# 系统版本
[root@VM-0-8-centos ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

安装Docker

安装

帮助文档:https://docs.docker.com/engine/install/centos/

# 1. 卸载旧版本docker
yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# 2. 需要的安装包
yum install -y yum-utils

# 3. 设置镜像的仓库地址 

#     3.1 国外地址不推荐使用
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo #国外的仓库地址很慢很慢
#     3.2 阿里云镜像地址推荐使用
yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #阿里云镜像地址,国内速度相对还可以

# 此时可以更新一下安装包索引(非必须,自行选择是否更新)

yum makecache fast

# 4. 安装docker服务 
#    docker-ce 代表为社区版 docker-ee则代表企业版
yum install docker-ce docker-ce-cli containerd.io

#    如果想要指定版本安装 例如指定版本为 18.09.1
yum install docker-ce-18.09.1 docker-ce-cli-18.09.1 containerd.io

# 5. 启动docker
systemctl start docker

# 6. 使用docker version 测试是否启动成功
docker version
[root@VM-0-8-centos ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:41 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:44:05 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
# 7. 测试启动 hello-world
docker run hello-world
[root@VM-0-8-centos ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
# 8. 查看一下下载的镜像 docker images
[root@VM-0-8-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

卸载Docker

# 1. 卸载docker依赖
yum remove docker-ce docker-ce-cli containerd.io

# 2. 删除资源
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

#  /var/lib/docker 是docker默认工作路径

阿里云镜像加速

  1. 登录 阿里云
  2. 进入控制台
  3. 点击左上角的
  4. 点击进入 [容器镜像服务]容器镜像服务 (aliyun.com)
  5. 菜单栏左侧有 镜像工具 ==> [镜像加速器]容器镜像服务 (aliyun.com)
  6. 使用方式参考页面上的操作文档,根据自己服务器类型选择.

底层原理

Docker是怎样工作的?

Docker是一个Clint - Server 结构的系统,Docker的守护进程运行在主机上.通过Socker从客户端访问.

DockerServer接收到 Docker - Client 的指令,就会执行这个命令!

Docker常用命令

帮助命令

docker version            # 显示docker的版本信息
docker info                # 显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help       # 帮助命令

官方命令文档: https://docs.docker.com/reference/

image-20211220183830067

镜像命令


images

命令解释: 列出本地所有镜像

[root@VM-0-8-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

# 解释
REPOSITORY    #镜像的仓库源
TAG            #镜像的标签
IMAGE ID    #镜像的ID
CREATED        #镜像的创建时间
SIZE        #镜像的大小

# 可选项
[root@VM-0-8-centos ~]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             # 列出所有镜像 (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           #只显示镜像的ID

search

命令解释: 在镜像仓库中搜索镜像

建议在网页端搜索: https://hub.docker.com/search?q=&type=image

命令搜索显示的结果没有网页端详细.

[root@VM-0-8-centos ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11840     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4512      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   887                  [OK]
phpmyadmin                        phpMyAdmin - A web interface for MySQL and M…   396       [OK]       

# 可选项
[root@VM-0-8-centos ~]# 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
      
# 可选项示例

docker search mysql --filter=STARS=4000        #这样就是搜索mysql镜像并且stars数不小于4000的

pull

命令解释: 下载镜像仓库中的镜像

# 下载镜像 docker pull 镜像名[:tag] 下面的示例中是指定mysql 5.7 版本 如果不指定版本 则默认为 latest (最新版)
# 如下为指定mysql版本下载示例
[root@VM-0-8-centos ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94   #这里代表签名
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7   #真实地址 

# 真实地址解释
docker pull mysql:5.7    等价于    docker pull docker.io/library/mysql:5.7

#如下为不指定mysql版本下载示例
[root@VM-0-8-centos ~]# docker pull mysql
Using default tag: latest   #因为未指定版本下载 所以这里默认为 latest  (最新版)
latest: Pulling from library/mysql
72a69066d2fe: Already exists 
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709  #这里代表签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest   #真实地址

# 真实地址解释
docker pull mysql    等价于    docker pull docker.io/library/mysql:latest

rmi

命令解释: 删除镜像

<u>词语速记技巧: ==rm==为linux中的删除命令 ==i== 可以理解为 images 的起始字母 所以==rmi==为删除镜像</u>

# 先使用images命令查看已下载镜像的信息
[root@VM-0-8-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   28 hours ago   448MB
mysql         latest    3218b38490ce   28 hours ago   516MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
#删除了镜像ID为 3218b38490ce 的镜像
[root@VM-0-8-centos ~]# docker rmi 3218b38490ce
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Deleted: sha256:aa81ca46575069829fe1b3c654d9e8feb43b4373932159fe2cad1ac13524a2f5
Deleted: sha256:0558823b9fbe967ea6d7174999be3cc9250b3423036370dc1a6888168cbd224d
Deleted: sha256:a46013db1d31231a0e1bac7eeda5ad4786dea0b1773927b45f92ea352a6d7ff9
Deleted: sha256:af161a47bb22852e9e3caf39f1dcd590b64bb8fae54315f9c2e7dc35b025e4e3
Deleted: sha256:feff1495e6982a7e91edc59b96ea74fd80e03674d92c7ec8a502b417268822ff
# 再次查看镜像信息发现该镜像已经成功删除
[root@VM-0-8-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   28 hours ago   448MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB

# 可选项示例
[root@VM-0-8-centos ~]# docker rmi --help

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
docker rmi 镜像id                            #删除指定的镜像id
docker rmi -f 镜像id                        #强制删除指定的镜像id
docker rmi -f 镜像id 镜像id 镜像id          #删除多个镜像 (可以不带 -f ,-f代表强制删除)
docker rmi -f $(docker images -aq)       #删除所有镜像

容器命令

说明: 我们有了镜像才可以创建容器.


run

命令解释: 根据镜像新建并启动一个容器.

docker run [可选参数] 镜像

# 参数说明
--name="YouName"     容器名字 name01 name02 ,用来区分容器
-d                    后台方式运行
-it                    使用交互方式运行,进入容器查看内容
-p                    小写p,指定容器的端口 例如: -p 8080:8080 (主机端口:容器端口)
    如下为-p的多种使用方式:
    -p ip:主机端口:容器端口        
    -p 主机端口:容器端口    (这一种最常用)
    -p 容器端口
    容器端口        (直接写容器端口也可,用的很少)
-P                    大写P,随机指定端口
-v                    设置路径挂载  -v 主机挂载路径:容器内路径
# 查看本机已下载的镜像
[root@VM-0-8-centos ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   28 hours ago   448MB
hello-world   latest    feb5d9fea6a5   2 months ago   13.3kB
centos        latest    5d0da3dc9764   3 months ago   231MB
# 使用名称为 centos 的镜像 并创建一个名称为 centos01 的容器. --name指定名称;  -it代表交互方式运行; 
# /bin/bash代表使用bash方式交互; centos 代表使用镜像名称 (注意:因为镜像中centos的TAG为latest,所以在启动时可以直接写成centos; 如果要启动上面的TAG为5.7的mysql,则应写成 docker run --name="mysql01" -it mysql:5.7 /bin/bash )
[root@VM-0-8-centos ~]# docker run --name="centos01" -it centos /bin/bash
[root@7ea0fe54fd52 /]# ls        #这里已经进入了容器内部
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@7ea0fe54fd52 /]# 

exit

退出容器命令:

  • 键盘按键 Ctrl + P + Q (小白提示 + 号不用按) # 容器不停止 退出当前容器
  • 命令输入 exit # 容器停止并退出

ps

命令解释: 显示所有运行的容器

# docker ps 命令
# 可选命令
        #无任何命令代表 列处当前运行中的容器
-a         #列出当前正在运行中的容器+历史运行过的容器
-n=?    # ? 处写数字 列出最近创建的 ? 个容器
-q        #只显示容器的编号
#ps  使用示例
[root@VM-0-8-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@VM-0-8-centos ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                     PORTS     NAMES
7ea0fe54fd52   centos        "/bin/bash"   14 minutes ago   Exited (0) 7 minutes ago             centos01
3900a1c3a846   hello-world   "/hello"      45 hours ago     Exited (0) 45 hours ago              zealous_rubin

rm

命令解释: 删除容器

docker rm 容器id                    #删除指定容器 不能删除正在运行的容器
docker rm -f 容器id                #强制删除指定容器 可以删除正在运行的容器
docker rm -f $(docker ps -aq)     #强制删除所有容器
docker ps -aq | xargs docker rm  #强制删除所有容器

start

restart

stop

kill

docker start 容器ID        #启动容器
docker restart 容器ID        #重启容器
docker stop 容器ID        #停止正在运行的容器
docker kill 容器ID        #强制停止正在运行的容器

其他常用命令

logs

命令解释: 查看日志

docker logs [可选项] 容器ID或容器名称

# 可选项

-f         #跟踪日志的输出
-n        #显示最近的日志行数 -n 后面需要指定一个数字
-t        #显示日志相关的时间信息

[root@VM-0-8-centos ~]# 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:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --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:37Z) or relative (e.g. 42m for 42 minutes)
# 我先用ps命令看了一下正在运行的容器
[root@VM-0-8-centos ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
873615df7a09   centos    "/bin/bash"   3 minutes ago   Up 3 minutes             os-01
# 打印最近10条日志 -f -t 可以简化成 -ft; --tail 10 代表只看最近10条日志;  os-01代表查看名称为os-01的日志(我这里使用的是容器的NAMES,这里也可以用容器的ID,都可以的)
[root@VM-0-8-centos ~]# docker logs -f -t --tail 10 os-01
[root@873615df7a09 /]# echo 111
2021-12-22T08:03:46.989056874Z 111
2021-12-22T08:03:51.024695915Z [root@873615df7a09 /]# echo 1111
2021-12-22T08:03:51.024724031Z 1111
2021-12-22T08:03:53.954727367Z [root@873615df7a09 /]# echo 222 
2021-12-22T08:03:53.954771161Z 222
2021-12-22T08:04:01.002293474Z [root@873615df7a09 /]# echo 打印日志
2021-12-22T08:04:01.002325350Z 打印日志
2021-12-22T08:04:06.660306724Z [root@873615df7a09 /]# echo 打印日志q
2021-12-22T08:04:06.660346906Z 打印日志q

top

命令解释: 查看容器中的进程信息

docker top [可选项] 容器ID或容器NAMES

#可选项  
#top命令的可选项用的不多 一般都是直接 docker top 容器ID   查看容器内部进程
[root@VM-0-8-centos ~]# docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print containers using a Go template
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes
# 先查看目前运行中的容器
[root@VM-0-8-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
873615df7a09   centos    "/bin/bash"   18 minutes ago   Up 18 minutes             os-01
# 使用top命令查看容器内的进程信息
[root@VM-0-8-centos ~]# docker top 873615df7a09
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7999                7979                0                   16:02               pts/0               00:00:00            /bin/bash

inspect

命令解释: 查看容器的元数据

#命令使用方式
docker inspect 容器ID
#先查看了一下正在运行的容器
[root@VM-0-8-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS             PORTS     NAMES
873615df7a09   centos    "/bin/bash"   About an hour ago   Up About an hour             os-01
#查看容器的元数据
[root@VM-0-8-centos ~]# docker inspect 873615df7a09
[
    {
        "Id": "873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90",
        "Created": "2021-12-22T08:02:42.62301867Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 7999,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-12-22T08:02:42.930504307Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90/hostname",
        "HostsPath": "/var/lib/docker/containers/873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90/hosts",
        "LogPath": "/var/lib/docker/containers/873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90/873615df7a09c929b271f8edb28ae7badf684f751f0d034420470fab99b74e90-json.log",
        "Name": "/os-01",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/a47df1e022413680de32a018ce16b5b4044d55d668ff9cff40f40802f0861df8-init/diff:/var/lib/docker/overlay2/1b8ca2b9c968eb18058e2e775240e8157dc8d9735570489513d71cf6b9fefc10/diff",
                "MergedDir": "/var/lib/docker/overlay2/a47df1e022413680de32a018ce16b5b4044d55d668ff9cff40f40802f0861df8/merged",
                "UpperDir": "/var/lib/docker/overlay2/a47df1e022413680de32a018ce16b5b4044d55d668ff9cff40f40802f0861df8/diff",
                "WorkDir": "/var/lib/docker/overlay2/a47df1e022413680de32a018ce16b5b4044d55d668ff9cff40f40802f0861df8/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "873615df7a09",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "d112c334662dbfa3af8e22fb9dd2ffec093b649f751c05d6e863f0befb48ff63",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/d112c334662d",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "0f32598f3ddfb56c71fb53dd9169ffe36c4bb27fd63445698da787cf9a6ac00e",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "2a3c64e54541bf5f5887f6678dfd55f4afee0018369e79928341054740c547b1",
                    "EndpointID": "0f32598f3ddfb56c71fb53dd9169ffe36c4bb27fd63445698da787cf9a6ac00e",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

exec

命令解释: 进入正在运行的容器

# 容器通常是使用后台方式运行的.如果我们需要进入容器修改一些配置,则可使用该命令

# 命令  -it 代表使用交互模式进入容器 /bin/bash 代表使用bash交互进入

docker exec -it 容器ID /bin/bash
#查看目前正在运行的容器
[root@VM-0-8-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
873615df7a09   centos    "/bin/bash"   2 hours ago   Up 2 hours             os-01
# 使用exec命令并根据容器ID进入容器
[root@VM-0-8-centos ~]# docker exec -it 873615df7a09 /bin/bash
[root@873615df7a09 /]#                     #    <===可以看到此时已经进入容器;root@后面内容已经变成容器ID了

attach

命令解释: 进入正在运行的容器

docker attach 容器ID
# 使用ps查看正在运行的容器
[root@VM-0-8-centos ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
873615df7a09   centos    "/bin/bash"   2 hours ago   Up 2 hours             os-01
# 进入容器ID为873615df7a09的容器
[root@VM-0-8-centos ~]# docker attach 873615df7a09
[root@873615df7a09 /]#                     #    <===可以看到此时已经进入容器;root@后面内容已经变成容器ID了

execattach区别

docker exec  #进入容器后开启一个新的终端,(相当于开启一个新命令窗口,所以在此窗口中exit只会退出该新增的窗口,不会停止容器)
docker attach #进入容器正在执行的终端 不会启动新的进程 (此命令进入容器后,如果exit是可能会停止容器的)

cp

命令解释: 从容器内拷贝文件到宿主机上

docker cp 容器ID:容器内文件路径 宿主机内保存的路径
# 查看当前文件夹下内容
[root@VM-0-8-centos dockerps]# ls
# 查看目前正在运行的容器
[root@VM-0-8-centos dockerps]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS       PORTS     NAMES
873615df7a09   centos    "/bin/bash"   2 hours ago   Up 2 hours             os-01
# 进入容器
[root@VM-0-8-centos dockerps]# docker exec -it 873615df7a09 /bin/bash
# 进入容器中的/home文件夹
[root@873615df7a09 /]# cd /home/
# 查看容器内/home文件夹内的内容
[root@873615df7a09 home]# ls
# 容器内创建一个名称为 test01.txt 的文件 
[root@873615df7a09 home]# touch test01.txt
# 查看文件已创建成功
[root@873615df7a09 home]# ls
test01.txt
# 退出容器 (因为使用的exec进入的容器,所以此时exit容器并不会停止)
[root@873615df7a09 home]# exit
exit
# 将容器内新创建的文件复制到当前文件夹
[root@VM-0-8-centos dockerps]# docker cp 873615df7a09:/home/test01.txt /home/dockerps/test01.txt
[root@VM-0-8-centos dockerps]# ls
test01.txt
[root@VM-0-8-centos dockerps]# 

未完待续...

最后修改:2021 年 12 月 22 日 06 : 52 PM
如果觉得我的文章对你有用,请随意赞赏

此处评论已关闭