其他文章:
----------------------------------------------------------------
二、安装
名词
镜像 image
docker镜像就好比一个模板,可以通过这个模板来创建容器服务,Tomcat镜像–》run -->tomcat01容器。通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。
容器 container
Docker利用容器技术,独立运行一个或一组应用,通过镜像来创建。
仓库 repository
仓库就是存放镜像的地方。
系统要求
centso7以上
测试机器信息:
[root@localhost ~]#
[root@localhost ~]# uname -r
3.10.0-957.el7.x86_64
[root@localhost ~]#
[root@localhost ~]# 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"
[root@localhost ~]#
安装步骤:
官方地址:https://docs.docker.com/engine/install/centos/
# 1.卸载已经安装的docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 2.安装(安装方式:使用仓库方式安装)
# 2.1安装工具
sudo yum install -y yum-utils
# 2.2.设置镜像仓库
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 # 阿里云镜像 推荐
# 3.建议更新yum软件包索引
sudo yum makecache fast
# 4.安装docker引擎(默认安装最新版本,也可指定版本)
sudo yum install docker-ce docker-ce-cli containerd.io # docker-ce:社区版(推荐) ee是企业版 cli是客户端 containerd是容器相关
# 5.启动docker(作为服务启动)
sudo systemctl start docker
sudo docker version
[root@localhost ~]# sudo systemctl start docker # 重启systemctl restart docker
[root@localhost ~]# sudo docker version
Client: Docker Engine - Community # 引擎的客户端信息
Version: 20.10.6
API version: 1.41
Go version: go1.13.15
Git commit: 370c289
Built: Fri Apr 9 22:45:33 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community # 引擎的服务端信息
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:43:57 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
[root@localhost ~]#
# 6.测试hello-world镜像
sudo docker run hello-world
[root@localhost ~]#
[root@localhost ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally #本地没有hello-world镜像
latest: Pulling from library/hello-world # 从远程仓库拉取镜像
b8dfde127a29: Pull complete
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
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/
[root@localhost ~]#
# 7.查看拉取的hello-world镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 7 weeks ago 13.3kB
[root@localhost ~]#
#--------------以下信息可以了解--------------------#
# 1.卸载docker
sudo yum remove docker-ce docker-ce-cli containerd.io
# 2.删除镜像
sudo rm -rf /var/lib/docker # 默认工作路径
sudo rm -rf /var/lib/containerd
# 2.删除镜像
sudo rm -rf /var/lib/docker # 默认工作路径
sudo rm -rf /var/lib/containerd
[root@localhost volumes]# cd /var/lib/docker
[root@localhost docker]#
[root@localhost docker]# ls # 这个目录中包括了docker的所有信息 containers image network swarm volumes 等
buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes
[root@localhost docker]#
docker run的运行流程
底层原理
docker是怎么工作的?
Docker是一个CS架构,Docker的守护进程运行在主机上,通过socket从客户端访问。
DockerServer接收到Docker-Client的指令就会执行这个命令。
client就是执行docker命令的 docker
.
容器内的端口和服务器的端口是不一样的,需要映射。
Docker为什么比VM快?
1.Docker的抽象层更少
2.docker是利用宿主机的内核,VM需要的是GuestOS
阿里云镜像加速 一定要配置 否则很慢
参考:https://www.cnblogs.com/allenjing/p/12575972.html
阿里云:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors