Docker基础

1. Docker简介

Docker 是一个开源的应用容器引擎,基于Go 语言 并遵从 Apache2.0 协议开源。

Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。

容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。

1.1 Docker发展历史

Docker最早是在Ubuntu 12.04上开发实现的;

Red Hat则从RHEL6.5开始对Docker进行支持。

而后Windows和Mac上也相应有了Docker版本支持。

在Docker容器技术出现之前,Linux上是已经有一个docker的工具的,但此docker非彼Docker。

这个docker是一个窗口停靠栏程序,就像苹果的Mac系统中的dock那个程序一样的一个工具。
为了区分开来,我们以Docker和docker来进行区分。
Docker:指容器技术。
docker:指窗口停靠栏程序。

Docker技术出来后,因为Linux系统上已经有了docker这个工具,所以Docker软件名也不能跟人家重名啊,要不然没办法安装。

由于那个时候Docker的官网是docker.io,所以就在软件名称上加了io的后缀,在Ubuntu中就是docker.io,在CentOS中就是docker-io。

但是虽然软件名跟docker程序不一样了,但软件安装后的操作命令还是一样的,都是docker的这个命令,所以要安装Docker软件,要先看看有没有安装了那个停靠栏程序docker,有的话要先卸载才行,要不然执行的命令是不对的。

这个时期要安装Docker,就要用docker加io后缀的方式来安装。

Docker容器使用docker.io和docker-io为软件名,主要是前期的一段时间。

后来随着Docker的发展,软件包名改成了docker-engine,不同系统中名称达到了统一。

再后来,随着Docker技术的火爆,在征得docker停靠栏程序作者同意下,原先的停靠栏程序docker名称改掉了,改成了wmdocker,Docker容器技术的软件包名才正式成了docker这个名称,Docker软件包的名称又得到了一次完全的统一。

到Docker1.13.1版本之前,Docker软件包的名称有两次变化,从docker-io(docker.io)到docker-engine,再到docker。

Docker发展到1.13.1版本号后,Docker公司把Docker分成了社区版(免费)Docker CE和商业版(付费)Docker EE两种形式,并且版本号命名方式也改了,以前是那种常用的版本号命令方式,比如0.1、0.2、1.0之类的,现在分社区和商业版后,版本号是“年.月”的形式命名的,比如2019年10月发布的,版本号就是19.10。

所以在Docker1.13.1之后,直接是Docker-ce 17.03.0版本了,也就是2017年03月发布的。

现在要安装最新版的Docker软件包,就是使用docker-ce这个名称了,如果是商业版的就是docker-ee了。

1.2 docker环境安装

# 安装依赖包
root@ubuntu-template:~# apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
...
Setting up gpg-wks-server (2.2.4-1ubuntu1.4) ...
Setting up gpg-wks-client (2.2.4-1ubuntu1.4) ...
Setting up gnupg (2.2.4-1ubuntu1.4) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

# 加 Docker 的官方 GPG 密钥
root@ubuntu-template:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK

# 验证您现在是否拥有带有指纹的密钥
root@ubuntu-template:~# apt-key fingerprint 0EBFCD88
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]

# 使用以下指令设置稳定版仓库
root@ubuntu-template:~# add-apt-repository \
>    "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \
>   $(lsb_release -cs) \
>   stable"
Get:1 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic InRelease [64.4 kB]
Hit:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic InRelease                             
Get:3 https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages [19.8 kB]
Get:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates InRelease [88.7 kB]
Get:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports InRelease [74.6 kB]
Get:6 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security InRelease [88.7 kB]
Get:7 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates/main amd64 Packages [2,192 kB]
Get:8 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates/main Translation-en [430 kB]
Get:9 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates/universe amd64 Packages [1,747 kB]
Get:10 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates/universe Translation-en [375 kB]
Get:11 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security/main amd64 Packages [1,846 kB]
Get:12 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security/main Translation-en [338 kB]
Get:13 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security/universe amd64 Packages [1,137 kB]
Get:14 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security/universe Translation-en [259 kB]
Fetched 8,660 kB in 2s (3,732 kB/s)                         
Reading package lists... Done

1.3 安装 Docker Engine-Community

# 安装最新版本(稳定)的 Docker Engine-Community 和 containerd 
root@ubuntu-template:~# apt-get install docker-ce docker-ce-cli containerd.io

# 要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。列出您的仓库中可用的版本
root@ubuntu-template:~# apt-cache madison docker-ce
 docker-ce | 5:20.10.8~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.7~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.6~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.5~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.4~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.3~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.2~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.1~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:20.10.0~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.15~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.14~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.13~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.12~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.11~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
 docker-ce | 5:19.03.10~3-0~ubuntu-bionic | https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu bionic/stable amd64 Packages
# 使用第二列中的版本字符串安装特定版本,例如 5:20.10.8~3-0~ubuntu-bionic。
root@ubuntu-template:~# apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

1.4 验证

root@ubuntu-template:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
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/

2. 存储引擎

目前docker的默认存储引擎为overlay2,不同的存储引擎需要相应的文件系统支持,如需要磁盘分区的时候传递d-type稳健分层功能,即需要传递内核参数并开启格式化磁盘的时候指定的功能。
存储引擎的选择文档

2.1 存储驱动类型

AUFS

AUFSAnotherUnionFileSystem是一种UnionFS。V2版本后更名为 advanced multi‐layered unification fileystem,即高级多层统一文件系统。所谓UnionFS就是把不同物理位置的目录合并mount到同一个目录中。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种系统可以一层一层的叠加修改文件。无论底下有多少层都是只读,只有最上层的文件系统是可读写。当需要修改一个文件时,AUFS创建该文件的一个副本。使用CoWCopy-on-Write将文件从只读层复制到可写层进行修改,结果也保留在可写层、在Docker中。底下的制度层就是image,可写层就是Container。

AUFS是Docker18.06及更早版本的首选存储驱动程序,在内核3.13上运行Unbantu14.04时不支持overlay2

Overlay

一种Union FS文件系统,Linux内核3.18后支持

Overlay2

overlay的升级版,到目前为止,所有Linux发行版推荐使用的存储类型

devicemapper

是CentOS和RHEL的推荐存储驱动程序,但是依赖于direct-lvm,存在空间受限的问题,虽然可以通过后期配置解决;因为之前的内核版本不支持overlay2(集中在Centos/RHEL7.2之前版本);但当前较新版本Centos和RHEL现已经支持overlay2。

https://www.cnblogs.com/youruncloud/p/5736718.html

zfs/btrfs(Oracle-2007)

目前没有广泛应用;这些文件系统允许使用高级选项,例如创建“快照”,但需要更多的维护和设置。并且每一个都依赖于正确配置的后备文件系统。

vfs

用于测试环境,适用于无法适用Cow文件系统的情况。此存储驱动程序的性能很差,通常不建议在生产中使用。


image.png

1)overlay存储驱动程序已在Docker Engine-Enterprise 18.09中弃用,并将在以后的版本中删除。建议将overlay存储驱动程序的用户迁移到overlay2。

2)devicemapper存储驱动程序已在Docker Engine 18.09中弃用,并将在以后的版本中删除。建议将devicemapper存储驱动程序的用户迁移到overlay2。

建议使用overlay2存储驱动程序。首次安装Docker时,默认情况下使用overlay2。早期版本,默认情况下会使用aufs。如果要在新版本中使用aufs,则需要对其配置,并且可能需要安装其他软件包,例如linux-image-extra。

2.2 支持的文件系统

对于Docker,支持文件系统是所在的文件系统 /var/lib/docker/。一些存储驱动程序仅适用于特定的后备文件系统。


image.png
  • overlay2,,aufs和overlay都在文件级别而不是块级别运行。这样可以更有效地使用内存,但是在写繁重的- 工作负载中,容器的可写层可能会变得非常大。
  • 块级存储驱动器,例如devicemapper,btrfs和zfs更好地为写繁重的工作负担(虽然不如Docker volumes)执行。
  • 对于许多小型写入或具有多层或深文件系统的容器,其性能 overlay可能比更好overlay2,但会消耗更多的inode,这可能导致inode耗尽。
  • btrfs并zfs需要大量内存。
  • zfs 对于高密度工作负载(例如PaaS)是一个不错的选择。

2.3 修改更换存储引擎

配置 Docker 存储驱动非常简单,只需要修改配置文件即可。

注意:如果你原先有不同存储驱动的层数据,更换存储驱动后将不可用,建议备份镜像并清除 /var/lib/docker 下所有数据。
备份镜像可以用 docker save 导出镜像,之后用 docker load 导入镜像。

root@ubuntu-template:~# vim /etc/docker/daemon.json
{
  ...
  "storage-driver": "overlay2"
  ...
}
root@ubuntu-template:~# systemctl restart docker
root@ubuntu-template:~# docker info
...
Server:
 Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
 Images: 1
 Server Version: 20.10.8
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 ...

3. docker常用命令

3.1 从远程仓库拉一个纯净的centos系统镜像

# 查看 centos相关的镜像
root@ubuntu-template:~# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15431     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2063                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   817                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   240                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   152                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   141                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        135                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   106                  [OK]
jasonrivers/nginx-rtmp            Docker images to host RTMP streams using NGI…   92                   [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   72                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   57                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   55                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  48                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   24                   [OK]
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   20                   
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   19                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   15                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11                   
flashspys/nginx-static            Super Lightweight Nginx Image                   10                   [OK]
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          2                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]


# 下载镜像到本地
root@ubuntu-template:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a330b6cecb98: Pull complete 
5ef80e6f29b5: Pull complete 
f699b0db74e3: Pull complete 
0f701a34c55e: Pull complete 
3229dce7b89c: Pull complete 
ddb78cb2d047: Pull complete 
Digest: sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c12867002e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

#查看本地镜像
root@ubuntu-template:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
nginx         latest    822b7ec2aaf2   4 days ago     133MB
hello-world   latest    d1165f221234   6 months ago   13.3kB

3.2 创建并进入容器

# 创建容器
root@ubuntu-template:~# docker run -dit --name=nginx-test 822b7ec2aaf2 /bin/bash
202f686cf02fa0371149bda2e1c2d966e52f52542cf2743c86640611bf308041


# 查看所有容器
root@ubuntu-template:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
202f686cf02f   822b7ec2aaf2   "/docker-entrypoint.…"   14 seconds ago   Up 14 seconds               80/tcp    nginx-test
e534bf8963bb   hello-world    "/hello"                 20 minutes ago   Exited (0) 20 minutes ago             agitated_brahmagupta

# 进入容器 使用容器名称或者CONTAINER ID都行
root@ubuntu-template:~# docker exec -it nginx-test /bin/bash
root@202f686cf02f:/# 

3.3 启动已经停止的容器

# 先查看容器CONTAINER ID
root@ubuntu-template:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                       PORTS     NAMES
202f686cf02f   822b7ec2aaf2   "/docker-entrypoint.…"   2 minutes ago    Exited (137) 2 seconds ago             nginx-test
e534bf8963bb   hello-world    "/hello"                 22 minutes ago   Exited (0) 22 minutes ago              agitated_brahmagupta

# 启动或停止
格式:docker start/stop CONTAINERID
root@ubuntu-template:~# docker start 202f686cf02f
202f686cf02f

# 验证
root@ubuntu-template:~# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS          PORTS     NAMES
202f686cf02f   822b7ec2aaf2   "/docker-entrypoint.…"   3 minutes ago   Up 11 seconds   80/tcp    nginx-test

3.4 修改容器名称

# 修改容器名称
格式:docker rename 老名称 新名称
root@ubuntu-template:~# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS          PORTS     NAMES
202f686cf02f   822b7ec2aaf2   "/docker-entrypoint.…"   3 minutes ago   Up 11 seconds   80/tcp    nginx-test
root@ubuntu-template:~# docker rename nginx-test nginx-newname
root@ubuntu-template:~# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS              PORTS     NAMES
202f686cf02f   822b7ec2aaf2   "/docker-entrypoint.…"   4 minutes ago   Up About a minute   80/tcp    nginx-newname

3.5 将容器制作成镜像

格式:docker commit -m '镜像描述' -a '署名' 容器名 镜像名

root@ubuntu-template:~# docker commit -m 'nginx test' -a 'pop' nginx-newname nginx-202109090130
sha256:1cec4595e1a105a431c5d33af3a45b0e0127400ba2cb70b596c90603dc283d7f
root@ubuntu-template:~# docker images
REPOSITORY           TAG       IMAGE ID       CREATED         SIZE
nginx-202109090130   latest    1cec4595e1a1   6 seconds ago   133MB
nginx                latest    822b7ec2aaf2   4 days ago      133MB
hello-world          latest    d1165f221234   6 months ago    13.3kB

3.6 将制作好的镜像打成 tar包

格式:docer save 镜像名 -o tar包路径
root@ubuntu-template:~# docker save -o /tmp/nginx.gz nginx-202109090130:latest
root@ubuntu-template:~# ll /tmp/nginx.gz 
-rw------- 1 root root 137448960 Sep  8 01:37 /tmp/nginx.gz

3.7 本地加载制作好的镜像

格式:docker load < tar包路径
# 先删除
root@ubuntu-template:~# docker rmi nginx-202109090130
Untagged: nginx-202109090130:latest
Deleted: sha256:1cec4595e1a105a431c5d33af3a45b0e0127400ba2cb70b596c90603dc283d7f
Deleted: sha256:38927a22f13725a964b84342f070aba1098328b1c7ceda23afa357c4debdad21

#导入
root@ubuntu-template:~# docker load < /tmp/nginx.gz
1853e87c604d: Loading layer [==================================================>]   2.56kB/2.56kB
Loaded image: nginx-202109090130:latest

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • docker最早是LXC(Linux Container)的二次封装发行,后来使用的是Libcontainer技术...
    OOM_Killer阅读 747评论 0赞 0
  • 一句话,docker 存储驱动用于管理docker 镜像和容器。 1. 镜像和层 在了解docker存储驱动之前,...
    lingguo610阅读 661评论 0赞 0
  • dcoker存储引擎介绍官方网址:https://docs.docker.com/storage/storaged...
    TimeRovers阅读 827评论 0赞 1
  • 本章内容 ◆ Docker简介◆ Docker 镜像与制作◆ Docker 数据管理◆ Docker 网络◆ Do...
    Liang_JC阅读 977评论 0赞 0
  • overlayFS是一个类似AUFS的联合挂载文件系统,与AUFS的实现类似但是比AUFS的存储速度更快。dock...
    Lis_阅读 868评论 0赞 0

友情链接更多精彩内容