28: 构建自己的centos
- 99%的镜像是从基础镜像FROM scratch构建过来.
- 构建自己的centos
- 编写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