前言:
学习之前需要了解docker相关概念,移步 Docker官网
此处不再赘述虚拟化技术和docker VS 虚拟机 的一些内容,有条件的建议直接移步 度娘
笔记全程操作在华为云务器,CentOS7系统。需要自行准备操作环境。
一. Docker的架构介绍
1.1 Docker概述
Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
- Docker 客户端(Client) : Docker 客户端通过命令行或者其他工具使用 Docker SDK与 Docker 的守护进程通信。
- Docker 主机(Host) :一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。
Docker中的三个基本概念:
- 镜像(Image):Docker 镜像,相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
- 容器(Container):镜像和容器的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
- 仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像。
1.2 运行原理
Docker 是一个 C/S模式的架构,后端是一个松耦合架构,众多模块各司其职。
- 用户是使用 Docker Client与 Docker Daemon建立通信,并发送请求给后者。
- Docker Daemon作为 Docker 架构中的主体部分,首先提供 Docker Server 的功能使其可以接受 Docker Client 的请求。
- Docker Engine 执行 Docker内部的一系列工作,每一项工作都是以一个Job的形式的存在。
- Job 的运行过程中,当需要容器镜像时,则从 Docker Registy 中下载镜像,并通过镜像管理驱动 Graph driver将下载镜像以Graph的形式存储。
- 当需要为 Docker 创建网络环境时,通过网络管理驱动 Network driver创建并配置 Docker 容器网络环境。
- 当需要限制 Docker容器运行资源或执行用户指令等操作时,则通过 Exec driver 来完成。
- Libcontainer是一项独立的容器管理包,Netwrk driver以及Exec driver都是通过Libcontainer来实现具体对容器进行的操作。
二. Docker 安装
官网通道:CentOS 安装 Docker
首先关闭防火墙
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
# 查看防火墙状态
systemctl status firewalld
2.1 环境要求
官方要求CentOS7及以上版本,建议使用CentOS7,稳定大于一切。
2.2 卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.3 安装gcc相关
yum -y install gcc
yum -y install gcc-c++
2.4 依赖库
- yum-utils 提供 yum-config-manager 类库
- device-mapper-persistent-data 和 lvm2 被devicemapper 存储驱动依赖
yum install -y yum-utils \
device-mapper-persistent-data \
lvm2 --skip-broken
2.5 设置本地镜像库
# 建议用国内仓库,此处使用阿里镜像仓库
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.6 安装Docker CE
# docker-ce为社区免费版本.稍等片刻,docker即可安装完成
yum install -y docker-ce
2.7 启动Docker
systemctl start docker
2.8 测试docker
# 查看docker版本
docker version
# 查看docker状态
systemctl status docker
2.9 配置镜像加速器
可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
参考阿里云的镜像版本:阿里云镜像加速器
# 创建文件夹
sudo mkdir -p /etc/docker
# 修改配置文件
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://x1zbm066.mirror.aliyuncs.com"]
}
EOF
# 重新加载配置文件
sudo systemctl daemon-reload
# 重启docker
sudo systemctl restart docker
三. Docker 中的 hello-world
Hello-world拉取
[root@local ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
查看拉取到的镜像
[root@local ~]# docker images hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 22 months ago 13.3kB
运行该镜像的实例,即容器
[root@local ~]# docker run hello-world
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 run hello-world也是可以的;它会先检查本地是否有这个镜像,没有的话会先从指定仓库中拉取。
至此,docker的安装配置以及 hello-world容器的运行基本完成。
啰嗦两句:
既然学一次,就安心的学,每个案例多试几次,看看不同的效果,这玩意光看也看不出来什么花儿。
做笔记,多练习,知其然也要知其所以然。加油,共勉!