1.自定义一个Dockerfile

FROM centos // 继承本地centos的镜像
MAINTAINER zzyyzzyy167@126.com // 作者和邮箱
ENV MYPATH /usr/local
WORKDIR $MYPATH //引用地址,为登录后的落脚点
RUN yum -y install vim //原版的centos镜像中没有vim和 netwok模块
RUN yum -y install net-tools
EXPOSE 80 //暴露端口
CMD echo $MYPATH
CMD echo “success——-…..——ok”
CMD /bin/bash

2.安装过程:

安装 vim和net-tools模块消耗的时间多一些

  1. [root@test mydocker]# docker build -f Dockerfile1 -t mycentos:1.1 .
  2. Sending build context to Docker daemon 3.072kB
  3. Step 1/8 : FROM centos
  4. ---> 300e315adb2f
  5. Step 2/8 : MAINTAINER zhy<123@qq.com>
  6. ---> Using cache
  7. ---> 5ce63c157e9a
  8. Step 3/8 : ENV MYPATH /usr/local
  9. ---> Using cache
  10. ---> 27bda98124bf
  11. Step 4/8 : WORKDIR $MYPATH
  12. ---> Using cache
  13. ---> 2cb99337c3af
  14. Step 5/8 : RUN yum -y install vim
  15. ---> Running in 9268967cb2bb
  16. CentOS Linux 8 - AppStream 382 kB/s | 6.3 MB 00:16
  17. CentOS Linux 8 - BaseOS 373 kB/s | 2.3 MB 00:06
  18. CentOS Linux 8 - Extras 12 kB/s | 8.6 kB 00:00
  19. Dependencies resolved.
  20. ================================================================================
  21. Package Arch Version Repository Size
  22. ================================================================================
  23. Installing:
  24. vim-enhanced x86_64 2:8.0.1763-15.el8 appstream 1.4 M
  25. Installing dependencies:
  26. gpm-libs x86_64 1.20.7-15.el8 appstream 39 k
  27. vim-common x86_64 2:8.0.1763-15.el8 appstream 6.3 M
  28. vim-filesystem noarch 2:8.0.1763-15.el8 appstream 48 k
  29. which x86_64 2.21-12.el8 baseos 49 k
  30. Transaction Summary
  31. ================================================================================
  32. Install 5 Packages
  33. Total download size: 7.8 M
  34. Installed size: 30 M
  35. Downloading Packages:
  36. (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 238 kB/s | 39 kB 00:00
  37. (2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 576 kB/s | 48 kB 00:00
  38. (3/5): which-2.21-12.el8.x86_64.rpm 26 kB/s | 49 kB 00:01
  39. (4/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 309 kB/s | 1.4 MB 00:04
  40. (5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 194 kB/s | 6.3 MB 00:33
  41. --------------------------------------------------------------------------------
  42. Total 222 kB/s | 7.8 MB 00:36
  43. 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
  44. CentOS Linux 8 - AppStream 206 kB/s | 1.6 kB 00:00
  45. Importing GPG key 0x8483C65D:
  46. Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>"
  47. Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
  48. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
  49. Key imported successfully
  50. Running transaction check
  51. Transaction check succeeded.
  52. Running transaction test
  53. Transaction test succeeded.
  54. Running transaction
  55. Preparing : 1/1
  56. Installing : which-2.21-12.el8.x86_64 1/5
  57. Installing : vim-filesystem-2:8.0.1763-15.el8.noarch 2/5
  58. Installing : vim-common-2:8.0.1763-15.el8.x86_64 3/5
  59. Installing : gpm-libs-1.20.7-15.el8.x86_64 4/5
  60. Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/5
  61. Installing : vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
  62. Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
  63. Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
  64. Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
  65. Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
  66. Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
  67. Verifying : vim-filesystem-2:8.0.1763-15.el8.noarch 4/5
  68. Verifying : which-2.21-12.el8.x86_64 5/5
  69. Installed:
  70. gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-15.el8.x86_64
  71. vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
  72. which-2.21-12.el8.x86_64
  73. Complete!
  74. Removing intermediate container 9268967cb2bb
  75. ---> f4e0ad28a3c5
  76. Step 6/8 : RUN yum -y install net-tools
  77. ---> Running in f5d010858c79
  78. Last metadata expiration check: 0:00:49 ago on Sun Jan 24 08:45:54 2021.
  79. Dependencies resolved.
  80. ================================================================================
  81. Package Architecture Version Repository Size
  82. ================================================================================
  83. Installing:
  84. net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
  85. Transaction Summary
  86. ================================================================================
  87. Install 1 Package
  88. Total download size: 322 k
  89. Installed size: 942 k
  90. Downloading Packages:
  91. net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 117 kB/s | 322 kB 00:02
  92. --------------------------------------------------------------------------------
  93. Total 96 kB/s | 322 kB 00:03
  94. Running transaction check
  95. Transaction check succeeded.
  96. Running transaction test
  97. Transaction test succeeded.
  98. Running transaction
  99. Preparing : 1/1
  100. Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
  101. Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
  102. Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
  103. Installed:
  104. net-tools-2.0-0.52.20160912git.el8.x86_64
  105. Complete!
  106. Removing intermediate container f5d010858c79
  107. ---> 17045c410a3a
  108. Step 7/8 : EXPOSE 80
  109. ---> Running in e0f8613d2f81
  110. Removing intermediate container e0f8613d2f81
  111. ---> 62a690413282
  112. Step 8/8 : CMD /bin/bash
  113. ---> Running in f385e8bf4b9c
  114. Removing intermediate container f385e8bf4b9c
  115. ---> ee45963ddcf4
  116. Successfully built ee45963ddcf4
  117. Successfully tagged mycentos:1.1

3.启动自定义的镜像

  1. [root@test mydocker]# docker run -it mycentos:1.1
  2. [root@b825aedfbad4 local]# //落脚点为dockerfile中编辑的 /usr/local
  3. [root@b825aedfbad4 local]# vim 1.txt //支持 vim编辑
  4. [root@b825aedfbad4 local]# ip addr //支持 net-tools网络模块
  5. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
  6. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  7. inet 127.0.0.1/8 scope host lo
  8. valid_lft forever preferred_lft forever
  9. 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
  10. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  11. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  12. valid_lft forever preferred_lft forever
  13. [root@b825aedfbad4 local]#

4.查看镜像的变更历史

  1. [root@b825aedfbad4 local]# [root@test mydocker]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mycentos 1.1 ee45963ddcf4 5 hours ago 291MB
  4. [root@test mydocker]# docker history ee45963ddcf4
  5. IMAGE CREATED CREATED BY SIZE COMMENT
  6. ee45963ddcf4 5 hours ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
  7. 62a690413282 5 hours ago /bin/sh -c #(nop) EXPOSE 80 0B
  8. 17045c410a3a 5 hours ago /bin/sh -c yum -y install net-tools 23.3MB
  9. f4e0ad28a3c5 5 hours ago /bin/sh -c yum -y install vim 58.1MB
  10. 2cb99337c3af 5 hours ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
  11. 27bda98124bf 5 hours ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B
  12. 5ce63c157e9a 5 hours ago /bin/sh -c #(nop) MAINTAINER zhy<123@qq.com> 0B
  13. 300e315adb2f 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
  14. <missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
  15. <missing> 6 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB