恭喜您发现了“新大陆”,希望对您有帮助!

Docker容器技术之基础用法(2)

1. docker架构


在这里插入图片描述

概念 说明
Docker 镜像(Images) Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。
Docker 容器(Container) 容器是独立运行的一个或一组应用,是镜像运行时的实体。
Docker 客户端(Client) Docker 客户端通过命令行或者其他工具使用 Docker SDK(https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信。
Docker 主机(Host) 一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker Registry Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。
Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。
一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。
通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。
Docker Machine Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。

2. docker镜像与镜像仓库


  • Docker镜像是由文件系统叠加而成。最低端是一个引导文件系统,即bootfs。当一个容器启动后,它将会被移到内存中,而引导文件系统则会被卸载,以留出更多的内存供initrd磁盘镜像使用。
  • Docker镜像的第二层是root文件系统rootfs,它位于引导文件系统之上。rootfs可以是一种或多种操作系统。
  • 在Docker里,root文件系统永远只能是只读状态,并且Docker利用联合加载技术又会在root文件系统层上加载更多的只读文件系统。联合加载指的是一次同时加载多个文件系统,但是在外面看起来只能看到一个文件系统。联合加载会将各层文件系统叠加到一起,这样最终的文件系统会包含所有底层的文件和目录。
  • 当从一个镜像启动容器时,Docker会在该镜像的最顶层加载一个读写文件系统。
  • 当Docker第一次启动一个容器时,初始的读写层是空的。当文件系统发生变化时,这些变化都会应用到这一层上。如果修改一个文件,这个文件首先会从该读写层下面的只读层复制到该读写层。该文件的只读版本依然存在,但是已经被读写层中的该文件副本所隐藏。
  • 通常这种机制被称为写时复制,这也是使Docker如此强大的技术之一。每个只读镜像层都是只读的,并且以后永远不会变化。当创建一个新容器时,Docker会构建出一个镜像栈,并在栈的最顶端添加一个读写层。这个读写层再加上其下面的镜像层以及一些配置数据,就构成了一个容器。

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

3. 安装及使用docker


[root@node02 ~]# cd /etc/yum.repos.d/
[root@node02 ~]#  curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@node02 ~]#  sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo

[root@node02 ~]#  yum -y install docker-ce
..........
[root@node02 ~]# systemctl start docker
[root@node02 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2020-08-25 21:10:21 CST; 1 day 14h ago
     Docs: https://docs.docker.com
 Main PID: 13346 (dockerd)
    Tasks: 10
   Memory: 160.6M
   CGroup: /system.slice/docker.service
           └─13346 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock...

8月 25 21:10:21 node02.localdomain dockerd[13346]: time="2020-08-25T21:10:21.084087316+..."
8月 25 21:10:21 node02.localdomain dockerd[13346]: time="2020-08-25T21:10:21.120471911+...2
8月 25 21:10:21 node02.localdomain dockerd[13346]: time="2020-08-25T21:10:21.120568243+..."
8月 25 21:10:21 node02.localdomain dockerd[13346]: time="2020-08-25T21:10:21.145448828+..."
8月 25 21:10:21 node02.localdomain systemd[1]: Started Docker Application Container Engine.
8月 26 14:35:11 node02.localdomain dockerd[13346]: time="2020-08-26T14:35:11.689001390+..."
8月 26 14:35:11 node02.localdomain dockerd[13346]: time="2020-08-26T14:35:11.689040187+..."
8月 26 14:35:11 node02.localdomain dockerd[13346]: time="2020-08-26T14:35:11.689113428+..."
8月 26 14:35:38 node02.localdomain dockerd[13346]: time="2020-08-26T14:35:38.325746170+..."
8月 26 14:37:22 node02.localdomain dockerd[13346]: time="2020-08-26T14:37:22.170782380+..."
Hint: Some lines were ellipsized, use -l to show in full.

3.1 docker加速

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

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@node02 ~]# cat > /etc/docker/daemon.json <<EOF
{
    "registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
[root@node02 ~]# systemctl restart docker
[root@node02 ~]# docker version
..........
[root@node02 ~]# docker info
..........

配置阿里云加速器

[root@node02 ~]# vim /etc/docker/daemon.json
[root@node02 ~]# cat /etc/docker/daemon.json
{
    "registry-mirrors": ["https://v3ose6n1.mirror.aliyuncs.com"]
}

[root@node02 ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

[root@node02 ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 2
  Running: 2
  Paused: 0
  Stopped: 0
 Images: 3
 Server Version: 19.03.12
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 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: runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
 init version: fec3683
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-1062.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 1
 Total Memory: 972.4MiB
 Name: node02.localdomain
 ID: U62S:SR3R:CBJP:25BX:XBJF:7YU5:F5RQ:A3RP:556R:DU54:DLTL:EANF
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://v3ose6n1.mirror.aliyuncs.com/
 Live Restore Enabled: false

WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

4. 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对象的低级信息

4.1 进程相关命令

4.1.1 启动docker服务

[root@node02 ~]# systemctl start docker

4.1.2 停止docker服务

[root@node02 ~]# systemctl stop docker

4.1.3 重启docker服务

[root@node02 ~]# systemctl restart docker

4.1.4 查看docker服务状态

[root@node02 ~]# systemctl status docker

4.1.5 设置开机启动docker服务

[root@node02 ~]# systemctl enable --now docker

4.2 镜像相关命令

4.2.1 查看镜像

[root@node02 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
dragonyear22/b1     v0.1                65d1b2004e78        19 hours ago        1.22MB
nginx               latest              4bb46517cac3        13 days ago         133MB
busybox             latest              018c9d7b792b        4 weeks ago         1.22MB

4.2.2 搜索镜像

[root@node02 ~]# docker search httpd
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
httpd                                   The Apache HTTP Server Project                  3155                [OK]                
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   36                                      
centos/httpd                                                                            30                                      [OK]
arm32v7/httpd                           The Apache HTTP Server Project                  9                                       
polinux/httpd-php                       Apache with PHP in Docker (Supervisor, CentO…   4        
..........                     

4.2.3 拉取镜像

//想下载什么镜像就换成什么镜像
[root@node02 ~]# docker pull httpd      

4.2.4 删除镜像


4.3 容器相关命令

4.3.1 查看容器

[root@node02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
08e6ab7d2085        busybox             "sh"                     19 hours ago        Up 19 hours                             b1
09c991e385d4        nginx               "/docker-entrypoint.…"   21 hours ago        Up 2 seconds        80/tcp              quizzical_rosalind
^[[A[root@node02 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
08e6ab7d2085        busybox             "sh"                     19 hours ago        Up 19 hours                             b1
09c991e385d4        nginx               "/docker-entrypoint.…"   21 hours ago        Up 3 seconds        80/tcp              quizzical_rosalind

4.3.2 创建容器

//httpd可以改成任意镜像名
[root@node02 ~]# docker create httpd

4.3.3 创建并启动容器

[root@node02 ~]# docker run -d httpd

参数:
  -i  保持容器运行。通常与 -t 同时使用。加入it这两个参数后,容器创建后自动进入容器中,退出容器后,容器自动关闭。
  -t  为容器重新分配一个伪输入终端,通常与 -i 同时使用。
  -d  以守护(后台)模式运行容器。创建一个容器在后台运行,需要使用docker exec 进入容器。退出后,容器不会关闭。
  -it 创建的容器一般称为交互式容器,-id 创建的容器一般称为守护式容器
  --name 为创建的容器命名。

4.3.4 进入容器

[root@node02 ~]# docker exec -it 735775363df1 /bin/bash
root@735775363df1:/usr/local/apache2# 

//退出容器,容器不会关闭

4.3.5 停止容器

[root@node02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS               NAMES
735775363df1        httpd               "httpd-foreground"       About a minute ago   Up About a minute   80/tcp              zen_northcutt
08e6ab7d2085        busybox             "sh"                     20 hours ago         Up 20 hours                             b1
09c991e385d4        nginx               "/docker-entrypoint.…"   21 hours ago         Up 6 minutes        80/tcp              quizzical_rosalind
[root@node02 ~]# docker stop 735775363df1
735775363df1

4.3.6 启动容器

[root@node02 ~]# docker start 735775363df1
735775363df1

4.3.7 杀死正在运行的容器(可杀死多个)

[root@node02 ~]# docker kill 735775363df1
735775363df1

4.3.8 删除容器

[root@node02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
735775363df1        httpd               "httpd-foreground"       4 minutes ago       Up 2 seconds        80/tcp              zen_northcutt
08e6ab7d2085        busybox             "sh"                     20 hours ago        Up 20 hours                             b1
09c991e385d4        nginx               "/docker-entrypoint.…"   21 hours ago        Up 9 minutes        80/tcp              quizzical_rosalind
[root@node02 ~]# docker rm -f 735775363df1
735775363df1
[root@node02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
08e6ab7d2085        busybox             "sh"                     20 hours ago        Up 20 hours                             b1
09c991e385d4        nginx               "/docker-entrypoint.…"   21 hours ago        Up 9 minutes        80/tcp              quizzical_rosalind

4.3.9 获取容器日志

[root@node02 ~]# docker logs 09c991e385d4
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
..........

4.3.10 查看容器信息

[root@node02 ~]# docker inspect 09c991e385d4
[
    {
        "Id": "09c991e385d4bfe2bb0d283abc2c47a477acf084822d20833f4fe85d95bab64c",
        "Created": "2020-08-26T06:35:02.155714914Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 16682,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-08-27T03:46:26.241450795Z",
            "FinishedAt": "2020-08-27T03:45:07.484724366Z"
        },
..........
posted @ 2020-08-27 12:00  酷小年  阅读(213)  评论(0编辑  收藏  举报