Docker和传统虚拟机区别
- 传统虚拟机是虚拟出一套硬件系统,运行一个完整的操作系统,然后再系统上安装和运行软件
- docker容器不需要自己的内核,因此非常轻量级
docker容器内都有一个属于自己的文件系统,每个容器之间互相隔离,之间互不影响
Docker的DevOps思想(开发、运维)
更快速地交付和部署
- 传统部署:需要提供一堆配置文件、部署文档
- docker:打包镜像,测试发布,一键运行
- 更便捷的升级和扩缩容
- 一键即可在集群上打包 部署
- 更简单的系统运维
- 更高效的利用计算资源
- 通过容器技术可以在一个服务器上部署很多个tomcat,将服务器的性能发挥到极致
docker架构图
镜像(image)
容器(container)
启动的镜像叫做容器,可以把容器理解为一个简易的linux系统
仓库(repository)
仓库是存放镜像的地方,仓库分为共有仓库(官方的为docker hub,国内的镜像仓库有阿里云、网易等等仓库)、私有仓库(自己搭建的私有镜像仓库)
docker安装
帮助文档:https://docs.docker.com/engine/install/centos/
su先进入root用户模式
Install Docker Engine on CentOSlinux确认环境
查看文件
查看系统环境[admin@localhost /]$ ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
查看系统版本# 系统内核是 3.10 以上的
[admin@localhost /]$ uname -r
4.18.0-305.3.1.el8.x86_64
[admin@localhost /]$ cat /etc/os-release
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
1、卸载旧的版本
命令回显sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
No match for argument: docker
No match for argument: docker-client
No match for argument: docker-client-latest
No match for argument: docker-common
No match for argument: docker-latest
No match for argument: docker-latest-logrotate
No match for argument: docker-logrotate
No match for argument: docker-engine
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!
2、需要的安装包
命令回显 ```shell CentOS Linux 8 - AppStream 4.5 MB/s | 8.2 MB 00:01sudo yum install -y yum-utils
CentOS Linux 8 - BaseOS 447 kB/s | 4.5 MB 00:10
CentOS Linux 8 - Extras 11 kB/s | 9.8 kB 00:00Dependencies resolved.
Package Architecture Version Repository Size
Installing: yum-utils noarch 4.0.18-4.el8 baseos 71 k
- 通过容器技术可以在一个服务器上部署很多个tomcat,将服务器的性能发挥到极致
Transaction Summary
Install 1 Package
Total download size: 71 k Installed size: 22 k Downloading Packages:
yum-utils-4.0.18-4.el8.noarch.rpm 51 kB/s | 71 kB 00:01
Total 29 kB/s | 71 kB 00:02
warning: /var/cache/dnf/baseos-929b586ef1f72f69/packages/yum-utils-4.0.18-4.el8.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - BaseOS 714 kB/s | 1.6 kB 00:00
Importing GPG key 0x8483C65D:
Userid : “CentOS (CentOS Official Signing Key) security@centos.org“
Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : yum-utils-4.0.18-4.el8.noarch 1/1
Running scriptlet: yum-utils-4.0.18-4.el8.noarch 1/1
Verifying : yum-utils-4.0.18-4.el8.noarch 1/1
Installed products updated.
Installed: yum-utils-4.0.18-4.el8.noarch
Complete!
<a name="PDNVl"></a>
## 3、设置镜像的仓库
```shell
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo # 默认是从国外的
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 推荐使用阿里云,十分快
更新yum软件包索引
yum makecache
4、安装docker docker-ce 社区 ee 企业版
sudo yum install docker-ce docker-ce-cli containerd.io
5、启动docker
sudo systemctl start docker
6、使用docker version 是否安装成功
[root@localhost /]# docker version
Client: Docker Engine - Community
Version: 20.10.7
API version: 1.41
Go version: go1.13.15
Git commit: f0df350
Built: Wed Jun 2 11:56:24 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.7
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: b0f5bc3
Built: Wed Jun 2 11:54:48 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.8
GitCommit: 7eba5930496d9bbe375fdf71603e610ad737d2b2
runc:
Version: 1.0.0
GitCommit: v1.0.0-0-g84113ee
docker-init:
Version: 0.19.0
GitCommit: de40ad0
7、测试hello world
sudo docker run hello-world
命令回显
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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、查看一下下载的这个hello-world镜像
docker images
命令回显
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
9、了解:卸载docker
1、卸载依赖
sudo yum remove docker-ce docker-ce-cli containerd.io
2、删除资源
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
3、docker的默认工作路径
/var/lib/containerd
阿里云镜像加速
系统环境:CentOS7
1. 访问 https://www.aliyun.com/ ,搜索“容器镜像服务”
2. 点击“管理控制台”,注册一个属于自己的阿里云账户(可复用淘宝账号)
3. 登录,根据引导进入主界面
4. 点击“镜像加速器”,获取加速器地址,下面也有操作文档
5. 添加或修改/etc/docker/daemon.json配置加速器
mkdir -p /etc/docker
vim /etc/docker/daemon.json
{
# 添加阿里云上面的加速器地址:
"registry-mirrors": ["https://{xxxxxxx}.mirror.aliyuncs.com"]
}
systemctl daemon-reload
systemctl restart docker
这里提供一个可用的加速器:
{
"registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"]
}
回顾HelloWorld流程
底层原理
Docker是怎么工作的?
Docker是一个Client - Server 结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问!
Docker-Server接收到Docker-Client 的指令,就会执行这个命令!
Docker为什么比VM快?
1、Docker有着比虚拟机更少的抽象层
2、Docker利用的是宿主机的内核,vm需要是Guest OS。
所以说,新建一个容器时,docker直接运行在宿主机内核上,运行级别为秒级;虚拟机是需要重新加载一个Guest OS内核,运行级别为分钟级。