1.dockerhub centos官网的dockerfile
FROM scratch
ADD centos-8-x86_64.tar.xz /
LABEL org.label-schema.schema-version="1.0" org.label-schema.name="CentOS Base Image" org.label-schema.vendor="CentOS" org.label-schema.license="GPLv2" org.label-schema.build-date="20201204"
CMD ["/bin/bash"]
2.dockerFile实战
1.在/usr/home 里创建一个文件夹存放所有的dockerFile
cd /usr/home
mkdir dockerfiles
cd dockerfiles
vim myCentos
2.在myCentos里 编写dockerFile文件
FROM centos
MAINTAINER hc<464945880@qq.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "---end--"
CMD /bin/bash
3.通过编写的myCentos文件创建镜像
[root@hc dockerfiles]# docker build -f myCentos -t mycentos:1.0 .
参数说明:
-f :指定要使用的Dockerfile路径;
--tag, -t: 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
运行输出如下:
[root@hc dockerfiles]# docker build -f myCentos -t mycentos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/9 : FROM centos
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
---> 300e315adb2f
Step 2/9 : MAINTAINER hc<464945880@qq.com>
---> Running in 92a4eb633bed
Removing intermediate container 92a4eb633bed
---> ea0187665841
Step 3/9 : ENV MYPATH /usr/local
---> Running in 1b5287f7c25e
Removing intermediate container 1b5287f7c25e
---> 5aacc57adb86
Step 4/9 : WORKDIR $MYPATH
---> Running in 09e04ae9baba
Removing intermediate container 09e04ae9baba
---> 7aaeacf26a60
Step 5/9 : RUN yum -y install vim
---> Running in 2555fed69777
CentOS Linux 8 - AppStream 2.8 MB/s | 6.3 MB 00:02
CentOS Linux 8 - BaseOS 3.3 MB/s | 2.3 MB 00:00
CentOS Linux 8 - Extras 5.0 kB/s | 9.6 kB 00:01
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-15.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-15.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k
which x86_64 2.21-12.el8 baseos 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 1.0 MB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 4.1 MB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 857 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 9.8 MB/s | 1.4 MB 00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 13 MB/s | 6.3 MB 00:00
--------------------------------------------------------------------------------
Total 5.7 MB/s | 7.8 MB 00:01
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 Linux 8 - AppStream 1.6 MB/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-12.el8.x86_64 1/5
Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-15.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-15.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5
Verifying : which-2.21-12.el8.x86_64 5/5
Installed:
gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 2555fed69777
---> ace8bafcab48
Step 6/9 : RUN yum -y install net-tools
---> Running in 97af7730e280
Last metadata expiration check: 0:00:08 ago on Wed May 19 13:58:10 2021.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 3.7 MB/s | 322 kB 00:00
--------------------------------------------------------------------------------
Total 590 kB/s | 322 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container 97af7730e280
---> bf137ef7de69
Step 7/9 : EXPOSE 80
---> Running in 439e38770a70
Removing intermediate container 439e38770a70
---> 3c926f2a6c35
Step 8/9 : CMD echo "---end--"
---> Running in 66d347f17eb7
Removing intermediate container 66d347f17eb7
---> dd9399b1eaf8
Step 9/9 : CMD /bin/bash
---> Running in ce3bb950653c
Removing intermediate container ce3bb950653c
---> 7183d35483d8
Successfully built 7183d35483d8
Successfully tagged mycentos:1.0
[root@hc dockerfiles]#
3.测试运行
[root@hc dockerfiles]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 1.0 7183d35483d8 4 minutes ago 282MB
nginx latest f0b8a9a54136 7 days ago 133MB
mysql 5.7 2c9028880e58 7 days ago 447MB
xuxueli/xxl-job-admin 2.3.0 24cc0d9dcf54 3 months ago 223MB
centos latest 300e315adb2f 5 months ago 209MB
[root@hc dockerfiles]# docker run -it 7183d35483d8
[root@2ab730265ab6 local]# pwd
/usr/local
docker history 容器id: 查看当前镜像是如何一步一步构建起来的
[root@hc dockerfiles]# docker history 7183d35483d8
IMAGE CREATED CREATED BY SIZE COMMENT
7183d35483d8 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
dd9399b1eaf8 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
3c926f2a6c35 9 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
bf137ef7de69 9 minutes ago /bin/sh -c yum -y install net-tools 14.3MB
ace8bafcab48 9 minutes ago /bin/sh -c yum -y install vim 58MB
7aaeacf26a60 9 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
5aacc57adb86 9 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
ea0187665841 9 minutes ago /bin/sh -c #(nop) MAINTAINER hc<464945880@q… 0B
300e315adb2f 5 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 5 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB