Docker之使用Docker-compose搭建LNMP环境[转]

Docker之使用Docker-compose搭建LNMP环境[转]

本文介绍Docker-compose搭建LNMP环境(centos7 php7)

文件目录结构如下

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
├── docker-compose.yml
├── mysql
│   ├── conf
│   └── data
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.16.0.tar.gz
│   └── nginx.conf
├── php
│   ├── Dockerfile
│   ├── libzip-1.2.0.tar.gz
│   ├── php-7.3.9.tar.gz
│   └── php.ini
└── wwwroot
    ├── index.html
    └── test.php

   需下载安装包 nginx-1.16.0.tar.gz libzip-1.2.0.tar.gz php-7.3.9.tar.gz

1
2
3
wget http://nginx.org/download/nginx-1.16.1.tar.gz
wget https://libzip.org/download/libzip-1.2.0.tar.gz
wget https://www.php.net/distributions/php-7.3.9.tar.gz

   docker-compose.yml

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
version: ‘3’
services:
  nginx:
    hostname: nginx
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      – “80:80”
    links:
      – php:php-cgi
    volumes:
      – ./wwwroot:/usr/local/nginx/html
 
  php:
    hostname: php
    build: ./php
    links:
      – mysql:mysql-db
    volumes:
      – ./wwwroot:/usr/local/nginx/html
 
  mysql:
    hostname: mysql
    image: mysql:5.7
    ports:
      – “3306:3306”
    volumes:
      – ./mysql/conf:/etc/mysql/conf.d
      – ./mysql/data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: wordpress
      MYSQL_USER: user
      MYSQL_PASSWORD: user123

   PS:links php:php-cgi意思是链接到服务名为php的服务,可以使用host名 php-cgi访问该容器,在启动容器后进入容器ping

进入nginx容器ping测试

 

   同理下面的配置mysql:mysql-db也是一样的道理,在php容器中使用host名mysql-db访问mysql容器

mysql/conf/my.cnf

1
2
3
4
5
6
7
8
9
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysql.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600

   nginx/Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
pid-file=/var/run/mysqld/mysql.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600
root@test-docker1:/data/compose_lnmp# cat nginx/Dockerfile
FROM centos:7
MAINTAINER liuym
RUN yum install -y gcc-c++ zlib-devel pcre-devel make
ADD nginx-1.16.0.tar.gz /tmp
RUN cd /tmp/nginx-1.16.0 && ./configure –prefix=/usr/local/nginx && make -j 2 && make install
RUN rm -f /usr/local/nginx/conf/nginx.conf
COPY nginx.conf /usr/local/nginx/conf
 
EXPOSE 80
CMD [“/usr/local/nginx/sbin/nginx”, “-g”, “daemon off;”]

   nginx/nginx.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
user  root;
worker_processes  auto;
  
error_log  logs/error.log  info;
  
pid        logs/nginx.pid;
  
  
events {
    use epoll;
}
  
http {
  
    include       mime.types;
    default_type  application/octet-stream;
  
    log_format  main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                      ‘$status $body_bytes_sent “$http_referer” ‘
                      ‘”$http_user_agent” “$http_x_forwarded_for”‘;
  
    access_log logs/access.log main;
    sendfile        on;
    keepalive_timeout  65;
  
    server {
        listen 80;
        server_name localhost;
        root html;
        index index.html index.php;
  
        location ~ \.php$ {
            root html;
            fastcgi_pass php-cgi:9000;
            fastcgi_index  index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

   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
27
FROM centos:7
MAINTAINER liuym
RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel make perl
ADD libzip-1.2.0.tar.gz /tmp
RUN cd /tmp/libzip-1.2.0 && \
    ./configure && \
    make && \
    make install
ADD php-7.3.9.tar.gz /tmp
RUN cd /tmp/php-7.3.9 && \
    ./configure –prefix=/usr/local/php \
    –with-config-file-path=/usr/local/php/etc \
    –with-mysql –with-mysqli \
    –with-openssl –with-zlib –with-curl –with-gd \
    –with-jpeg-dir –with-png-dir –with-iconv \
    –enable-fpm –enable-zip –enable-mbstring && \
    cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/ && \
    make -j 4 && make install && \
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf  && \
    sed -i “s/127.0.0.1/0.0.0.0/g” /usr/local/php/etc/php-fpm.d/www.conf && \
    cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
    chmod +x /etc/init.d/php-fpm
#COPY php.ini /usr/local/php/etc
 
EXPOSE 9000
CMD /etc/init.d/php-fpm start  && tail -F /var/log/messages

   PS:1,CentOS7自带libzip版本较低需要下载编译安装

    2,需要把默认配置文件www.conf.default重命名为www.conf并且修改配置把127.0.0.1改成0.0.0.0否则会导致无法访问php页面,因为启动了不同的容器无法通过127.0.0.1访问

                3,配置文件php.ini可以不拷贝

         

wwwroot/test.php

1
<?php phpinfo()?>

   启动

1
docker-compose up -d

   查看

 

   web页面访问

 

摘自:https://www.cnblogs.com/minseo/p/11558020.html

Docker之使用Docker-compose搭建LNMP环境[转]