我的docker随笔21:web 服务器部署

本文记录几种不同的web服务器部署,其过程大同小异。

技术总结:
1、选择镜像,建议体积越小越好。确认宿主机目录,将其挂载到容器存放 html 文件的目录。
2、可以将 html 文件拷贝到镜像中重新运行,但此法不太方便。
3、容器内的目录:

  • nginx:/usr/share/nginx/html
  • httpd:/usr/local/apache2/htdocs/
  • tomcat: /usr/local/tomcat/webapps/ROOT
  • php:/var/www/

nginx部署

要点:选择nginx:alpine版本,体积小。

docker-compose 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: "2"
services:
nginx_all:
image: nginx:alpine
container_name: nginx_all
volumes:
- $PWD/nginx:/etc/nginx
ports:
- 8080:80
networks:
- mywebsite
web1:
image: latelee/nginx
container_name: web1
volumes:
- $PWD/html1:/usr/share/nginx/html
ports:
- 8081:80
networks:
- mywebsite

web2:
image: latelee/nginx
container_name: web2
volumes:
- $PWD/html2:/usr/share/nginx/html
ports:
- 8082:80
networks:
- mywebsite
networks:
mywebsite:
driver: bridge

主页示例:

1
2
3
4
5
6
cat XX/index.html
<html>
<body>
<h2>hello world</h2>
</body>
</html>

httpd

httpd实际是apache。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
version: "2"
services:
web1:
image: latelee/httpd
container_name: web1
volumes:
- $PWD/html1:/usr/local/apache2/htdocs/
ports:
- 8081:80
networks:
- mywebsite
web2:
image: latelee/httpd
container_name: web2
volumes:
- $PWD/html2:/usr/local/apache2/htdocs/
ports:
- 8082:80
networks:
- mywebsite
networks:
mywebsite:
driver: bridge

tomcat

1
2
3
4
5
6
7
8
9
10
version: '2'
services:
tomcat:
image: tomcat:8.0.51-jre8-slim
container_name: tomcat
#restart: always
volumes:
- $PWD/webapps:/usr/local/tomcat/webapps/ROOT
ports:
- "8080:8080"
1
2
3
4
5
6
7
8
$ cat webapps/index.php
<html>
<body>
<h2>THis is tomcat test</h2>
<p> PHP </p>
<p> 2018 5 5 </p>
</body>
</html>

php

1
2
3
4
5
6
7
8
9
10
version: "2"
services:
php:
image: php:7.2.7-apache
container_name: php
restart: always
volumes:
- ./www:/var/www/
ports:
- 5000:80