docker入门

  • 云原生概念
  • Docker 容器介绍
  • Docker 容器入门
  • Docker 一些常用的命令

云原生概念

这一概念之所以前面没有提出来让大家了解是担心大家一下子不太容易理解。所以经过前面微服务的实验过后能够对相关概念有所理解后,再为大家介绍云原生的概念。

提及云原生,首先需要了解一下 CNCF,即云原生计算基金会,2015 年由谷歌牵头成立。基金会成员目前已有上百家企业与机构,包括亚马逊、微软、思科等巨头。目前 CNCF 所托管的应用一大数十个,知名的项目有 Kubernetes、Prometheus、Envoy 等。

CNCF 宪章中给出了云原生应用的三大特性,概括如下:

  • 容器化封装:以容器将为基础,提高整体开发水平,形成代码与组件的重用,简化云原生应用程序的维护。在容器中运行应用程序和进程,并作为应用程序部署的独立单元,实现高水平资源隔离。
  • 动态管理:通过集中式的编排调用系统来动态管理和调度。
  • 面向微服务:明确服务间的依赖,互相解耦。

依照云原生的理念来看,有如图四个部分组成:

图片描述

云原生包含了一组应用的模式,用于帮组企业快速、持续、可靠和规模化地交付业务软件。云原生有微服务架构、DevOps 和以容器为代表的敏捷基础架构组成。

前面介绍了微服务,这里着重介绍容器化。

 
 
Docker 容器介绍

Docker 是一个开源的容器引擎,可以轻松地为任何应用创建一个轻量级的、可移植的和自给自足的容器。它可以帮助我们更快地交付应用。Docker 可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理。使用 Docker,可更快地打包、测试以及部署应用程序,并可减少从编写到部署运行代码的周期。

我们先从官方提供的 Docker 架构图来讲起。

图片描述

  • Docker daemon(Docker 守护进程)

Docker daemon 是一个运行在宿主机(DOCKER_HOST)的后台进程。我们可通过 Docker 客户端与之通信。

  • Client(Docker 客户端)

Docker 客户端是 Docker 的用户界面,它可以接受用户命令和配置标识,并与 Docker daemon 通信。图中,docker build 等都是 Docker 的相关命令。

  • Images(Docker 镜像)

Docker 镜像是一个只读模板,它包含创建 Docker 容器的说明。它和系统安装光盘有点像——我们使用系统安装光盘安装系统,同理,我们使用 Docker 镜像运行 Docker 镜像中的程序。

  • Container(容器)

容器是镜像的可运行实例。镜像和容器的关系有点类似于面向对象中,类和对象的关系。我们可通过 Docker API 或者 CLI 命令来启停、移动和删除容器。

  • Registry

Docker Registry 是一个集中存储与分发镜像的服务。我们构建完 Docker 镜像后,就可在当前宿主机上运行。但如果想要在其他机器上运行这个镜像,我们就需要手动拷贝。此时,我们可借助 Docker Registry 来避免镜像的手动拷贝。

一个 Docker Registry 可包含多个 Docker 仓库;每个仓库可包含多个镜像标签;每个标签对应一个 Docker 镜像。这跟 Maven 的仓库有点类似,如果把 Docker Registry 比作 Maven 仓库的话,那么 Docker 仓库就可理解为某 jar 包的路径,而镜像标签则可理解为 jar 包的版本号。

Docker Registry 可分为公有 Docker Registry 和私有 Docker Registry。最常用的 Docker Registry 莫过于官方的 Docker Hub,这也是默认的 Docker Registry。Docker Hub 上存放着大量优秀的镜像,我们可使用 Docker 命令下载并使用。

Docker 安装

安装方式参考官方文档

  • 系统要求: Ubuntu 为例

  • 移除非官方软件包

sudo apt-get update && sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    gnupg-agent 
    software-properties-common

  

  • 添加官方 GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  

  • 安装 Docker
sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli

  

  • 在生产系统中,可能需要安装指定版本的 Docker,而不是最新的,先列出可用的 Docker 版本:
apt-cache madison docker-ce

  

  • 选择版本号,开始安装,比如安装 5:18.09.1~3-0~ubuntu-xenial 版本:
sudo apt-get install docker-ce=5:18.09.1~3-0~ubuntu-xenial docker-ce-cli=5:18.09.1~3-0~ubuntu-xenia

  

  • 测试是否安装成功,从镜像仓库拉取并运行一个容器:
sudo docker run hello-world

  

如果执行完该指令后,控制台打印出如下一些信息并推出,说明 Docker 已经安装成功。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
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/

  

 
  • 你也可以查看安装的版本号:
$ docker version
Client: Docker Engine - Community
 Version:           18.09.1
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        6247962
 Built:             Sun Feb 10 04:12:39 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.1
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       6247962
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     true
 
 
 
 

tom@ubuntu:~$ lsb_release -cs
xenial


# step 1: 安装必要的一些系统工具 sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common # step 2: 安装GPG证书 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # Step 3: 写入软件源信息 sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Step 4: 更新并安装Docker-CE sudo apt-get -y update sudo apt-get -y install docker-ce # 安装指定版本的Docker-CE: # Step 1: 查找Docker-CE的版本: # apt-cache madison docker-ce # docker-ce | 17.03.1~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages # docker-ce | 17.03.0~ce-0~ubuntu-xenial | https://mirrors.aliyun.com/docker-ce/linux/ubuntu xenial/stable amd64 Packages # Step 2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.1~ce-0~ubuntu-xenial) # sudo apt-get -y install docker-ce=[VERSION]


tom@ubuntu:~$ docker version
Client: Docker Engine - Community
Version: 20.10.3
API version: 1.41
Go version: go1.13.15
Git commit: 48d30b5
Built: Fri Jan 29 14:33:37 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version: dial unix /var/run/docker.sock: connect: permission denied

tom@ubuntu:~$ sudo docker version
Client: Docker Engine - Community
Version: 20.10.3
API version: 1.41
Go version: go1.13.15
Git commit: 48d30b5
Built: Fri Jan 29 14:33:37 2021
OS/Arch: linux/amd64
Context: default
Experimental: true

Server: Docker Engine - Community
Engine:
Version: 20.10.3
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 46229ca
Built: Fri Jan 29 14:31:47 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.3
GitCommit: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc:
Version: 1.0.0-rc92
GitCommit: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
docker-init:
Version: 0.19.0
GitCommit: de40ad0

  

CentOS 7(使用 yum 进行安装)

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

# 注意:
# 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
# vim /etc/yum.repos.d/docker-ce.repo
#   将[docker-ce-test]下方的enabled=0修改为enabled=1
#
# 安装指定版本的Docker-CE:
# Step 1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
#   Loading mirror speeds from cached hostfile
#   Loaded plugins: branch, fastestmirror, langpacks
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable
#   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable
#   Available Packages
# Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)
# sudo yum -y install docker-ce-[VERSION]

  

tom@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:95ddb6c31407e84e91a986b004aee40975cb0bda14b5949f6faac5d2deadb4b9
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/

  

Docker 常用命令
docker search: 搜索存放在 Docker Hub 仓库中的镜像;
docker pull: 从 Docker Registry 仓库拉取镜像到本地;
docker images: 列出本地的镜像;
docker run: 运行镜像;
docker ps:查看正在运行的 Docker 容器。
更多命令可以参考官方文档。

  

菜鸟的自白
原文地址:https://www.cnblogs.com/lzjloveit/p/14426051.html