1 [root@localhost ~]# mkdir httpd # 首先创建一个apache目录
2 [root@localhost ~]# cd httpd # 进去
3 [root@localhost httpd]# ls # 下载软件包
4 apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz
5 [root@localhost httpd]# mkdir files # 创建一个目录用来放软件包等。
6 [root@localhost httpd]# ls
7 apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz files httpd-2.4.53.tar.gz
8 [root@localhost httpd]# mv apr-* files/ # 把包移到files
9 [root@localhost httpd]# mv httpd-2.4.53.tar.gz files/
10 [root@localhost httpd]# ls
11 files
12 [root@localhost httpd]# cd files/ # 进到files
13 [root@localhost files]# ls
14 apr-1.7.0.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.53.tar.gz
15 [root@localhost files]# cd ..
16 [root@localhost httpd]# vim Dockerfile #写一个Dockerfile,D必须大写
17 FROM centos
18
19 LABEL MANTAINER "benbugaizheyang 122271071@qq.com"
20
21 EXPOSE 80 443
22 ADD files/* /usr/src/
23
24 RUN useradd -r -M -s /sbin/nologin apache && \
25 rm -rf /etc/yum.repos.d/* && \
26 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
27 sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
28 dnf clean all && \
29 dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && \
30 cd /usr/src/apr-1.7.0 && \
31 sed -i '/$RM "$cfgfile"/d' configure && \
32 ./configure --prefix=/usr/local/apr && \
33 make && make install && \
34 cd ../apr-util-1.6.1/ && \
35 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
36 make && make install && \
37 cd ../httpd-2.4.53/ && \
38 ./configure --prefix=/usr/local/apache \
39 --enable-so \
40 --enable-ssl \
41 --enable-cgi \
42 --enable-rewrite \
43 --with-zlib \
44 --with-pcre \
45 --with-apr=/usr/local/apr \
46 --with-apr-util=/usr/local/apr-util/ \
47 --enable-modules=most \
48 --enable-mpms-shared=all \
49 --with-mpm=prefork && \
50 make && make install
51
52 WORKDIR /usr/local/apache
53 CMD ["-D","FOREGROUND"]
54 ENTRYPOINT ["/usr/local/apache/bin/httpd"]
55 [root@localhost httpd]# docker images
56 REPOSITORY TAG IMAGE ID CREATED SIZE
57 httpd v0.1 dee2e907c0ec 3 minutes ago 706MB
58 centos latest 5d0da3dc9764 7 months ago 231MB
59 [root@localhost httpd]# docker run -d --rm --name web httpd:v0.1
60 3bf3d4533e63bc52b184192709891488dd8295d7318032047c3c7df95725cd71
61 [root@localhost httpd]# docker ps
62 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
63 3bf3d4533e63 httpd:v0.1 "/usr/local/apache/b…" 25 seconds ago Up 22 seconds 80/tcp, 443/tcp web
64 0bde7c48bc12 centos "/bin/bash" 3 hours ago Up 3 hours nervous_mclaren
65 [root@localhost httpd]# curl 172.17.0.3
66 <html><body><h1>It works!</h1></body></html>
67
68
69 [root@localhost httpd]# docker build -t httpd:v0.2 .
70 [root@localhost httpd]# vim Dockerfile
71 FROM centos
72
73 LABEL MANTAINER "benbugaizheyang 122271071@qq.com"
74
75 EXPOSE 80 443
76 ADD files/* /usr/src/
77
78 RUN useradd -r -M -s /sbin/nologin apache && \
79 rm -rf /etc/yum.repos.d/* && \
80 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
81 sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
82 dnf clean all && \
83 dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && \
84 cd /usr/src/apr-1.7.0 && \
85 sed -i '/$RM "$cfgfile"/d' configure && \
86 ./configure --prefix=/usr/local/apr && \
87 make && make install && \
88 cd ../apr-util-1.6.1/ && \
89 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
90 make && make install && \
91 cd ../httpd-2.4.53/ && \
92 ./configure --prefix=/usr/local/apache \
93 --enable-so \
94 --enable-ssl \
95 --enable-cgi \
96 --enable-rewrite \
97 --with-zlib \
98 --with-pcre \
99 --with-apr=/usr/local/apr \
100 --with-apr-util=/usr/local/apr-util/ \
101 --enable-modules=most \
102 --enable-mpms-shared=all \
103 --with-mpm=prefork && \
104 make && make install && \ # 缩小镜像大小,制作0.2版本
105 rm -rf /var/log/* /var/cache/* /usr/src/* && \
106 dnf -y remove gcc gcc-c++ make
107
108 WORKDIR /usr/local/apache
109 CMD ["-D","FOREGROUND"]
110 ENTRYPOINT ["/usr/local/apache/bin/httpd"]
111
112
113 FROM centos
114
115 LABEL MANTAINER "benbugaizheyang 122271071@qq.com"
116 ENV VERSION 2.4.53
117 ENV PATH /usr/local/apache/bin:$PATH
118
119 EXPOSE 80 443
120 ADD files/apr-1.7.0.tar.gz /usr/src/
121 ADD files/apr-util-1.6.1.tar.gz /usr/src/
122 ADD https://downloads.apache.org/httpd/httpd-${VERSION}.tar.gz /usr/src/
123
124 RUN useradd -r -M -s /sbin/nologin apache && \
125 rm -rf /etc/yum.repos.d/* && \
126 curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
127 sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
128 dnf clean all && \
129 dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && \
130 cd /usr/src && \
131 tar xf httpd-${VERSION}.tar.gz && \
132 cd /usr/src/apr-1.7.0 && \
133 sed -i '/$RM "$cfgfile"/d' configure && \
134 ./configure --prefix=/usr/local/apr && \
135 make && make install && \
136 cd ../apr-util-1.6.1/ && \
137 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
138 make && make install && \
139 cd ../httpd-${VERSION}/ && \
140 ./configure --prefix=/usr/local/apache \
141 --enable-so \
142 --enable-ssl \
143 --enable-cgi \
144 --enable-rewrite \
145 --with-zlib \
146 --with-pcre \
147 --with-apr=/usr/local/apr \
148 --with-apr-util=/usr/local/apr-util/ \
149 --enable-modules=most \
150 --enable-mpms-shared=all \
151 --with-mpm=prefork && \
152 make && make install && \
153 rm -rf /var/log/* /var/cache/* /usr/src/* && \
154 dnf -y remove gcc gcc-c++ make
155
156 WORKDIR /usr/local/apache
157 CMD ["-D","FOREGROUND"]
158 ENTRYPOINT ["/usr/local/apache/bin/httpd"]
159 [root@localhost httpd]# docker images
160 REPOSITORY TAG IMAGE ID CREATED SIZE
161 httpd v0.2 9537fee87046 18 minutes ago 411MB
162 httpd v0.1 dee2e907c0ec 43 minutes ago 706MB
163 centos latest 5d0da3dc9764 7 months ago 231MB
164
165 [root@localhost httpd]# docker tag httpd:v0.2 benbugaizheyang/myhttpd:v0.1
166 [root@localhost httpd]# docker login
167 Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
168 Username: benbugaizheyang
169 Password:
170 WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
171 Configure a credential helper to remove this warning. See
172 https://docs.docker.com/engine/reference/commandline/login/#credentials-store
173
174 Login Succeeded
175 [root@localhost httpd]# docker tag httpd:v0.2 benbugaizheyang/myhttpd:v0.1
176 [root@localhost httpd]# docker push benbugaizheyang/myhttpd:v0.1 # 上传仓库
177 The push refers to repository [docker.io/benbugaizheyang/myhttpd]
178 8f45e27d60c2: Pushed
179 97950f3afaec: Pushed
180 74ddd0ec08fa: Mounted from library/centos
181 v0.1: digest: sha256:9f95760865334a8fbbcf07ffa32f3520ab60fabba9d331e0c0213c5710c1e6fc size: 953
182 [root@localhost httpd]# docker images
183 REPOSITORY TAG IMAGE ID CREATED SIZE
184 benbugaizheyang/myhttpd v0.1 9537fee87046 30 minutes ago 411MB
185 httpd v0.2 9537fee87046 30 minutes ago 411MB
186 httpd v0.1 dee2e907c0ec 55 minutes ago 706MB
187 centos latest 5d0da3dc9764 7 months ago 231MB # 还是有411MB应为centos镜像就有231MB
188 # 官方的只有50多MB应为它基于alpine系统制作。
189 [root@localhost httpd]# docker pull alpine # 拉alpine镜像
190 [root@localhost httpd]# docker images
191 REPOSITORY TAG IMAGE ID CREATED SIZE
192 benbugaizheyang/myhttpd v0.1 9537fee87046 42 minutes ago 411MB
193 httpd v0.2 9537fee87046 42 minutes ago 411MB
194 httpd v0.1 dee2e907c0ec About an hour ago 706MB
195 alpine latest c059bfaa849c 5 months ago 5.59MB # 5.59MB
196 centos latest 5d0da3dc9764 7 months ago 231MB
197 [root@localhost httpd]# cat Dockerfile > files/install.sh # 把Dockerfile 写到files/install.sh里 相当于复制过来,可以直接改install.sh
198 [root@localhost httpd]# vim files/install.sh # 修改如下
199 sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo && \
200 dnf clean all && \
201 dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make && \
202 cd /usr/src && \
203 tar xf httpd-${VERSION}.tar.gz && \
204 cd /usr/src/apr-1.7.0 && \
205 sed -i '/$RM "$cfgfile"/d' configure && \
206 ./configure --prefix=/usr/local/apr && \
207 make && make install && \
208 cd ../apr-util-1.6.1/ && \
209 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && \
210 make && make install && \
211 cd ../httpd-${VERSION}/ && \
212 ./configure --prefix=/usr/local/apache \
213 --enable-so \
214 --enable-ssl \
215 --enable-cgi \
216 --enable-rewrite \
217 --with-zlib \
218 --with-pcre \
219 --with-apr=/usr/local/apr \
220 --with-apr-util=/usr/local/apr-util/ \
221 --enable-modules=most \
222 --enable-mpms-shared=all \
223 --with-mpm=prefork && \
224 make && make install && \
225 dnf -y remove gcc gcc-c++ make && \
226 rm -rf /var/log/* /var/cache/* /usr/src/* /tmp/*
227
228 [root@localhost httpd]# vim Dockerfile
229 FROM centos
230
231 LABEL MANTAINER "benbugaizheyang 122271071@qq.com"
232 ENV VERSION 2.4.53
233 ENV PATH /usr/local/apache/bin:$PATH
234
235 EXPOSE 80 443
236 ADD files/apr-1.7.0.tar.gz /usr/src/
237 ADD files/apr-util-1.6.1.tar.gz /usr/src/
238 ADD https://downloads.apache.org/httpd/httpd-${VERSION}.tar.gz /usr/src/
239 ADD files/install.sh /tmp/
240
241 RUN /bin/bash /tmp/install.sh
242
243 WORKDIR /usr/local/apache
244 CMD ["-D","FOREGROUND"]
245 ENTRYPOINT ["/usr/local/apache/bin/httpd"]
246
247
248 [root@localhost httpd]# cat Dockerfile
249 FROM centos
250
251 LABEL MANTAINER "benbugaizheyang 122271071@qq.com"
252 ENV VERSION 2.4.53
253 ENV PATH /usr/local/apache/bin:$PATH
254
255 EXPOSE 80 443
256 ADD files/apr-1.7.0.tar.gz /usr/src/
257 ADD files/apr-util-1.6.1.tar.gz /usr/src/
258 ADD https://downloads.apache.org/httpd/httpd-${VERSION}.tar.gz /usr/src/
259 ADD files/install.sh /tmp/
260
261 RUN /bin/bash /tmp/install.sh
262
263 WORKDIR /usr/local/apache
264 CMD ["-D","FOREGROUND"]
265 ENTRYPOINT ["/usr/local/apache/bin/httpd"]
266
267 [root@localhost httpd]# docker build -t myhttpd:latest .
268 [root@localhost httpd]# docker run -d --name myweb myhttpd
269 f631cf981bc232074639a8b9c650734dbea95cf8567522e48b19b03d0fd9ece0
270 [root@localhost httpd]# docker ps
271 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
272 f631cf981bc2 myhttpd "/usr/local/apache/b…" 10 seconds ago Up 9 seconds 80/tcp, 443/tcp myweb
273 3bf3d4533e63 httpd:v0.1 "/usr/local/apache/b…" 2 hours ago Up 2 hours 80/tcp, 443/tcp web
274 [root@localhost httpd]# curl 172.17.0.3
275 <html><body><h1>It works!</h1></body></html>
276 [root@localhost httpd]# docker images
277 REPOSITORY TAG IMAGE ID CREATED SIZE
278 myhttpd latest 625baaedc612 5 minutes ago 379MB
279 httpd v0.2 9537fee87046 About an hour ago 411MB
280 benbugaizheyang/myhttpd v0.1 9537fee87046 About an hour ago 411MB
281 httpd v0.1 dee2e907c0ec 2 hours ago 706MB
282 alpine latest c059bfaa849c 5 months ago 5.59MB
283 centos latest 5d0da3dc9764 7 months ago 231MB
284
285
286
287
288