理论
问题
1 换了一个环境或者机器,代码跑不起来了
2 支持5000qps = query per second,突然并发太大,hold不住了
解决 ---- 扩容,买机器,成本高
概念
Docker:Go语言编写的,”Build, ship and run any App, anywhere”
一次编译,到处运行
将应用程序所需的环境,包括底层系统,如:linux,应用程序:tomcat、nginx、java,整体打包成一个模板,也就是镜像,实现跨平台的无缝对接
根据镜像模板,生成一个个docker容器实例
基于linux内核开发的,一般用CentOS,因为性能更好
优势
扩容简单:让镜像一键生成容器实例即可
高效的利用计算资源:共用一个操作系统,提升了CPU和内存的利用率
更轻量:
环境
Docker支持以下的CentOS版本:
CentOS 7 (64-bit) (线上最好7.4以上)
CentOS 6.5 (64-bit) 或更高的版本
前提条件:
目前,CentOS 仅发行版本中的内核支持Docker
Docker 运行在CentOS 7 上,要求系统为64位、系统内核版本为3.10 以上。
Docker 运行在CentOS-6.5 或更高的版本的CentOS 上,要求系统为64位、系统内核版本为2.6.32-431 或者更高版本。
查看系统内核版本:
[root@bogon ~]# uname -r
查看已安装的CentOS版本信息,两种方式:
[root@bogon ~]# lsb_release -a # 有的版本支持
[root@bogon ~]# cat /etc/redhat-release # 都支持
Docker基本组成
Docker 镜像(Image)
是一个只读的模板
作用:创建Docker容器,一个镜像可以创建很多容器实例。
Docker 容器(Container)
Docker利用容器(Container)独立运行一个或一组应用
用镜像创建的运行实例 ,它可以被启动、开始、停止、删除
每个容器都是相互隔离的、保证安全的平台
容器的定义和镜像几乎一模一样,唯一区别在于容器的最上面那一层是可读可写的
Docker仓库(Repository)
是集中存放镜像文件的场所
仓库(Repository)和仓库注册服务器(Registry)是有区别的
仓库注册服务器上往往存放着多个仓库,每个仓库中又包含了多个镜像,每个镜像有不同的标签(tag)
仓库分为公开仓库(Public)和私有仓库(Private)两种形式
最大的公开仓库是Docker Hub(https://hub.docker.com/),存放了数量庞大的镜像供用户下载
国内的公开仓库包括阿里云、网易云等
总结:
从仓库拉下来的是镜像(模板),根据模板生成容器(实例)
CentOS Docker 安装
https://docs.docker.com/engine/install/centos/#installation-methods
Install using the repository
Before you install Docker Engine for the first time on a new host machine, you need to set up the Docker repository. Afterward, you can install and update Docker from the repository.
Set up the repository
Install the yum-utils
package (which provides the yum-config-manager
utility) and set up the stable repository.
$ sudo yum install
-y yum-utils
$ sudo yum-config-manager
\
--add-repo
\
https://download.docker.com/linux/centos/docker-ce.repo
Install Docker Engine
Install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
$ sudo yum install docker-ce docker-ce-cli containerd.io
If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35
, and if so, accept it.
Docker is installed but not started. The docker group is created, but no users are added to the group.
Start Docker.
$ sudo systemctl start docker
Verify that Docker Engine is installed correctly by running the hello-world image.
$ sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
Docker Engine is installed and running. You need to use sudo
to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.
Docker客户端
Docker命令
[root@bogon ~]# docker version 版本信息
[root@bogon ~]# docker info 详细信息
[root@bogon ~]# docker -help 帮助
[root@bogon ~]# docker images
配置镜像加速器
[root@1 ~]# sudo mkdir -p /etc/docker
[root@1 ~]# cd /etc/docker
[root@1 docker]# ls
key.json
[root@1 docker]# vi daemon.json
[root@1 docker]# cat daemon.json
{
"registry-mirrors": ["https://xxxx.mirror.aliyuncs.com"]
}
[root@1 docker]# sudo systemctl daemon-reload
[root@1 docker]# sudo systemctl restart docker
Docker拉取tomcat
https://docs.docker.com/engine/install/centos/
验证速度:
[root@bogon ~]# docker images
[root@bogon ~]# docker pull tomcat ---- timeout,就多试几次!!!
Using default tag: latest
latest: Pulling from library/tomcat
376057ac6fa1: Pull complete
5a63a0a859d8: Pull complete
496548a8c952: Pull complete
2adae3950d4d: Pull complete
0a297eafb9ac: Pull complete
09a4142c5c9d: Pull complete
9e78d9befa39: Pull complete
18f492f90b9c: Pull complete
7834493ec6cd: Pull complete
216b2be21722: Pull complete
Digest: sha256:ce753be7b61d86f877fe5065eb20c23491f783f283f25f6914ba769fee57886b
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@bogon ~]# docker images
问题:一个tomcat 是10-20MB,但是这个里面大,有647MB,为什么?
回答:里面有环境:centos等等
[root@bogon ~]# docker pull hello-world 如果没有写版本,默认是最新
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest
[root@bogon ~]# docker images
[root@bogon ~]# docker run hello-world 运行