自主学习:Docker
笔记编写者:房佳亮
学习视频:Bilibili遇见狂神说
Docker安装
1、Docker的基本组成
镜像(images):
Docker镜像就好比是一个模版,可以通过这个模版创建容器服务,tomcat ----> run ----> tomcat01容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中)
容器(container):
Docker利用容器技术,可以独立运行一个或一组应用,通过镜像来创建。可以使用启动,停止,删除,等基本命令,就目前,可以把这个容器理解成就是一个简易的Linux操作系统。
仓库(repository):
仓库就是存放镜像的地方!仓库分为公有仓库和私有仓库,
Docker Hub(默认是国外的),阿里云等等都有容器服务器(配置镜像加速)
2、安装Docker
环境准备
1、会一点基础的Linux只是
2、CentOS 7
3、使用Xshell(windows),mac直接用终端即可
环境查看
# 系统内核是3.10以上的
[root@iZ2zeefmdpadf6fxudttltZ ~]# uname -r
3.10.0-957.27.2.el7.x86_64
# 系统版本
[root@iZ2zeefmdpadf6fxudttltZ ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装
帮助文档:
# 1. 卸载旧版本
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2. 需要的安装包
sudo yum install -y yum-utils
# 3. 设置镜像的仓库
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 默认国外的,非常慢
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云的非常快
# 更新yum软件包索引
yum makecache fast
# 4. 安装Docker相关的 docker-ce 社区版,docker-ee 企业版
sudo yum install docker-ce docker-ce-cli containerd.io
# 5. 启动Docker
systemctl start docker
# 6. 查看docker版本
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
# 7. HelloWorld程序
[root@iZ2zeefmdpadf6fxudttltZ docker]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8c5aeeb6a5f3ba4883347d3747a7249f491766ca1caa47e5da5dfcf6b9b717c0
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/
# 8. 查看镜像
[root@iZ2zeefmdpadf6fxudttltZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 10 months ago 13.3kB
# 9. 卸载docker
#(1)卸载Docker Engine,CLI和Containerd软件包:
sudo yum remove docker-ce docker-ce-cli containerd.io
#(2)主机上的映像,容器,卷或自定义配置文件不会自动删除。要删除所有图像,容器和卷:
sudo rm -rf /var/lib/docker #docker的默认工作路径
3、配置阿里云镜像加速
(1)登陆阿里云,找到容器镜像服务
(2)找到镜像加速的地址
(3)测试配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://wshphhng.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
4、回顾HelloWorld流程
5、Docker底层原理
Docker是怎么工作的?
Docker是一个Client-Server结构的系统,Docker的守护进程运行在我们的主机(宿主机)上。通过Socke从客户端访问!
DockerServer接收到Docker-Client的执行命令,就会执行这个命令。
Docker为什么比虚拟机(VM…)快?
1、Docker有着比虚拟机更少的抽象层
2、Docker利用的是宿主机的内核,虚拟机需要Guest OS
![点击查看源网页](https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1473564113,4288905819&fm=26&gp=0.jpg)
所以说新建一个容器的时候,Docker不需要向虚拟机一样重新加载一个操作系统内核,避免引导,虚拟机是加载Guest OS,分钟级别的启动,而Docker是利用宿主机的操作系统,省略了这个复杂的过程,秒级启动。