理論
問題
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 運行