码迷,mamicode.com
首页 > 其他好文 > 详细

docker入门实践教程 -date: 20191108

时间:2019-11-08 23:51:56      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:image   镜像   update   nal   follow   port   ash   download   动态   

docker入门实践教程 -date: 2019108

1、docker架构

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

Docker 从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版)

容器非常适合持续集成和持续交付(CI / CD)工作流程。
Docker 的可移植性和轻量级的特性,还可以使您轻松地完成动态管理的工作负担,并根据业务需求指示,实时扩展或拆除应用程序和服务。
Docker 轻巧快速。它为基于虚拟机管理程序的虚拟机提供了可行、经济、高效的替代方案,因此您可以利用更多的计算能力来实现业务目标。Docker 非常适合于高密度环境以及中小型部署,

Docker 包括三个基本概念:

镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像。
Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。

Docker 容器通过 Docker 镜像来创建。

2.docker 安装
ubuntu安装方法:
卸载旧版本
Docker 的旧版本被称为 docker,docker.io 或 docker-engine 。如果已安装,请卸载它们:

#apt-get remove docker docker-engine docker.io containerd runc

使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker 。

设置仓库
更新 apt 包索引。

#apt-get update

安装 apt 依赖包,用于通过HTTPS来获取仓库:

#apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common

添加 Docker 的官方 GPG 密钥:

#curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 通过搜索指纹的后8个字符,验证您现在是否拥有带有指纹的密钥。

#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]

使用以下指令设置稳定版仓库

#add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

安装 Docker Engine-Community

更新 apt 包索引。

#apt-get update

安装最新版本的 Docker Engine-Community 和 containerd ,或者转到下一步安装特定版本:

#apt-get install docker-ce docker-ce-cli containerd.io

要安装特定版本的 Docker Engine-Community,请在仓库中列出可用版本,然后选择一种安装。列出您的仓库中可用的版本:

#apt-cache madison docker-ce

docker-ce | 5:18.09.1~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
docker-ce | 5:18.09.0~3-0~ubuntu-xenial | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
docker-ce | 18.06.1~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
docker-ce | 18.06.0~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
...

使用第二列中的版本字符串安装特定版本,例如 5:18.09.1~3-0~ubuntu-xenial。

#apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

测试 Docker 是否安装成功,输入以下指令,打印出以下信息则安装成功:

#docker run hello-world

Unable to find image ‘hello-world:latest‘ locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

  1. 使用 Shell 脚本进行安装
    Docker 在 get.docker.com 和 test.docker.com 上提供了方便脚本,用于将快速安装 Docker Engine-Community 的边缘版本和测试版本。脚本的源代码在 docker-install 仓库中。 不建议在生产环境中使用这些脚本,在使用它们之前,您应该了解潜在的风险:

脚本需要运行 root 或具有 sudo 特权。因此,在运行脚本之前,应仔细检查和审核脚本。

这些脚本尝试检测 Linux 发行版和版本,并为您配置软件包管理系统。此外,脚本不允许您自定义任何安装参数。从 Docker 的角度或您自己组织的准则和标准的角度来看,这可能导致不支持的配置。

这些脚本将安装软件包管理器的所有依赖项和建议,而无需进行确认。这可能会安装大量软件包,具体取决于主机的当前配置。

该脚本未提供用于指定要安装哪个版本的 Docker 的选项,而是安装了在 edge 通道中发布的最新版本。

如果已使用其他机制将 Docker 安装在主机上,请不要使用便捷脚本。

本示例使用 get.docker.com 上的脚本在 Linux 上安装最新版本的Docker Engine-Community。要安装最新的测试版本,请改用 test.docker.com。在下面的每个命令,取代每次出现 get 用 test。

#curl -fsSL https://get.docker.com -o get-docker.sh
#sh get-docker.sh
如果要使用 Docker 作为非 root 用户,则应考虑使用类似以下方式将用户添加到 docker 组:

#usermod -aG docker your-user

Debian Docker 安装

卸载旧版
#apt-get remove docker docker-engine docker.io containerd runc

#apt-get update

安装 apt 依赖包,用于通过 HTTPS 来获取仓库。

#apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg2 \
software-properties-common

添加 Docker 的官方 GPG 密钥:

#curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -

9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 通过搜索指纹的后8个字符,验证您现在是否拥有带有指纹的密钥。

#apt-key fingerprint 0EBFCD88

pub 4096R/0EBFCD88 2017-02-22
Key fingerprint = 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid Docker Release (CE deb) <docker@docker.com>
sub 4096R/F273FCD8 2017-02-22
使用以下指令设置稳定版仓库
#add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) \
stable"

安装 Docker Engine-Community
#apt-get update

安装最新版本的 Docker Engine-Community 和 containerd ,或者转到下一步安装特定版本:

#apt-get install docker-ce docker-ce-cli containerd.io
#apt-cache madison docker-ce

docker-ce | 5:18.09.1~3-0~debian-stretch | https://download.docker.com/linux/debian stretch/stable amd64 Packages
docker-ce | 5:18.09.0~3-0~debian-stretch | https://download.docker.com/linux/debian stretch/stable amd64 Packages
docker-ce | 18.06.1~ce~3-0~debian | https://download.docker.com/linux/debian stretch/stable amd64 Packages
docker-ce | 18.06.0~ce~3-0~debian | https://download.docker.com/linux/debian stretch/stable amd64 Packages
.......

使用第二列中的版本字符串安装特定版本,例如 5:18.09.1~3-0~debian-stretch 。

#sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
测试 Docker 是否安装成功,输入以下指令,打印出以下信息则安装成功:

#docker run hello-world

Unable to find image ‘hello-world:latest‘ locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
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/

  1. CentOS Docker 安装

Docker 支持以下的 64 位 CentOS 版本:

CentOS 7
CentOS 8
更高版本...
该 centos-extras 库必须启用。默认情况下,此仓库是启用的,但是如果已禁用它,则需要重新启用它。

建议使用 overlay2 存储驱动程序。

较旧的 Docker 版本称为 docker 或 docker-engine 。如果已安装这些程序,请卸载它们以及相关的依赖项。

#yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

安装 Docker Engine-Community
使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库。之后,您可以从仓库安装和更新 Docker。

设置仓库

安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

#yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
使用以下命令来设置稳定的仓库。

#yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker Engine-Community
安装最新版本的 Docker Engine-Community 和 containerd,或者转到下一步安装特定版本:

#yum install docker-ce docker-ce-cli containerd.io
如果提示您接受 GPG 密钥,请选是。

有多个 Docker 仓库吗?

如果启用了多个 Docker 仓库,则在未在 yum install 或 yum update 命令中指定版本的情况下,进行的安装或更新将始终安装最高版本,这可能不适合您的稳定性需求。

Docker 安装完默认未启动。并且已经创建好 docker 用户组,但该用户组下没有用户。

要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:

1、列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。

#yum list docker-ce --showduplicates | sort -r

docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable
docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable
docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable

2、通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce-18.09.1。

#yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

启动 Docker。

#systemctl start docker
通过运行 hello-world 映像来验证是否正确安装了 Docker Engine-Community 。

#docker run hello-world

Docke 镜像加速

国内从 DockerHub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。Docker 官方和国内很多云服务商都提供了国内加速器服务,例如:

Docker官方提供的中国镜像库:https://registry.docker-cn.com
七牛云加速器:https://reg-mirror.qiniu.com
当配置某一个加速器地址之后,若发现拉取不到镜像,请切换到另一个加速器地址。国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务。

我们以 Docker 官方加速器 https://registry.docker-cn.com 为例进行介绍。

Ubuntu14.04、Debian7Wheezy
对于使用 upstart 的系统而言,编辑 /etc/default/docker 文件,在其中的 DOCKER_OPTS 中配置加速器地址:

DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com"
重新启动服务:

$ sudo service docker restart
Ubuntu16.04+、Debian8+、CentOS7
对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):

{"registry-mirrors":["https://registry.docker-cn.com"]}
之后重新启动服务:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
检查加速器是否生效
检查加速器是否生效配置加速器之后,如果拉取镜像仍然十分缓慢,请手动检查加速器配置是否生效,在命令行执行 docker info,如果从结果中看到了如下内容,说明配置成功
$ docker info
Registry Mirrors:
https://registry.docker-cn.com/

docker入门实践教程 -date: 20191108

标签:image   镜像   update   nal   follow   port   ash   download   动态   

原文地址:https://blog.51cto.com/guanhaizhan/2448802

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!