Docker简介和安装

Docker简介和安装

Docker是什么

Docker 是一个应用打包、分发、部署的工具
你也可以把它理解为一个轻量的虚拟机,它只虚拟你软件需要的运行环境,多余的一点都不要,
而普通虚拟机则是一个完整而庞大的系统,包含各种不管你要不要的软件。

跟普通虚拟机的对比

特性 普通虚拟机 Docker
跨平台 通常只能在桌面级系统运行,例如 Windows/Mac,无法在不带图形界面的服务器上运行 支持的系统非常多,各类 windows 和 Linux 都支持
性能 性能损耗大,内存占用高,因为是把整个完整系统都虚拟出来了 性能好,只虚拟软件所需运行环境,最大化减少没用的配置
自动化 需要手动安装所有东西 一个命令就可以自动部署好所需环境
稳定性 稳定性不高,不同系统差异大 稳定性好,不同系统都一样部署方式

打包、分发、部署

打包:就是把你软件运行所需的依赖、第三方库、软件打包到一起,变成一个安装包

分发:你可以把你打包好的“安装包”上传到一个镜像仓库,其他人可以非常方便的获取和安装

部署:拿着“安装包”就可以一个命令运行起来你的应用,自动模拟出一摸一样的运行环境,不管是在 Windows/Mac/Linux。

重要概念:镜像、容器

镜像:可以理解为软件安装包,可以方便的进行传播和安装。

容器:软件安装后的状态,每个软件运行环境都是独立的、隔离的,称之为容器。

安装Docker

查看系统内核
[root@localhost named]# uname -r
3.10.0-1160.71.1.el7.x86_64
卸载
systemctl stop docker
yum -y remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
安装
yum -y install gcc
yum -y install gcc-c++
卸载
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
安装
yum install -y yum-utils
设置镜像仓库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
跟新软件包索引
yum makecache fast
安装epel
yum install epel-release
安装Docker相关的内容

docker-ce 社区 ee企业版

yum install -y docker-ce docker-ce-cli containerd.io 

#docker-compose-plugin
启动docker
systemctl start docker
安装docker报错
  1. 禁用fastestmirror插件

编辑fastestmirror.conf文件

vim /etc/yum/pluginconf.d/fastestmirror.conf

enabled=1改为enabled=0,保存

[main]
enabled=0
verbose=0
always_print_best_host = true
socket_timeout=3
#  Relative paths are relative to the cachedir (and so works for users as well
# as root).
hostfilepath=timedhosts.txt
maxhostfileage=10
maxthreads=15
#exclude=.gov, facebook
#include_only=.nl,.de,.uk,.ie
  1. 修改yum的配置,不使用插件

编辑yum.conf文件

vim /etc/yum.conf

plugins=1改为plugins=0

[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=0
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release
验证docker
docker run hello-world

image-20220715222254005

安装失败

解决方法:

  1. 切换国内阿里云镜像,创建daemon.json文件
vim /etc/docker/daemon.json
  1. 文件名中添加以下内容
 { 
 "registry-mirrors": ["https://alzgoonw.mirror.aliyuncs.com"] 
 }
  1. 重启docker
systemctl restart docker
systemctl status docker

systemctl restart docker.service
  1. 再次运行docker run hello-world
[root@localhost docker]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
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会在本机寻找镜像 → 判断本机是否有这个镜像 → Y → 使用这个镜像

​ ↓ N ↑  Y

​ 去docker hub上下载镜像 → docker hub 是否找到镜像

​ ↓  N

​ 报错

底层原理

Docker是什么工作的

Docker 是一个 Client - Server 结构的系统, Docker的守护进程运行在主机上,通过socker从客户端访问

DockerServer接受到Docker-Client的指令,就会执行这个命令

Docker为什么比VM快

  1. Docker有比虚拟机更少的抽象层
  2. Docker利用的是宿主机的内核,vm需要是Guest OS

新建容器时,docker不需要向虚拟机一样重新加载一个操作系统的内核,避免引导,虚拟机是加载Guster OS,分钟级别的,而Docker是利用宿主机的操作系统,省略了复杂的过程,秒级。

posted @ 2022-11-16 10:23  Fanslyx  阅读(65)  评论(0)    收藏  举报