本文发布于Cylon的收藏册,转载请著名原文链接~

使用docker-compose构建LNMP环境。

编写Dockerfile

这里采用的是先将nginx php打包为rpm包,然后做成镜像。与直接在容器里编译安装同理的。

nginx Dockerfile

1
2
3
4
5
6
7
8
FROM centos
MAINTAINER lc
RUN yum install -y gcc gcc-c++ openssl-devel make pcre-devel
ADD nginx-1.13.9-1.el7.centos.x86_64.rpm /tmp/
RUN cd /tmp/ && rpm -ivh nginx-1.13.9-1.el7.centos.x86_64.rpm
ADD nginx.conf /etc/nginx/
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

php Dockerfile

 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
FROM centos
MAINTAINER LC
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum install zlib-devel \
    libxml2-devel \
    libjpeg-devel \
    libjpeg-turbo-devel \
    freetype-devel \
    libpng-devel gd-devel \
    curl-devel \
    libxslt-devel \
    bzip2-devel \
    gmp-devel \
    readline-devel \
    mcrypt \
    mhash \
    openssl-devel \
    libmcrypt-devel -y
COPY php-7.1.17-1.el7.centos.x86_64.rpm /tmp/
COPY libiconv-1.15-1.el7.centos.x86_64.rpm /tmp/
RUN rpm -ivh /tmp/libiconv-1.15-1.el7.centos.x86_64.rpm
RUN rpm -ivh /tmp/php-7.1.17-1.el7.centos.x86_64.rpm
ADD php-fpm.conf /etc/php/
CMD /usr/sbin/php-fpm && tail -f /dev/null
EXPOSE 9000

准备构建容器的配置文件

在nginx配置文件中增加解析php的语句

1
2
3
4
5
location ~ .*\.(php|php5)$ {
		fastcgi_pass  php.com:9000;$<--这里使用link将php的ip解析过来
		fastcgi_index index.php;
		include fastcgi.conf;
}

修改php-fpm监听端口为外网通讯的ip

1
2
3
$ sed -i "s#listen = 127.0.0.1:900$listen = 0.0.0.0:900$g" php/php-fpm.conf 
$ cat php/php-fpm.conf |grep 9000
listen = 0.0.0.0:9000

注:此步骤可以在打包RPM时,使用%post在安装后进行修改,免去构建镜像的步骤


准备RPM包

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ ls -1 php
libiconv-1.15-1.el7.centos.x86_64.rpm
php-7.1.17-1.el7.centos.x86_64.rpm
phpfile
php-fpm.conf
php.ini

$ ls -1 nginx
nginx-1.13.9-1.el7.centos.x86_64.rpm
nginx.conf
nginxfile

使用docker-compose一键构建镜像

编写docker-compose.yaml

 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
version: "3"
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: nginxfile
    expose:
      - "80"
    ports:
      - "80:80"
    links:
      - mysql
      - php:php.com
    volumes:
      - ./wwwroot:/usr/share/nginx/html/
  php:
    hostname: "php"
    build:
      context: ./php
      dockerfile: phpfile
    ports: 
      - "9000:9000"
    links:
      - mysql:mysql-db
    volumes:
      - ./wwwroot:/usr/share/nginx/html/
  mysql:
    image: mysql:5.6
    hostname: mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "Zhang@123"
      MYSQL_USER: "test"
      MYSQL_PASSWORD: "test@123"

参考文档: https://hub.docker.com/_/mysql/

检查docker-compose-yaml语法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ docker-compose config
services:
  mysql:
    environment:
      MYSQL_PASSWORD: test@123
      MYSQL_ROOT_PASSWORD: Zhang@123
      MYSQL_USER: test
    hostname: mysql
    image: mysql:5.6
    ports:
......

注:在语法正确时,打印docker-compose.yaml内容,语法出错直接报问题所在位置。


一键构建所有镜像

 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
$ docker-compose build
mysql uses an image, skipping

Building php
Step 1/12 : FROM centos
 ---> 5182e96772bf

.....

Step 12/12 : EXPOSE 9000
 ---> Running in 3c5f3c46124c
Removing intermediate container 3c5f3c46124c
 ---> 8791ad17224d
Successfully built 8791ad17224d
Successfully tagged lnmp_php:latest

Building nginx
Step : FROM centos
 ---> 5182e96772bf

.....

Step 8/8 : CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
 ---> Using cache
 ---> 8cca531a5c21
Successfully built 8cca531a5c21
Successfully tagged lnmp_nginx:latest

管理编排容器

1
2
3
docker-compose up -d
docker-compose down
docker-compose ps 

查看运行结果

使用docker-compose一键构建tomcat集群

编写Dockerfile

nginx Dockerfile

1
2
3
4
5
6
7
8
FROM centos
MAINTAINER lc
RUN yum install -y gcc gcc-c++ openssl-devel make pcre-devel
ADD nginx-1.13.9-1.el7.centos.x86_64.rpm /tmp/
RUN cd /tmp/ && rpm -ivh nginx-1.13.9-1.el7.centos.x86_64.rpm
ADD nginx.conf /etc/nginx/
EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

tomcat Dockerfile

1
2
3
4
5
6
7
8
9
FROM centos
MAINTAINER lc
ADD apache-tomcat-8.5.29.tar.gz /usr/share/
ADD jdk-8u161-linux-x64.tar.gz /usr/share/
RUN mv /usr/share/apache-tomcat-8.5.29 /usr/share/tomcat
ENV JAVA_HOME=/usr/share/jdk1.8.0_161
WORKDIR /usr/share/tomcat
ENTRYPOINT ["bin/catalina.sh","run"]
EXPOSE 8080

准备配置文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
upstream tomcat {
		server tomcat01:8080;
		server tomcat02:8080;
}

server {
		listen 81;
		location / {
		     proxy_pass http://tomcat/;
		     proxy_set_header Host $host;
		     client_max_body_size 10m;
		     proxy_set_header X-Real-IP $remote_addr;
		     proxy_set_header REMOTE-HOST $remote_addr;
		     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		 }
}

# 在日志中加入如下配置,来证明访问是负载进行的。

log_format  access  '"$upstream_addr" $remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
										'"$http_user_agent" "$http_x_forwarded_for"';
access_log /data/nginx/log/access.log access;
1
2
mkdir ./webapps/ROOT
echo jsp-test >webapps/ROOT/index.jsp

准备构建容器所需的软件

1
2
3
apache-tomcat-8.5.29.tar.gz
jdk-8u144-linux-x64.tar.gz
tomcatfile

编写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
34
35
36
37
38
39
40
41
42
43
44
45
46
version: "3"
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: nginxfile
    ports:
      - 80:81
    links:
      - tomcat01:tomcat01
      - tomcat02:tomcat02
    volumes:
      - ./webapps:/usr/share/nginx/html
    depends_on:
      - mysql
      - tomcat01
      - tomcat02
  tomcat01:
    hostname: tomcat01
    build:
      context: ./tomcat
      dockerfile: tomcatfile
    links:
      - mysql:mysql-db
    volumes:
      - ./webapps:/usr/share/tomcat/webapps/
  tomcat02:
    hostname: tomcat02
    build: 
      context: ./tomcat
      dockerfile: tomcatfile
    links: 
      - mysql:mysql-db
    volumes:
      - ./webapps:/usr/share/tomcat/webapps/
  mysql:
    hostname: mysql
    image: mysql:5.5
    ports: 
      - 3306:3306
    environment: 
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: 123456

使用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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
$ docker-compose -f docker-compose.yml build 


mysql uses an image, skipping
Building tomcat02
Step : FROM centos
 ---> 5182e96772bf
Step 2/9 : MAINTAINER lc
 ---> Using cache
 ---> 111890e6d42e
Step 3/9 : ADD apache-tomcat-8.5.29.tar.gz /usr/share/
 ---> 46725ed86e2e
Step 4/9 : ADD jdk-8u144-linux-x64.tar.gz /usr/share/
 ---> 431ea9bb9918
Step 5/9 : RUN mv /usr/share/apache-tomcat-8.5.29 /usr/share/tomcat
 ---> Running in 22275e028633
Removing intermediate container 22275e028633
 ---> 0f3919e97b50
Step 6/9 : ENV JAVA_HOME=/usr/share/jdk1.8.0_144
 ---> Running in 60d616e0b9fc
Removing intermediate container 60d616e0b9fc
 ---> 5255488c51ab
Step 7/9 : WORKDIR /usr/share/tomcat
 ---> Running in 6b04d8f5524b
Removing intermediate container 6b04d8f5524b
 ---> db88a81ec00a
Step 8/9 : ENTRYPOINT ["bin/catalina.sh","run"]
 ---> Running in 189b6ee0ff4e
Removing intermediate container 189b6ee0ff4e
 ---> 9b444829ed55
Step 9/9 : EXPOSE 8080
 ---> Running in f2115bf03afb
Removing intermediate container f2115bf03afb
 ---> 31c01ca93305
Successfully built 31c01ca93305
Successfully tagged lnmp_tomcat02:latest
Building tomcat01
Step : FROM centos
 ---> 5182e96772bf
Step 2/9 : MAINTAINER lc
 ---> Using cache
 ---> 111890e6d42e
Step 3/9 : ADD apache-tomcat-8.5.29.tar.gz /usr/share/
 ---> Using cache
 ---> 46725ed86e2e
Step 4/9 : ADD jdk-8u144-linux-x64.tar.gz /usr/share/
 ---> Using cache
 ---> 431ea9bb9918
Step 5/9 : RUN mv /usr/share/apache-tomcat-8.5.29 /usr/share/tomcat
 ---> Using cache
 ---> 0f3919e97b50
Step 6/9 : ENV JAVA_HOME=/usr/share/jdk1.8.0_144
 ---> Using cache
 ---> 5255488c51ab
Step 7/9 : WORKDIR /usr/share/tomcat
 ---> Using cache
 ---> db88a81ec00a
Step 8/9 : ENTRYPOINT ["bin/catalina.sh","run"]
 ---> Using cache
 ---> 9b444829ed55
Step 9/9 : EXPOSE 8080
 ---> Using cache
 ---> 31c01ca93305
Successfully built 31c01ca93305
Successfully tagged lnmp_tomcat01:latest
Building nginx
Step : FROM centos
 ---> 5182e96772bf
Step 2/8 : MAINTAINER lc
 ---> Using cache
 ---> 111890e6d42e
Step 3/8 : RUN yum install -y gcc gcc-c++ openssl-devel make pcre-devel
 ---> Using cache
 ---> 36090d81ef5e
Step 4/8 : ADD nginx-1.13.9-1.el7.centos.x86_64.rpm /tmp/
 ---> Using cache
 ---> cecd606c7619
Step 5/8 : RUN cd /tmp/ && rpm -ivh nginx-1.13.9-1.el7.centos.x86_64.rpm
 ---> Using cache
 ---> 8c7b331fc175
Step 6/8 : ADD nginx.conf /etc/nginx/
 ---> 84940ae84e06
Step 7/8 : EXPOSE 80
 ---> Running in 94ac22711605
Removing intermediate container 94ac22711605
 ---> ef10799c0844
Step 8/8 : CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
 ---> Running in 039ea9de17f0
Removing intermediate container 039ea9de17f0
 ---> d489d87223f8
Successfully built d489d87223f8
Successfully tagged lnmp_nginx:latest

测试访问结果

查看nginx访问日志,发现是负载到每一台tomcat上的。

1
2
"172.24.0.5:8080" 10.0.0.1 - - [12/Aug/2018:17:52:39 +0000] "GET /index.jsp HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.Safari/537.36" "-"
"172.24.0.4:8080" 10.0.0.1 - - [12/Aug/2018:17:52:40 +0000] "GET /index.jsp HTTP/1.1" 200 9 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.Safari/537.36" "-"

本文发布于Cylon的收藏册,转载请著名原文链接~

链接:docker-compose示例

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」 许可协议进行许可。