搭建 Docker+Nginx+Tomcat+PHP+MySQL 运行环境

Docker 近两年简直就是大红大紫,发展非常之快。所以感觉这项技术已经成熟的情况下,决定学习了。

就近期使用的情况来看,docker 确实好用,已经深深喜欢上了它。有了 docker,无论是架构、运维还是开发,都变得比以前方便高效很多。

这篇文章的主题就是用 docker 搭建一个常用的运行或开发环境:Docker+Nginx+Tomcat+PHP+MySQL

测试环境

  • CentOS 7.3
  • Docker 1.12.6

安装配置 Docker

CentOS 7.3 下利用 yum 安装 docker,并加入开机启动

1
2
3
yum -y install docker
systemctl enable docker
systemctl start docker

如果使用 docker 官网仓库,国内会非常的慢,这里推荐两个比较好用的国内镜像:阿里云网易蜂巢

博主用的是阿里云的镜像,速度比较快。使用方法很简单,先注册个账号,进入管理后台,然后里面有详细的教程。它还有个比较好的功能就是,可以在后台创建自己的远程仓库。

把需要用到的镜像拉下来:

1
2
3
4
docker pull nginx
docker pull mysql
docker pull tomcat
docker pull php:fpm

创建相关文件夹

保存下面的 shell 脚本,执行

1
2
3
[root@localhost ~]# vi dmlkinit.sh
[root@localhost ~]# chmod +x dmlkinit.sh
[root@localhost ~]# ./dmlkinit.sh
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh

# dmlkinit.sh
#
# initialize dmlkdata directories
#

# absolute path
dmlkdataroot="/dmlkdata"

dmlkdataapps="apps"
dmlkdatadev="dev"

#
# apps
#

#nginx
nginx[0]="nginx"
nginx[1]="${nginx[0]}/etc/nginx"
nginx[2]="${nginx[0]}/usr/share/nginx/html"
nginx[3]="${nginx[0]}/var/log/nginx/"

#tomcat
tomcat[0]="tomcat"
tomcat[1]="${tomcat[0]}/usr/local/tomcat/webapps"
tomcat[2]="${tomcat[0]}/usr/local/tomcat/logs"

#mysql
mysql[0]="mysql"
mysql[1]="${mysql[0]}/etc/mysql/conf.d"
mysql[2]="${mysql[0]}/var/lib/mysql"

#php
php[0]="php"
php[1]="${php[0]}/var/www/html"

apps=(nginx tomcat mysql php)

for app in ${apps[@]};
do
eval apppaths=(\${${app}[@]})
for apppath in ${apppaths[@]};
do
mkdir -p "${dmlkdataroot}/${dmlkdataapps}/${apppath}"
done
done

mkdir -p "${dmlkdataroot}/${dmlkdatadev}"

可以先启动 Nginx、Tomcat 容器,把里面一些默认配置复制下来,比如 Nginx 的 conf 文件,Tomcat 的 webapps 文件等,再把这些临时用的容器删除了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
docker run -itd --name tmp-nginx nginx
docker run -itd --name tmp-tomcat tomcat
docker run -itd --name tmp-mysql mysql

docker cp tmp-nginx:/usr/share/nginx/html /dmlkdata/apps/nginx/usr/share/nginx
docker cp tmp-nginx:/etc/nginx /dmlkdata/apps/nginx/etc/
docker cp tmp-tomcat:/usr/local/tomcat/webapps /dmlkdata/apps/tomcat/usr/local/tomcat
docker cp tmp-mysql:/etc/mysql/conf.d /dmlkdata/apps/mysql/etc/mysql

docker stop tmp-nginx
docker stop tmp-tomcat
docker stop tmp-mysql

docker rm tmp-nginx
docker rm tmp-tomcat
docker rm tmp-mysql

启动容器

MySQL

1
docker run -itd --name dmlkdockermysql -v /dmlkdata/apps/mysql/etc/mysql/conf.d:/etc/mysql/conf.d -v /dmlkdata/apps/mysql/var/lib/mysql:/var/lib/mysql --privileged -e MYSQL_ROOT_PASSWORD=admin mysql

或者

1
docker run -itd --name dmlkdockermysql -p 3306:3306 -v /dmlkdata/apps/mysql/etc/mysql/conf.d:/etc/mysql/conf.d -v /dmlkdata/apps/mysql/var/lib/mysql:/var/lib/mysql --privileged -e MYSQL_ROOT_PASSWORD=admin mysql

这两者之间的区别就是数据库是否映射宿主机端口,如果映射了后续可以配置外网可以访问到数据库。

Tomcat

1
docker run -itd --name dmlkdockertomcat -v /dmlkdata/apps/tomcat/usr/local/tomcat/webapps:/usr/local/tomcat/webapps -v /dmlkdata/apps/tomcat/usr/local/tomcat/logs:/usr/local/tomcat/logs --privileged --link dmlkdockermysql:dmlkdockermysql tomcat

PHP

1
docker run -itd --name dmlkdockerphp -v /dmlkdata/apps/php/var/www/html:/var/www/html --privileged --link dmlkdockermysql:dmlkdockermysql php:fpm

Nginx

新建配置文件: /dmlkdata/apps/nginx/etc/nginx/conf.d/php.conf

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
34
35
36
37
38
39
40
41
42
43
44
45
server {
listen 80;
server_name php.dmlk.dev;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

root /var/www/html;
index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass dmlkdockerphp:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

/dmlkdata/apps/nginx/etc/nginx/conf.d/tomcat.conf

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
server {
listen 80;
server_name tomcat.dmlk.dev;

#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;

location / {
proxy_pass http://dmlkdockertomcat:8080;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
1
docker run -itd --name dmlkdockernginx -p 80:80 -v /dmlkdata/apps/nginx/etc/nginx:/etc/nginx -v /dmlkdata/apps/nginx/usr/share/nginx/html:/usr/share/nginx/html:ro -v /dmlkdata/apps/nginx/var/log/nginx:/var/log/nginx --link dmlkdockerphp:dmlkdockerphp --link dmlkdockertomcat:dmlkdockertomcat --privileged nginx

开放服务器端口

服务器的端口如果没有对外开放,外网是访问不了的。 这里我选择开放 80 和 3306 端口,分别对应 Nginx 和 MySQL:

1
2
3
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

更改 hosts 文件,方便开发测试用:

1
[root@localhost ~]# ip addr

找到本机服务器 IP 是 192.168.95.128 添加到 C:\Windows\System32\drivers\etc\hosts

1
2
3
192.168.95.128 dmlk.dev
192.168.95.128 php.dmlk.dev
192.168.95.128 tomcat.dmlk.dev

运行结果

  • Docker+Nginx+Tomcat+PHP+MySQL-0
  • Docker+Nginx+Tomcat+PHP+MySQL-1
  • Docker+Nginx+Tomcat+PHP+MySQL-2