二、Docker安装

1.Docker的基本组成

镜像(image)

docker镜像就好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像==>run==>容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)

容器(container)

Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的
启动,停止,删除,基本命令
目前就可以把这个容器理解为就是一个简易的 Linux系统

仓库(repository)

仓库就是存放镜像的地方!
仓库分为公有仓库和私有仓库。(很类似git)
Docker Hub 默认是国外的。
阿里云...都有容器服务器(配置镜像加速!)

2.安装Docker

环境准备

要求Linux内核3.0以上

[root@fedora ~]# uname -r
5.17.14-300.fc36.x86_64
[root@fedora ~]# cat /etc/os-release 
NAME="Fedora Linux"
VERSION="36 (Workstation Edition)"

安装

官方文档:

https://docs.docker.com/engine/install/

# 1.卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2.需要的安装包
yum install -y yum-utils
# 3.设置镜像的仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
# 默认是从国外的,不推荐
# 推荐使用国内的
yum-config-manager \
--add-repo \
https://mirrors.aliyun.com/docker-ce/linux/fedora/docker-ce.repo
# https://mirrors.aliyun.com/docker-ce/linux/(选择与你的系统相匹配的配置文件)
# 如果你的系统是centos就用centos
# https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#更新yum软件包索引
yum repolist
#4.安装docker相关的 docker-ce 社区版 而ee是企业版
yum install docker-ce docker-ce-cli containerd.io
#6. 使用docker version查看是否安装成功
systemctl start docker # 开启docker服务
systemctl enable docker # 将docker服务设置为开机自启
docker version
#7. 测试
docker run hello-world
[root@fedora ~]# 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/
# 8.查看下载的镜像
[root@fedora ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   9 months ago   13.3kB

了解:卸载Docker

# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
# 2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径

阿里云镜像加速

阿里云官网

登录阿里云-->产品-->容器与中间件-->容器镜像服务-->管理控制台-->镜像工具-->镜像加速器

配置镜像加速器

通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://cuus1v0b.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

3.底层原理

Docker是怎么工作的

Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问!
Docker-Server接收到Docker-Client的指令,就会执行这个命令!

为什么DockerVM快?

1docker有着比虚拟机更少的抽象层。由于docker不需要Hypervisor实现硬件资源虚拟化,运行在
docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在
效率上有明显优势。

2docker利用的是宿主机的内核,而不需要Guest OS
GuestOS VM(虚拟机)里的的系统(OS
HostOS:物理机里的系统(OS

当新建一个 容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。从而避免引导、加载操作系统内核这个比较费时费资源的过程。

当新建一个虚拟机时,虚拟机软件需要加载GuestOS,整个新建过程是分钟级别的。docker由于直接利用宿主机的操作系统,则省略了这个复杂的过程,因此新建一个docker容器只需要几秒钟。

docker run 的底层原理

posted @ 2022-06-21 10:37  CharlieBrown  阅读(104)  评论(0编辑  收藏  举报
标题