【Docker】Docker入门

狂神老师的视频地址:https://space.bilibili.com/95256449

1、Docker历史和定义?

1.1、历史

2010年,美国成立dotClound公司,做一些pass的云计算服,LXC有关的容器技术。

他们将自己的容器技术命名为Docker

2013年,Docker开源,逐渐火热

2014年Docker1.0发布

2.1、相关概念

官方解释:

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows操作系统的机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口

官网文档地址:https://docs.docker.com/
仓库地址:https://hub.docker.com/

相关名词:

镜像(image):可以理解为集装箱,从本质上来说镜像就是一系列文件,可以包括我们应用程序的文件,也可以包括我们应用的运行环境的文件

仓库(registry):可以理解为码头,先把我们的镜像传到Docker仓库中,再由目的地把Docker仓库拉过去,这就完成了这样的一次传输过程 (分为公共的和私有的,官方的比较卡,建议使用阿里镜像仓库)

容器(containers):就是运行程序的地方,为了便于理解,大家可以把容器想象成一个简易的Linux

简而概之, Docker运行程序的过程就是去仓库把镜像拉到本地,然后用一条命令把镜像运行到容器中.

2、Docker和虚拟区的区别

  • 传统虚拟机,虚拟出一套硬件,运行一个完整的操作系统,在操作系统上安装和运行软件,如图一

  • 容器内的应用直接运行在宿主机,没有自己的内核,利用的是宿主机的内核,也没有虚拟硬件,更为轻便

  • 每个容器互相隔离,每个容器内都有属于自己的一套文件系统,互不影响

优势:

  • 更快捷的交付和部署,打包镜像发布测试,一键运行

  • 升级和扩容非常方便

  • 容器化之后,开发和测试环境是高度一致的

  • Docker是内核级别的虚拟化,可以在一个物理机上运行很多的容器实例!服务器性能可以被使用到极致

3、安装Docker

环境准备(这里用超融合虚拟机)

内核3.0以上,centos7

[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
[root@localhost ~]# 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"

具体安装步骤:

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

#安装yum工具 
sudo yum install -y yum-utils

#设置镜像仓库
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #这里是阿里镜像网站

#更新yum软件包索引
yum makecache fast

#安装Docker相关内容 docker--ce社区版,docker-ee企业版
sudo yum install docker-ce

#启动docker
sudo systemctl start docker

#查看docker版本
[root@localhost yum.repos.d]# docker version
Client: Docker Engine - Community
 Version:           20.10.16
 API version:       1.41
 Go version:        go1.17.10
 Git commit:        aa7e414
 Built:             Thu May 12 09:19:45 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.16
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.10
  Git commit:       f756502
  Built:            Thu May 12 09:18:08 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.4
  GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
 runc:
  Version:          1.1.1
  GitCommit:        v1.1.1-0-g52de29d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

#hello world
[root@localhost yum.repos.d]# sudo docker run hello-world

Hello from Docker!

#查看下载的hello world镜像
[root@localhost yum.repos.d]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   8 months ago   13.3kB

卸载docker

 sudo yum remove docker-ce docker-ce-cli containerd.io
 sudo rm -rf /var/lib/docker
 sudo rm -rf /var/lib/containerd

4、Run流程和Docker原理

如上文安装的时候跑了一个测试镜像 hello world,打印信息如下

Run原理

Docker原理

Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问。
需要说明的是,这里的客户端其实就相当于访问Mysql的终端,Mysql的进程就相当于server

5、docker基本命令

帮助文档地址:https://docs.docker.com/engine/reference/run/

docker version  #显示版本信息
docker info     #显示更详细信息
docker 命令 --help

5.1、镜像基本命令

docker images

#查看镜像
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
tomcat        9.0       a32ad1e7bf43   9 days ago     685MB
tomcat        latest    5eb506608219   9 days ago     685MB
hello-world   latest    feb5d9fea6a5   8 months ago   13.3kB
#参数
Options:
  -a, --all             Show all images (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           Only show image IDs

docker search

#搜索镜像
[root@localhost ~]# docker search mysql
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                          MySQL is a widely used, open-source relation…   12652     [OK]       
mariadb                        MariaDB Server is a high performing open sou…   4856      [OK]       
[root@localhost ~]# docker search mysql --filter=stars=3000  #搜索收藏数大于3000的
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   12652     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4856      [OK]     

docker pull

#下载镜像
[root@localhost ~]# docker pull mysql
Using default tag: latest  #如果不写tag,默认下载最新版
latest: Pulling from library/mysql
c32ce6654453: Pull complete   #分层下载,docker image的核心,联合文件系统
415d08ee031a: Pull complete 
7a38fec2542f: Pull complete 
352881ee8fe9: Pull complete 
b8e20da291b6: Pull complete 
66c2a8cc1999: Pull complete 
d3a3a8e49878: Pull complete 
e33a48832bec: Pull complete 
410b942b8b28: Pull complete 
d5323c9dd265: Pull complete 
e51041021063: Pull complete 
b68b4cbc496e: Pull complete 
Digest: sha256:dc3cdcf3025c3257e8047bb0eaee9d5a42d9f694f84fc5e7b6d12710ba7f6fcb
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  #真实地址
#下载指定版本的镜像
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
c32ce6654453: Already exists  #重复的内容不下载,这就是联合文件的好处
415d08ee031a: Already exists 
7a38fec2542f: Already exists 
352881ee8fe9: Already exists 
b8e20da291b6: Already exists 
66c2a8cc1999: Already exists 
d3a3a8e49878: Already exists 
172aabfba65c: Pull complete 
fea17d0b1d1e: Pull complete 
9e875a69819c: Pull complete 
ff8480c408a2: Pull complete 
Digest: sha256:7815f2f82a78eb5e5ebccf6ec6352d94691faf1232e80335ab36a4dfdc0b6723
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi

#删除镜像
[root@localhost ~]# docker rmi -f mysql
Untagged: mysql:latest
Untagged: mysql@sha256:dc3cdcf3025c3257e8047bb0eaee9d5a42d9f694f84fc5e7b6d12710ba7f6fcb
Deleted: sha256:b2500a44757fb9f9eef2089840ea3d7f1f53f36000e500853904786a291a7093
Deleted: sha256:cdd29f3cfa1d360dc591dfa7d97df34097aec9b0a10c5a97b639c1ac2f6229d0
Deleted: sha256:cdb4b319e813cff76042a326e164c92cda2706e546b1e5bcc8def89a7a112675
Deleted: sha256:823dda8b24627ebb50c789527c36964c16914ff1061422eca9bc494be1166a96
Deleted: sha256:ac856325eabcbafe6cefe589df92498eeacf1f741a283faedd9603ed01cc27e1
Deleted: sha256:f9774446a1968284a82961f2b48526ff6402725f258cbd536ce891afda4c5239
#删除多个镜像
[root@localhost ~]# docker rmi -f 容器id 容器id ...
#删除所有镜像
[root@localhost ~]# docker rmi -f $(docker images -aq)

5.2、容器命令

有了镜像才可以创建容器,下载一个centos来学习

docker pull centos

将镜像放入容器并启动容器

docker run [可选参数] image

#参数说明
--name="Name" 容器名字 tomcat1 tomcat2,用来区分容器
-d            后台方式运行
-it           使用交互方式运行
-p            指定容器端口,不填就随机端口
      -p 主机端口:容器端口(最常用)
      -p ip:主机端口:容器端口
      -p 容器端口
      或者直接 容器端口

#测试,启动并加入容器
[root@localhost ~]# docker run -it centos
[root@96a964f73273 /]# ls #查看容器内的centos,基本版本,很多命令都是不全的
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@96a964f73273 /]# exit #退出容器回到主机
exit

列出所有的运行的容器

# docker ps
-a    #显示所有的容器
-n=? #显示固定数量的容器
-q    #只显示容器的编号
-aq   #所有当前运行的容器编号

[root@localhost ~]# docker ps  #正在运行
CONTAINER ID   IMAGE     COMMAND             CREATED      STATUS      PORTS                                       NAMES
3e659456c63e   tomcat    "catalina.sh run"   6 days ago   Up 6 days   0.0.0.0:3355->8080/tcp, :::3355->8080/tcp   tomcat01
[root@localhost ~]# docker ps -a  
CONTAINER ID   IMAGE         COMMAND             CREATED         STATUS                          PORTS                                       NAMES
96a964f73273   centos        "/bin/bash"         4 minutes ago   Exited (0) About a minute ago                                               nervous_euclid
3e659456c63e   tomcat        "catalina.sh run"   6 days ago      Up 6 days                       0.0.0.0:3355->8080/tcp, :::3355->8080/tcp   tomcat01
bbee43839eb3   hello-world   "/hello"            6 days ago      Exited (0) 6 days ago                                                       elastic_bartik
0f566b446fab   hello-world   "/hello"            6 days ago      Exited (0) 6 days ago                                                       vibrant_borg

退出容器

exit          #直接容器停止推出
ctrl + p + q  #容器不停止退出

删除容器

docker rm 容器id                   #删除指定容器,不能删除运行中的容器
docker rm -f $(docker ps -aq)      #删除所有的容器
docker ps -a -q | xargs docker rm  #用管道命令删除所有的容器

启动和停止容器

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

查看日志

# docker logs

#运行容器centos,并且执行shell脚本
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo this is log test;sleep 1;done"       
5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9
#查看容器,因为有脚本运行
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
5a98701a43ea   centos    "/bin/sh -c 'while t…"   14 seconds ago   Up 13 seconds             goofy_pascal

#查看容器里的十条日志
-tf            #显示全部,带上时间戳
--tail number  #查看指定条数的日志
[root@localhost ~]# docker logs -t -f --tail 10 5a98701a43ea                                                
2022-06-01T02:44:16.808749307Z this is log test
2022-06-01T02:44:17.813069428Z this is log test
2022-06-01T02:44:18.819123669Z this is log test
^C

查看容器内的进程信息

[root@localhost ~]# docker top 5a98701a43ea 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                29341               29322               0                   10:43               ?                   00:00:00            /bin/sh -c while true;do echo this is log test;sleep 1;done
root                29750               29341               0                   10:49               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看容器信息

docker inspect

点击查看代码
[root@localhost ~]# docker inspect 5a98701a43ea
[
    {
        "Id": "5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9",
        "Created": "2022-06-01T02:43:48.191370524Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo this is log test;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 29341,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-06-01T02:43:48.654817101Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9/hostname",
        "HostsPath": "/var/lib/docker/containers/5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9/hosts",
        "LogPath": "/var/lib/docker/containers/5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9/5a98701a43ea67d45debc252e1f1b02d32ce2fa28b05371e020692cbacc2deb9-json.log",
        "Name": "/goofy_pascal",
        "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/5ac3c670d31b75a0b861146ef1f5c21745066d53bcaa3c40521f4f5ac0dac454-init/diff:/var/lib/docker/overlay2/cfc279d36cda774ffbc04e3202814d5ed23cc38facd80c7e44ff9a065cd79eaf/diff",
                "MergedDir": "/var/lib/docker/overlay2/5ac3c670d31b75a0b861146ef1f5c21745066d53bcaa3c40521f4f5ac0dac454/merged",
                "UpperDir": "/var/lib/docker/overlay2/5ac3c670d31b75a0b861146ef1f5c21745066d53bcaa3c40521f4f5ac0dac454/diff",
                "WorkDir": "/var/lib/docker/overlay2/5ac3c670d31b75a0b861146ef1f5c21745066d53bcaa3c40521f4f5ac0dac454/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "5a98701a43ea",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo this is log test;sleep 1;done"
            ],
            "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": "59c1cbdcc919b26b53c45547933c425e3cf6cbfae47c228754531d145bcd8580",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/59c1cbdcc919",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f1a039781626a34eaba2aa1a90792104734c9ffeacdd2c0352396f19eba8da0e",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "5c11da2a20eef8b14fcf0a693de82b265bf2a4e5af406557472ed65d81f54b4c",
                    "EndpointID": "f1a039781626a34eaba2aa1a90792104734c9ffeacdd2c0352396f19eba8da0e",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

注意事项

#后台启动容器
docker run -d centos
docker ps 后会发现centos停止了
#docker容器后台运行,必须有一个对应的前台进程,都在docker发现没有应用,就会自动停止,例如想nginx。

进入当前正在运行的容器

#通常运行的容器都是后台运行的,需要进入容器,修改一些配置

#命令
docker exec -it 容器id bashshell     #会打开一个新的终端

#测试
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS     NAMES
5a98701a43ea   centos    "/bin/sh -c 'while t…"   2 hours ago   Up 2 hours             goofy_pascal
[root@localhost ~]# docker exec -it 5a98701a43ea /bin/bash
[root@5a98701a43ea /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

方法二

docker attach 容器id                 #会进入正在运行的终端
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS     NAMES
5a98701a43ea   centos    "/bin/sh -c 'while t…"   3 hours ago   Up 3 hours             goofy_pascal
[root@localhost ~]# docker attach 5a98701a43ea
this is log test
this is log test
this is log test

从容器内拷贝文件到主机

# docker cp 容器id:容器文件路径 目的主机路径

#创建容器并进入
[root@localhost ~]# docker run -it centos /bin/bash
[root@d820215ccf61 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@d820215ccf61 /]# cd /home
#创建文件
[root@d820215ccf61 home]# touch test.java
[root@d820215ccf61 home]# ls
test.java
#返回到主机
[root@d820215ccf61 home]# [root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
d820215ccf61   centos    "/bin/bash"   58 seconds ago   Up 56 seconds             nostalgic_taussig
#从容器内复制文件到主机
[root@localhost ~]# docker cd d820215ccf61:/home/test.java /home
docker: 'cd' is not a docker command.
See 'docker --help'
[root@localhost ~]# docker cp d820215ccf61:/home/test.java /home
[root@localhost ~]# ls /home
shenhao  temp1  test.java

#拷贝是一个手动过程,后续使用-v卷的技术可以实现自动同步

5.3、命令小结

attach      Attach local standard input, output, and error streams to a running container
  #当前shell下 attach连接指定运行的镜像
  build       Build an image from a Dockerfile # 通过Dockerfile定制镜像
  commit      Create a new image from a container's changes #提交当前容器为新的镜像
  cp          Copy files/folders between a container and the local filesystem #拷贝文件
  create      Create a new container #创建一个新的容器
  diff        Inspect changes to files or directories on a container's filesystem #查看docker容器的变化
  events      Get real time events from the server # 从服务获取容器实时时间
  exec        Run a command in a running container # 在运行中的容器上运行命令
  export      Export a container's filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
  history     Show the history of an image # 展示一个镜像形成历史
  images      List images #列出系统当前的镜像
  import      Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
  info        Display system-wide information # 显示全系统信息
  inspect     Return low-level information on Docker objects #查看容器详细信息
  kill        Kill one or more running containers # kill指定docker容器
  load        Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
  login       Log in to a Docker registry #
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

6、Portainer可视化面板安装

安装命令

[root@localhost ~]# docker run -d -p 8088:9000 \--restart=always -v /var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
772227786281: Pull complete 
96fd13befc87: Pull complete 
8b2d9b141e4d: Pull complete 
Digest: sha256:25415d1143949e5dc0b03585365dc8bbe84f443ef116dc27719dc69f23ead35e
Status: Downloaded newer image for portainer/portainer:latest
d37c048559ca01632905df1ac7274e797df7ba551753d0e6d366168e4389366c

外部访问 ip:8088,设置密码后选中连接本地:

7、Docker镜像原理

7.1、镜像是什么?

镜像是一种轻量级的,可独立运行的软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含运行某个软件所需的所有内容,
包括代码,运行时库,环境变量和配置文件。

所有的应用,直接打包镜像,就可以直接跑起来!

如何得到镜像:

  • 从仓库下载
  • 自己打包
  • 从它处拷贝

7.2、Docker镜像加载原理

UnionFS(联合文件系统)

在Docker pull容器中就已经看到镜像安装的时候是分层安装的,这其实就是来自于UnionFS系统

它是一种分层,轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层叠加,同时可以将不同目录挂载到同一个虚拟文件系统下面。

Union文件系统是docker镜像的基础,镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

特性:一次同时记载多个文件系统,但从外表看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录。

Docker镜像加载原理

docker的镜像实际就是一层层的文件系统(UnionFS)组成

7.3、分层理解

例如安装redis的时候,观察pull日志会发现

[root@localhost ~]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
42c077c10790: Already exists //已经存在,其实这步就是部署centos,因为之前已经部署过了,存在文件级的记录
a300d83d65f9: Pull complete 
ebdc3afaab5c: Pull complete 
31eec7f8651c: Pull complete 
9c6a6b89d274: Pull complete 
5c8099a4b45c: Pull complete 
Digest: sha256:1b90dbfe6943c72a7469c134cad3f02eb810f016049a0e19ad78be07040cdb0c
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest

#docker inspect redis信息里面有分层的信息
"Layers": [
                "sha256:ad6562704f3759fb50f0d3de5f80a38f65a85e709b77fd24491253990f30b6be",
                "sha256:49cba0f0997b2bb3a24bcfe71c7cbd6e9f6968ef7934e3ad56b0f1f9361b6b91",
                "sha256:309498e524b3e2da1f036d00cd5155e0b74cf9e1d964a3636c8ed63ca4a00d43",
                "sha256:f7c9b429437f7ada2d3d455ac4ea90ff38e0cb7ef2551b08d152264b74116309",
                "sha256:4dabdd56bbf16307e2328cb6ed1d42b0bb9b8f40551421271c0b38dc9a685dcc",
                "sha256:ea450ad6ef893e998f88a35dc9cc22f952c62b88d58f948344cf4eda1a6264fc"
            ]

所有的Docker镜像都起始于一个基础镜像层,当进行修改或增加新的内容时,就会在当前镜像层之上,创建新的容器层。
容器在启动时会在镜像最外层上建立一层可读写的容器层(R/W),而镜像层是只读的(R/O)。

7.4、commit镜像

如果你想要保持当前容器的状态,就可以通过commit来保存一个镜像

docker commit: 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:TAG
posted @ 2022-05-25 15:29  吴承勇  阅读(714)  评论(0编辑  收藏  举报