1、创建上下文环境并创建首页文件(压缩)

  1. [root@localhost ~]# mkdir nginx && cd nginx
  2. [root@localhost nginx]# echo "hello docker" > index.html

2、编写Dockerfile

  1. [root@localhost nginx]# cat Dockerfile
  2. ARG version=7
  3. FROM centos:${version}
  4. MAINTAINER HWJ
  5. RUN mkdir /hwjvol
  6. VOLUME ["vdata1","hwjvol"]
  7. LABEL version="12" author=hwj description="This is hwj's nginx"
  8. RUN yum install -y wget
  9. RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  10. RUN yum install -y nginx
  11. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  12. ADD index.html /usr/share/nginx/html/index.html
  13. EXPOSE 80 443
  14. CMD ["/usr/sbin/nginx"]

3、构建镜像

  1. [root@localhost nginx]# docker build -t hwj_ng:12 -f /root/nginx/Dockerfile .
  1. Sending build context to Docker daemon 4.096kB
  2. Step 1/13 : ARG version=7
  3. Step 2/13 : FROM centos:${version}
  4. ---> eeb6ee3f44bd
  5. Step 3/13 : MAINTAINER HWJ
  6. ---> Using cache
  7. ---> 2496609ee6e4
  8. Step 4/13 : RUN mkdir /hwjvol
  9. ---> Using cache
  10. ---> b22884341e05
  11. Step 5/13 : VOLUME ["vdata1","hwjvol"]
  12. ---> Using cache
  13. ---> e3f891783a32
  14. Step 6/13 : LABEL version="12" author=hwj description="This is hwj's nginx"
  15. ---> Using cache
  16. ---> af80bf31b198
  17. Step 7/13 : RUN yum install -y wget
  18. ---> Running in f0c28b144d86
  19. Loaded plugins: fastestmirror, ovl
  20. Determining fastest mirrors
  21. * base: mirrors.aliyun.com
  22. * extras: mirrors.ustc.edu.cn
  23. * updates: mirrors.ustc.edu.cn
  24. Resolving Dependencies
  25. --> Running transaction check
  26. ---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
  27. --> Finished Dependency Resolution
  28. Dependencies Resolved
  29. ================================================================================
  30. Package Arch Version Repository Size
  31. ================================================================================
  32. Installing:
  33. wget x86_64 1.14-18.el7_6.1 base 547 k
  34. Transaction Summary
  35. ================================================================================
  36. Install 1 Package
  37. Total download size: 547 k
  38. Installed size: 2.0 M
  39. Downloading packages:
  40. warning: /var/cache/yum/x86_64/7/base/packages/wget-1.14-18.el7_6.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
  41. Public key for wget-1.14-18.el7_6.1.x86_64.rpm is not installed
  42. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  43. Importing GPG key 0xF4A80EB5:
  44. Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
  45. Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
  46. Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
  47. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  48. Running transaction check
  49. Running transaction test
  50. Transaction test succeeded
  51. Running transaction
  52. Installing : wget-1.14-18.el7_6.1.x86_64 1/1
  53. install-info: No such file or directory for /usr/share/info/wget.info.gz
  54. Verifying : wget-1.14-18.el7_6.1.x86_64 1/1
  55. Installed:
  56. wget.x86_64 0:1.14-18.el7_6.1
  57. Complete!
  58. Removing intermediate container f0c28b144d86
  59. ---> 5fe03776d9ba
  60. Step 8/13 : RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  61. ---> Running in fcca48467ba2
  62. --2022-03-24 03:01:35-- http://mirrors.aliyun.com/repo/epel-7.repo
  63. Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 183.216.185.208, 183.216.185.215, 117.167.122.240, ...
  64. Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|183.216.185.208|:80... connected.
  65. HTTP request sent, awaiting response... 200 OK
  66. Length: 664 [application/octet-stream]
  67. Saving to: '/etc/yum.repos.d/epel.repo'
  68. 0K 100% 137M=0s
  69. 2022-03-24 03:01:36 (137 MB/s) - '/etc/yum.repos.d/epel.repo' saved [664/664]
  70. Removing intermediate container fcca48467ba2
  71. ---> 3c9ec882ee26
  72. Step 9/13 : RUN yum install -y nginx
  73. ---> Running in dfaad2d1147b
  74. Loaded plugins: fastestmirror, ovl
  75. Loading mirror speeds from cached hostfile
  76. * base: mirrors.aliyun.com
  77. * extras: mirrors.ustc.edu.cn
  78. * updates: mirrors.ustc.edu.cn
  79. Resolving Dependencies
  80. --> Running transaction check
  81. ---> Package nginx.x86_64 1:1.20.1-9.el7 will be installed
  82. --> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64
  83. --> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  84. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  85. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  86. --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-9.el7.x86_64
  87. --> Processing Dependency: openssl for package: 1:nginx-1.20.1-9.el7.x86_64
  88. --> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-9.el7.x86_64
  89. --> Processing Dependency: system-logos for package: 1:nginx-1.20.1-9.el7.x86_64
  90. --> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  91. --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  92. --> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  93. --> Running transaction check
  94. ---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
  95. ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
  96. ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
  97. ---> Package nginx-filesystem.noarch 1:1.20.1-9.el7 will be installed
  98. ---> Package openssl.x86_64 1:1.0.2k-24.el7_9 will be installed
  99. --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-24.el7_9 for package: 1:openssl-1.0.2k-24.el7_9.x86_64
  100. --> Processing Dependency: make for package: 1:openssl-1.0.2k-24.el7_9.x86_64
  101. ---> Package openssl11-libs.x86_64 1:1.1.1k-2.el7 will be installed
  102. --> Running transaction check
  103. ---> Package make.x86_64 1:3.82-24.el7 will be installed
  104. ---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
  105. ---> Package openssl-libs.x86_64 1:1.0.2k-24.el7_9 will be an update
  106. --> Finished Dependency Resolution
  107. Dependencies Resolved
  108. ================================================================================
  109. Package Arch Version Repository Size
  110. ================================================================================
  111. Installing:
  112. nginx x86_64 1:1.20.1-9.el7 epel 587 k
  113. Installing for dependencies:
  114. centos-indexhtml noarch 7-9.el7.centos base 92 k
  115. centos-logos noarch 70.0.6-3.el7.centos base 21 M
  116. gperftools-libs x86_64 2.6.1-1.el7 base 272 k
  117. make x86_64 1:3.82-24.el7 base 421 k
  118. nginx-filesystem noarch 1:1.20.1-9.el7 epel 24 k
  119. openssl x86_64 1:1.0.2k-24.el7_9 updates 494 k
  120. openssl11-libs x86_64 1:1.1.1k-2.el7 epel 1.5 M
  121. Updating for dependencies:
  122. openssl-libs x86_64 1:1.0.2k-24.el7_9 updates 1.2 M
  123. Transaction Summary
  124. ================================================================================
  125. Install 1 Package (+7 Dependent packages)
  126. Upgrade ( 1 Dependent package)
  127. Total download size: 26 M
  128. Downloading packages:
  129. Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
  130. --------------------------------------------------------------------------------
  131. Total 632 kB/s | 26 MB 00:41
  132. Running transaction check
  133. Running transaction test
  134. Transaction test succeeded
  135. Running transaction
  136. Installing : centos-logos-70.0.6-3.el7.centos.noarch 1/10
  137. Installing : centos-indexhtml-7-9.el7.centos.noarch 2/10
  138. Installing : 1:make-3.82-24.el7.x86_64 3/10
  139. Installing : gperftools-libs-2.6.1-1.el7.x86_64 4/10
  140. Installing : 1:openssl11-libs-1.1.1k-2.el7.x86_64 5/10
  141. Updating : 1:openssl-libs-1.0.2k-24.el7_9.x86_64 6/10
  142. Installing : 1:openssl-1.0.2k-24.el7_9.x86_64 7/10
  143. Installing : 1:nginx-filesystem-1.20.1-9.el7.noarch 8/10
  144. Installing : 1:nginx-1.20.1-9.el7.x86_64 9/10
  145. Cleanup : 1:openssl-libs-1.0.2k-19.el7.x86_64 10/10
  146. Verifying : 1:nginx-filesystem-1.20.1-9.el7.noarch 1/10
  147. Verifying : 1:nginx-1.20.1-9.el7.x86_64 2/10
  148. Verifying : 1:openssl-libs-1.0.2k-24.el7_9.x86_64 3/10
  149. Verifying : 1:openssl11-libs-1.1.1k-2.el7.x86_64 4/10
  150. Verifying : gperftools-libs-2.6.1-1.el7.x86_64 5/10
  151. Verifying : 1:make-3.82-24.el7.x86_64 6/10
  152. Verifying : 1:openssl-1.0.2k-24.el7_9.x86_64 7/10
  153. Verifying : centos-indexhtml-7-9.el7.centos.noarch 8/10
  154. Verifying : centos-logos-70.0.6-3.el7.centos.noarch 9/10
  155. Verifying : 1:openssl-libs-1.0.2k-19.el7.x86_64 10/10
  156. Installed:
  157. nginx.x86_64 1:1.20.1-9.el7
  158. Dependency Installed:
  159. centos-indexhtml.noarch 0:7-9.el7.centos
  160. centos-logos.noarch 0:70.0.6-3.el7.centos
  161. gperftools-libs.x86_64 0:2.6.1-1.el7
  162. make.x86_64 1:3.82-24.el7
  163. nginx-filesystem.noarch 1:1.20.1-9.el7
  164. openssl.x86_64 1:1.0.2k-24.el7_9
  165. openssl11-libs.x86_64 1:1.1.1k-2.el7
  166. Dependency Updated:
  167. openssl-libs.x86_64 1:1.0.2k-24.el7_9
  168. Complete!
  169. Removing intermediate container dfaad2d1147b
  170. ---> 3156146d32ad
  171. Step 10/13 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  172. ---> Running in 806bd0eba0a9
  173. Removing intermediate container 806bd0eba0a9
  174. ---> 538ab0005d67
  175. Step 11/13 : ADD index.html.tar.gz /usr/share/nginx/httml/index.html
  176. ---> 96e50ee998f1
  177. Step 12/13 : EXPOSE 80 443
  178. ---> Running in 5e305708d2e6
  179. Removing intermediate container 5e305708d2e6
  180. ---> 5f40f9fd1057
  181. Step 13/13 : CMD ["/usr/sbin/nginx"]
  182. ---> Running in ac5bc5c3a67f
  183. Removing intermediate container ac5bc5c3a67f
  184. ---> 103fc4f15fa0
  185. Successfully built 103fc4f15fa0
  186. Successfully tagged hwj_ng:12

4、运行镜像

  1. docker run -d -p 8080:80 --name hwj12 103
  2. 1c9f69945232fc8bdffbecbd95d7d74fda6dd2bc5091d55192d3b85ce105bcd0
  3. [root@localhost nginx]# curl localhost:8080
  4. hello docker

image.png