Docker 中的名词概念
镜像(Images):
Docker镜像就好比是一个模板,可以通过这个模板来创建容器服务,tomcat镜像===> run ==> tomcat01容器(提供服务器)通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
镜像和容器的关系就像是类和对象实例的关系
容器(Containers):
Docker利用容器技术,独立运行一个或者一个组应用,通过镜像来创建的。
启动、停止、删除、等基本命令
目前就可以把这个容器理解为就是一个简易的linux系统
仓库(Repository):
仓库就是存放镜像的地方
仓库分为 公有仓库 和 私有仓库
Docker Hub (默认是国外的)
阿里云...都有容器服务器(配置镜像加速)
安装 Docker
环境准备
系统是 3.10 以上的
[root@xiaoyequ ~]# uname -r
3.10.0-1062.18.1.el7.x86_64
系统版本
[root@xiaoyequ ~]# 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"
安装
帮助文档:https://docs.docker.com/engine/install/centos/
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 \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4、安装 Docker 相关的
更新 yum 软件包索引
[root@xiaoyequ ~]# yum makecache fast
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* centos-sclo-rh: mirrors.tuna.tsinghua.edu.cn
* centos-sclo-sclo: mirror.bit.edu.cn
base | 3.6 kB 00:00:00
centos-sclo-rh | 3.0 kB 00:00:00
centos-sclo-sclo | 3.0 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/x86_64/updateinfo | 55 B 00:00:00
(2/2): docker-ce-stable/x86_64/primary_db | 43 kB 00:00:00
Metadata Cache Created
docker-ce 社区版;docker-ee 企业版
yum install docker-ce docker-ce-cli containerd.io
5、启动 Docker
systemctl start docker
查看 Docker 版本,此时意味着安装成功!
[root@xiaoyequ ~]# docker version
Client: Docker Engine - Community
Version: 19.03.10
API version: 1.40
Go version: go1.13.10
Git commit: 9424aeaee9
Built: Thu May 28 22:18:06 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.10
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 9424aeaee9
Built: Thu May 28 22:16:43 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
6、测试 helloword
docker run hello-world
测试成功!安装成功
[root@xiaoyequ ~]# 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/
7、查看一下载的 hello-world 镜像
[root@xiaoyequ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
8、卸载 Docker
卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
删除资源
rm -rf /var/lib/docker # docker的默认工作路径
配置阿里云的镜像加速
1、找到 “容器镜像服务”
2、找到镜像加速地址
3、配置使用即可
Run 的流程
底层原理
Docker 是怎么工作的
Docker 是一个 Client-Server 结构的系统,Docker 的守护进程运行在主机上。通过 Socket 从客户端访问
DockerServer 接收到 Docker-Client 的指令,就会执行这个命令
Docker 为什么比虚拟机快
1、Docker有着比虚拟机更少的抽象层。
2、docker 利用的是宿主机的内核,vm需要是Guest OS
所以说,新建一个容器的时候,Docker不需要想虚拟机一样重新加载一个操作系统内核,避免引导。
虚拟机是加载 Guest OS,分钟级别的;而 Docker 是利用宿主机的操作系统,省略了这个复杂的过程,秒级!