文章目錄
Image到Container
1.我們通過 docker run image-name 可以生成Container,這裏就不再進行贅述
Container到Image
1.既然image可以生成Container,相反,通過一個Container我們也可以生成一個image
centos樣例
1.我們首先拉取最新版本的centos鏡像
[root@iZwz91h49n3mj8r232gqweZ opt]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:f94c1d992c193b3dc09e297ffd54d8a4f1dc946c37cbeceb26d35ce1647f88d9
Status: Image is up to date for centos:latest
docker.io/library/centos:latest
[root@iZwz91h49n3mj8r232gqweZ opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 0f3e07c0138f 2 months ago 220MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
2.運行鏡像
[root@iZwz91h49n3mj8r232gqweZ opt]# docker run -d -it --name first-centos centos
332439fb5554d60553fff952078ec29511253ba563af5255ad5ac13cb43e2c55
3.修改上面容器Container的配置
[root@iZwz91h49n3mj8r232gqweZ opt]# docker exec -it first-centos bash
[root@332439fb5554 /]# pwd
/
[root@332439fb5554 /]# vim
bash: vim: command not found
[root@332439fb5554 /]# yum install -y vim
Failed to set locale, defaulting to C
CentOS-8 - AppStream 1.4 MB/s | 6.3 MB 00:04
CentOS-8 - Base 2.6 MB/s | 7.9 MB 00:03
CentOS-8 - Extras 817 B/s | 2.1 kB 00:02
Last metadata expiration check: 0:00:01 ago on Sun Dec 1 04:13:02 2019.
Dependencies resolved.
。。。。
Complete!
再次查看是否已經安裝成功,可以使用了
[root@332439fb5554 /]# vim test.txt
4.退出當前容器
[root@332439fb5554 /]# exit
exit
5.根據當前運行的Container 製作最新的image
[root@iZwz91h49n3mj8r232gqweZ opt]# docker commit first-centos vim-centos
sha256:c3a1001b03e45645888fe8c051f0cc28c5021132a02c4bf71b991cd5cb729c38
[root@iZwz91h49n3mj8r232gqweZ opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
vim-centos latest c3a1001b03e4 4 seconds ago 286MB
centos latest 0f3e07c0138f 2 months ago 220MB
hello-world latest fce289e99eb9 11 months ago 1.84kB
6.進入最新的容器的Container,驗證是否包含vim
[root@iZwz91h49n3mj8r232gqweZ opt]# docker run -d -it --name first-vim-centos vim-centos
86a02874f0952c89924f11cfcceb0a0ee7a78b905e2b31d325a6e50a4c8208ab
[root@iZwz91h49n3mj8r232gqweZ opt]# docker exec -it first-vim-centos bash
[root@86a02874f095 /]# vim test.txt
[root@86a02874f095 /]#
1.從上面最新的容器,我們可以看到 已經包含vim這種功能;
Container資源限制
內存限制 --memory 大小
1.對於容器,如果資源不夠用,它會自動去物理機器上去實時或許更多的內存,但是這樣無限制去獲取顯然是不合適的;
因此我們可以在啓動容器的過程中,預先設置內存;
[root@iZwz91h49n3mj8r232gqweZ ~]# docker run -d -it --memory 100M --name first-centos centos
7aa9960b8e1ae75de034d88cd8cdcbd3d4307d49138ac6d427247ede01166147
[root@iZwz91h49n3mj8r232gqweZ ~]#
CPU限制 --cpu-shares 權重比例
[root@iZwz91h49n3mj8r232gqweZ ~]# docker run -d -it --memory 100M --cpu-shares 10 --name second-centos centos
f987e94f9251c01a8b48c591fd7866adb6fd1fccee394acd2452325fa3796860
[root@iZwz91h49n3mj8r232gqweZ ~]#
Container資源監控
圖形化資源監控 Weave Scope
官方地址
https://github.com/weaveworks/scope
概述
1.Weave Scope 是對Docker 以及K8S的一種比較好的監控工具
安裝
[root@iZwz91h49n3mj8r232gqweZ ~]# cd /opt/
[root@iZwz91h49n3mj8r232gqweZ opt]# ll
total 765164
drwx--x--x 4 root root 4096 Dec 1 09:09 containerd
-rw-r--r-- 1 root root 0 Dec 1 09:05 device-mapper-persistent-data
drwxr-xr-x 3 root root 4096 Dec 1 10:36 harbor
-rw-r--r-- 1 root root 597857483 Dec 1 09:43 harbor-offline-installer-v1.7.1.tgz
drwxr-xr-x 7 10 143 4096 Jul 7 2018 jdk1.8.0_181
-rw-r--r-- 1 root root 185646832 Nov 30 19:55 jdk-8u181-linux-x64.tar.gz
-rw-r--r-- 1 root root 163 Dec 1 09:05 lvm2
[root@iZwz91h49n3mj8r232gqweZ opt]# sudo curl -L git.io/scope -o /usr/local/bin/scope
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:05 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:06 --:--:-- 0
0 0 0 595 0 0 87 0 --:--:-- 0:00:06 --:--:-- 87
100 11664 100 11664 0 0 1478 0 0:00:07 0:00:07 --:--:-- 20903
[root@iZwz91h49n3mj8r232gqweZ opt]# sudo chmod a+x /usr/local/bin/scope
[root@iZwz91h49n3mj8r232gqweZ opt]# scope launch
Unable to find image 'weaveworks/scope:1.12.0' locally
1.12.0: Pulling from weaveworks/scope
5d20c808ce19: Pull complete
bbde3b770a97: Pull complete
b648e9d51e9d: Pull complete
d284dfe4b906: Pull complete
83e88b3b6bfb: Pull complete
a00e32a7ca7a: Pull complete
5cbd69f2576c: Pull complete
4915fcaf1597: Pull complete
7d8a6e574629: Pull complete
f0175c50e258: Pull complete
Digest: sha256:2f063422cd74d6b0cc6a4c7a26aeb531a69d719de1fa039ca592877662051446
Status: Downloaded newer image for weaveworks/scope:1.12.0
b9cd90b554769211c5a11d39c2d1a7caf55fa4898da3fb9b44d0e6e90d3df118
Scope probe started
Weave Scope is listening at the following URL(s):
* http://XXX.XXX.XXX.XXX:4040/
* http://XXX.XXX.XXX.XXX:4040/
[root@iZwz91h49n3mj8r232gqweZ opt]#
監控頁面
然後訪問(這裏注意下,需要開通4040的訪問權限)
http://XXX.XXX.XXX.XXX:4040/