Docker安装

1. Docker中的容器

lxc --> libcontainer --> runC
在这里插入图片描述

2. OCI和OCF

OCI(Open Container-initiative):

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • 包含两个规范
    运行时的规范(runtime-spec)
    镜像规范(image-spec)

OCF(Open Container Format):
runC 是一个 CLI (command-line interface)工具,用于根据 OCI 规范生成和运行容器

  • 容器作为 runC 的子进程启动,可以嵌入到各种其他系统中,而无需运行守护进程
  • runC 建立在 libcontainer 之上,同样的容器技术为数百万个 Docker Engine 安装提供支持
  • runC直接与容器所依赖的cgroup/linux kernel等进行交互,负责为容器配置cgroup/namespace等启动容器所需的环境,创建启动容器的相关进程。

3. Docker原理图

在这里插入图片描述
Docker pull向Docker daemon发送指令,告诉Docker daemon要拉取某一个镜像,Docker daemon首先会检查本机镜像是否存在,如果存在且版本就是我们想要拉取的版本,它就不会做任何的操作。如果不存在下一步它会到Docker的仓库中找我们要拉取的镜像,如果找到了就会有Docker仓库传送到本机。

Docker run:首先把命令发送到Docker daemon,Docker daemon会先检查镜像在本机是否存在,如果不存在相当于执行了一个Docker pull的过程,下载回来之后会以一定方式把镜像运行起来变成Docker容器。

4.Docker镜像与镜像仓库的关系

我们可以发现Docker镜像仓库的名字是Registry而不是repository?因为在Docker中仓库的名字是以应用的名称命名的。

在这里插入图片描述
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也就是进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

5. Docker对象

当你使用 Docker 时,您正在创建和使用图像、容器、网络、卷、插件和其他对象。

  • 镜像
    镜像是一个只读模板,其中包含创建 Docker 容器的说明
    通常,一个镜像基于另一个镜像,并带有一些额外的自定义
    你可以创建自己的镜像,也可以仅使用其他人创建并在仓库中发布的镜像
  • 容器
    容器是镜像的可运行实例。
    你可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
    你可以将容器连接到一个或多个网络,为其附加存储,甚至可以根据其当前状态创建新镜像。

6. 安装及使用Docker

6.1 Docker安装

//配置yum仓库,我们配置阿里云的源
[root@localhost ~]# vim /etc/yum.repos.d/docker-ce.repo 
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
enabled=1
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
[root@localhost ~]# dnf -y install docker-ce
安装过程略...

6.2 Docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)

我们使用阿里云加速器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//启动docker就能生成/etc/docker目录
[root@localhost ~]# systemctl start docker
[root@localhost ~]# ls /etc/docker/
key.json

[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
> {
>   "registry-mirrors": ["https://1gnt5vnf.mirror.aliyuncs.com"]
> }
> EOF

//重启docker
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker

6.3 Docker常用的操作

命令功能
docker search在Docker Hub中搜索镜像
docker pull从仓库中提取镜像或存储库
docker images列出镜像
docker create创建一个新的容器
docker start启动一个或多个已停止的容器
docker run在新容器中运行命令
docker attach附加到运行容器
docker ps列出容器
docker logs提取容器的日志
docker restart重新启动容器
docker stop停止一个或多个运行中的容器
docker kill杀死一个或多个正在运行的容器
docker rm删除一个或更多的容器
docker exec在正在运行的容器中运行命令
docker info显示系统范围的信息
docker inspect返回有关Docker对象的低级信息
[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.7
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc version: v1.0.0-0-g84113ee
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-257.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 780.3MiB
 Name: minion2
 ID: KILY:JJ2B:TIKH:ZWX5:SLZI:2Y62:PK4O:4TDG:U3ET:JD7Q:6DNH:IPSD
 Docker Root Dir: /var/lib/docker    #docker的家目录
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://1gnt5vnf.mirror.aliyuncs.com/    #这里就是我们刚刚加的加速器
 Live Restore Enabled: false

//查看版本信息
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.7     #Docker版本
 API version:       1.41        #API版本
 Go version:        go1.13.15   #Go语言版本 
 Git commit:        f0df350     #Git提交
 Built:             Wed Jun  2 11:56:24 2021   #什么时候做的这个包
 OS/Arch:           linux/amd64   #平台  
 Context:           default      #安全上下文
 Experimental:      true        #实验版本

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:48 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.8
  GitCommit:        7eba5930496d9bbe375fdf71603e610ad737d2b2
 runc:
  Version:          1.0.0
  GitCommit:        v1.0.0-0-g84113ee
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker hub地址
https://hub.docker.com/

示例:

//以拉tomcat为例
[root@localhost ~]# docker pull tomcat   #直接这样拉默认都是最新的
Using default tag: latest
latest: Pulling from library/tomcat
627b765e08d1: Pull complete 
c040670e5e55: Pull complete 
073a180f4992: Pull complete 
bf76209566d0: Pull complete 
f10db7ba7580: Pull complete 
5b2f970878fa: Pull complete 
ed434bfebf18: Pull complete 
f6c437110aa9: Pull complete 
a772951f83db: Pull complete 
752225c3768e: Pull complete 
Digest: sha256:6e40250d8fac4eca05c2067cb81f79427e4ddbaf4e78d5ecd21c35e8c5f2bfcf
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest

//查看镜像是否拉取成功
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED      SIZE
tomcat       latest    46cfbf1293b1   4 days ago   668MB

//创建一个容器
[root@localhost ~]# docker create tomcat
b3b0780aa2f83ea105dad83848005b8f2018346155511167324cc07c2b136513

//查看容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
//这样是看不到的,因为容器没有启动

//我们可以加上-a查看
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS    PORTS     NAMES
b3b0780aa2f8   tomcat    "catalina.sh run"   51 seconds ago   Created             beautiful_khayyam

//启动这个docker,通过这个ID来启动
[root@localhost ~]# docker start b3b0780aa2f8
b3b0780aa2f8

//然后再查看
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS      NAMES
b3b0780aa2f8   tomcat    "catalina.sh run"   2 minutes ago   Up 6 seconds   8080/tcp   beautiful_khayyam
//查看一些低级信息
[root@localhost ~]# docker inspect b3b0780aa2f8
[
    {
        "Id": "b3b0780aa2f83ea105dad83848005b8f2018346155511167324cc07c2b136513",
        "Created": "2021-07-27T15:19:50.39117496Z",
        "Path": "catalina.sh",
        "Args": [
            "run"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 5004,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-07-27T15:21:58.39777026Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
此处省略N行...
"IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "ccd198f93c2c43c7c3530d12cf2c38ebb955caf37530b2205f01217cb8d069b7",
                    "EndpointID": "2bd299dcafaa115c32490ee97fc3db74e22c32c04e143aca305352c8c09b256a",
                    "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
                }
            }
        }
    }
]

//通过上面的ip是可以访问的
[root@localhost ~]# curl 172.17.0.2:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="lin
//在正在运行的容器中运行命令
[root@localhost ~]# docker exec b3b0780aa2f8 pwd
/usr/local/tomcat

//直接通过it选项进入交互模式
[root@localhost ~]# docker exec -it f723b18dc53c /bin/bash
root@f723b18dc53c:/usr/local/tomcat# pwd
/usr/local/tomcat
//通过-d选项来运行
[root@localhost ~]# docker run -d tomcat
f723b18dc53c610ddff2513988f2643933212c5d5d95af64c45cbe5ee7461aec
//查看日志
[root@localhost ~]# docker logs b3b0780aa2f8

7. docker event state

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值