2020-06-11 docker-3 实践

28: 构建自己的centos

  • 99%的镜像是从基础镜像FROM scratch构建过来.
  • 构建自己的centos
  1. 编写Dockerfile的文件.
MAINTAINER lvyang<tylvyang@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim 
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "-----end----"
CMD /bin/bash
  • 通过这个文件构建镜像
  docker build -f mydockerfile-centos -t mycentos:0.1 .
[root@localhost home]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon  180.1MB
Step 1/10 : FROM centos
 ---> 470671670cac
Step 2/10 : MAINTAINER lvyang<tylvyang@qq.com>
 ---> Using cache
 ---> 3dbddf5bb314
Step 3/10 : ENV MYPATH /usr/local
 ---> Using cache
 ---> 85cfcddb2714
Step 4/10 : WORKDIR $MYPATH
 ---> Using cache
 ---> a9b9077abac6
Step 5/10 : RUN yum -y install vim
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in edcbcc87975e
CentOS-8 - AppStream                            0.0  B/s |   0  B     00:20    
Failed to download metadata for repo 'AppStream'
Error: Failed to download metadata for repo 'AppStream'
The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1
[root@localhost home]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2020-06-11 20:31:00--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 36.99.22.241, 1.180.31.244, 1.180.31.238, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|36.99.22.241|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[============================================================================================================>] 2,523       --.-K/s   in 0s      

2020-06-11 20:31:00 (175 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

[root@localhost home]# yum makecache
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                                                                           | 7.6 kB  00:00:00     

 * base: mirrors.aliyun.com
 * epel: mirrors.yun-idc.com
 * extras: mirrors.aliyun.com
 * remi-safe: mirror.innosol.asia
 * updates: mirrors.aliyun.com
base                                                                                                                           | 3.6 kB  00:00:00     
docker-ce-stable                                                                                                               | 3.5 kB  00:00:00     
extras                                                                                                                         | 2.9 kB  00:00:00     
mysql-connectors-community                                                                                                     | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                          | 2.5 kB  00:00:00     
mysql56-community                                                                                                              | 2.5 kB  00:00:00     
remi-safe                                                                                                                      | 3.0 kB  00:00:00     
updates                                                                                                                        | 2.9 kB  00:00:00     


(1/21): docker-ce-stable/x86_64/filelists_db                                                                                   |  20 kB  00:00:01     
(2/21): docker-ce-stable/x86_64/other_db                                                                                       | 114 kB  00:00:01     
(3/21): epel/x86_64/prestodelta                                                                                                | 4.0 kB  00:00:00     
(4/21): base/7/x86_64/other_db                                                                                                 | 2.6 MB  00:00:03     
(5/21): extras/7/x86_64/filelists_db                                                                                           | 205 kB  00:00:01     
(6/21): extras/7/x86_64/other_db                                                                                               | 122 kB  00:00:00     
(7/21): mysql-connectors-community/x86_64/other_db                                                                             |  15 kB  00:00:01     
(8/21): mysql-connectors-community/x86_64/filelists_db                                                                         |  78 kB  00:00:02     
(9/21): mysql-tools-community/x86_64/filelists_db                                                                              | 246 kB  00:00:00     
(10/21): mysql-tools-community/x86_64/other_db                                                                                 |  16 kB  00:00:00     
(11/21): epel/x86_64/other_db                                                                                                  | 3.3 MB  00:00:04     
(12/21): mysql56-community/x86_64/filelists_db                                                                                 | 839 kB  00:00:01     
(13/21): mysql56-community/x86_64/other_db                                                                                     |  66 kB  00:00:00     
(14/21): base/7/x86_64/filelists_db                                                                                            | 7.1 MB  00:00:09     
remi-safe/other_db             FAILED                                          ===================                  ] 2.3 MB/s |  21 MB  00:00:05 ETA 
http://mirror.bebout.net/remi/enterprise/7/safe/x86_64/repodata/2a9f33cd43c0bb09246e7f1c553055f7ab92e24fbe4f3b25a0a435dd3f9f0442-other.sqlite.bz2: [Errno 14] HTTP Error 404 - Not Found
Trying other mirror.
To address this issue please refer to the below wiki article 

https://wiki.centos.org/yum-errors

If above article doesn't help to resolve this issue please use https://bugs.centos.org/.

(15/21): updates/7/x86_64/filelists_db                                                                                         | 1.3 MB  00:00:01     
(16/21): updates/7/x86_64/other_db                                                                                             | 199 kB  00:00:00     
(17/21): remi-safe/filelists_db                                                                                                | 1.3 MB  00:00:04     
(18/21): updates/7/x86_64/primary_db                                                                                           | 2.1 MB  00:00:02     
(19/21): epel/x86_64/filelists_db                                                                                              |  12 MB  00:00:12     
(20/21): remi-safe/other_db                                                                                                    | 457 kB  00:00:04     
(21/21): remi-safe/primary_db                                                                                                  | 1.7 MB  00:00:09     
Metadata Cache Created
[root@localhost home]#   systemctl restart docker
[root@localhost home]# 
[root@localhost home]# 
[root@localhost home]#   systemctl restart docker
[root@localhost home]# docker build -f mydockerfile-centos -t mycentos:0.1 .                                 
Sending build context to Docker daemon  180.1MB
Step 1/10 : FROM centos
 ---> 470671670cac
Step 2/10 : MAINTAINER lvyang<tylvyang@qq.com>
 ---> Using cache
 ---> 3dbddf5bb314
Step 3/10 : ENV MYPATH /usr/local
 ---> Using cache
 ---> 85cfcddb2714
Step 4/10 : WORKDIR $MYPATH
 ---> Using cache
 ---> a9b9077abac6
Step 5/10 : RUN yum -y install vim
 ---> Running in 1c57cc9c1fa6
CentOS-8 - AppStream                            1.7 MB/s | 7.0 MB     00:04    
CentOS-8 - Base                                 1.0 MB/s | 2.2 MB     00:02    
CentOS-8 - Extras                               8.3 kB/s | 6.7 kB     00:00    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-13.el8         AppStream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-15.el8             AppStream       39 k
 vim-common          x86_64      2:8.0.1763-13.el8         AppStream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-13.el8         AppStream       48 k
 which               x86_64      2.21-10.el8               BaseOS          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm        167 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 489 kB/s |  48 kB     00:00    
(3/5): which-2.21-10.el8.x86_64.rpm             108 kB/s |  49 kB     00:00    
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  1.0 MB/s | 1.4 MB     00:01    
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    1.9 MB/s | 6.3 MB     00:03    
--------------------------------------------------------------------------------
Total                                           1.8 MB/s | 7.8 MB     00:04     
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream                            689 kB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : which-2.21-10.el8.x86_64                               1/5 
  Installing       : vim-filesystem-2:8.0.1763-13.el8.noarch                2/5 
  Installing       : vim-common-2:8.0.1763-13.el8.x86_64                    3/5 
  Installing       : gpm-libs-1.20.7-15.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64                          4/5 
  Installing       : vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5 
  Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5 
  Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64                    5/5 
  Verifying        : gpm-libs-1.20.7-15.el8.x86_64                          1/5 
  Verifying        : vim-common-2:8.0.1763-13.el8.x86_64                    2/5 
  Verifying        : vim-enhanced-2:8.0.1763-13.el8.x86_64                  3/5 
  Verifying        : vim-filesystem-2:8.0.1763-13.el8.noarch                4/5 
  Verifying        : which-2.21-10.el8.x86_64                               5/5 

Installed:
  vim-enhanced-2:8.0.1763-13.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64          
  vim-common-2:8.0.1763-13.el8.x86_64   vim-filesystem-2:8.0.1763-13.el8.noarch
  which-2.21-10.el8.x86_64             

Complete!
Removing intermediate container 1c57cc9c1fa6
 ---> 15118c83da7c
Step 6/10 : RUN yum -y install net-tools
 ---> Running in e8a806dbf7a6
Last metadata expiration check: 0:00:20 ago on Thu Jun 11 12:34:14 2020.
Dependencies resolved.
================================================================================
 Package         Architecture Version                        Repository    Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.51.20160912git.el8       BaseOS       323 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 323 k
Installed size: 1.0 M
Downloading Packages:
net-tools-2.0-0.51.20160912git.el8.x86_64.rpm   471 kB/s | 323 kB     00:00    
--------------------------------------------------------------------------------
Total                                           250 kB/s | 323 kB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 
  Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 
  Verifying        : net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 

Installed:
  net-tools-2.0-0.51.20160912git.el8.x86_64                                     

Complete!
Removing intermediate container e8a806dbf7a6
 ---> caeab6d53071
Step 7/10 : EXPOSE 80
 ---> Running in c0c9fa55e554
Removing intermediate container c0c9fa55e554
 ---> 2059a1c53307
Step 8/10 : CMD echo $MYPATH
 ---> Running in 8aeeeb25e3c3
Removing intermediate container 8aeeeb25e3c3
 ---> 02ae7d85384a
Step 9/10 : CMD echo "-----end----"
 ---> Running in b7528e4c3c76
Removing intermediate container b7528e4c3c76
 ---> 47d7666f6c9b
Step 10/10 : CMD /bin/bash
 ---> Running in 4239fbfc78ae
Removing intermediate container 4239fbfc78ae
 ---> 5e32d738ab86
Successfully built 5e32d738ab86
Successfully tagged mycentos:0.1

报错解决

  • Failed to download metadata for repo 'AppStream' Error:
  • 解决方案:
1. 下载阿里源:

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

2. 运行  yum makecache  生成缓存(或 yum clean all&&yum makecache )

3. 重启docker服务【 重要 】

systemctl restart docker

[root@localhost home]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
mycentos              0.1                 5e32d738ab86        3 minutes ago       321MB
lvyang/centos         1.0                 b8a01d973fc0        23 hours ago        237MB
nginx                 latest              2622e6cca7eb        44 hours ago        132MB
mysql                 5.7                 9cfcce23593a        2 days ago          448MB
mysql                 latest              be0dbf01a0f3        2 days ago          541MB
portainer/portainer   latest              cd645f5a4769        9 days ago          79.1MB
tomcat                9.0                 1b6b1fe7261e        3 weeks ago         647MB
tomcat                latest              1b6b1fe7261e        3 weeks ago         647MB
elasticsearch         7.6.2               f29a1ee41030        2 months ago        791MB
centos                latest              470671670cac        4 months ago        237MB
[root@localhost home]# docker run -it mycentos:0.1
[root@58c61d39779d local]# 
[root@localhost ~]# docker history 5e32d738ab86
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
5e32d738ab86        10 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B                  
47d7666f6c9b        10 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
02ae7d85384a        10 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
2059a1c53307        10 minutes ago      /bin/sh -c #(nop)  EXPOSE 80                    0B                  
caeab6d53071        10 minutes ago      /bin/sh -c yum -y install net-tools             24MB                
15118c83da7c        10 minutes ago      /bin/sh -c yum -y install vim                   59.8MB              
a9b9077abac6        17 minutes ago      /bin/sh -c #(nop) WORKDIR /usr/local            0B                  
85cfcddb2714        17 minutes ago      /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B                  
3dbddf5bb314        17 minutes ago      /bin/sh -c #(nop)  MAINTAINER lvyang<tylvyan…   0B                  
470671670cac        4 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           4 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           4 months ago        /bin/sh -c #(nop) ADD file:aa54047c80ba30064…   237MB  
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279