Docker-Compose构建lnmp

目录

    • 实验前准备
    • 安装compose
    • Nginx
      • 准备工作目录
      • 准备Dockerfile脚本
      • 准备nginx.conf
    • Mysql
      • 准备工作目录
      • 编写Dockerfile脚本
      • 准备my.cnf
    • PHP
      • 准备工作目录
      • 准备相关文件
    • 编写docker-compose.yml配置文件
    • 目录结构
    • 启动
    • 测试
      • Mysql授权
      • 测试
    • 问题
      • Mysql容器无权访问问题
      • 浏览器访问file not found问题

实验前准备

宿主机:192.168.188.17,安装docker、docker-compose
准备以下压缩包和配置文件nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz、nginx.conf、mysql-boost-5.7.20.tar.gz、my.cnf、php-7.1.10.tar.bz2、php-fpm.conf、php.ini、www.conf

安装compose

curl -L https://github.com/docker/compose/releases/download/1.21.1/docker-compose-uname -s-uname -m -o /usr/local/bin/docker-compose
这个下得很慢,自己想办法搞一份也行,把它放到/usr/local/bin/目录下
cp /opt/docker-compose /usr/local/bin/
chmod +x /usr/local/bin/docker-compose
可以看一下版本检查能不能用
docker-compose --version
在这里插入图片描述

Nginx

准备工作目录

mkdir -p /opt/compose_lnmp/compose_nginx/nginx
cp /opt/nginx-1.12.0.tar.gz /opt/compose_lnmp/compose_nginx/nginx/
cp /opt/wordpress-4.9.4-zh_CN.tar.gz /opt/compose_lnmp/compose_nginx/nginx/

准备Dockerfile脚本

vim /opt/compose_lnmp/compose_nginx/nginx/Dockerfile

FROM centos:7
MAINTAINER this is nginx image 
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

Dockerfile的解释如下

#基础镜像
FROM centos:7
#维护者信息
MAINTAINER this is nginx image 
#安装依赖包,并且添加nginx用户
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
#将Dockerfile所在目录下的nginx-1.12.0.tar.gz压缩包添加到容器中/usr/local/src/的目录下(会自动解压)
ADD nginx-1.12.0.tar.gz /usr/local/src/
#进入容器的/usr/local/src/nginx-1.12.0目录
WORKDIR /usr/local/src/nginx-1.12.0
#编译安装
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 4 && make install
#添加环境变量
ENV PATH /usr/local/nginx/sbin:$PATH
#将Dockerfile所在目录下的nginx.conf配置文件添加到容器中/usr/local/nginx/conf/的目录下
ADD nginx.conf /usr/local/nginx/conf/
#将Dockerfile所在目录下的wordpress-4.9.4-zh_CN.tar.gz添加到容器中/usr/local/nginx/html/的目录下(自动解压)
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
#为/usr/local/nginx/html/赋权
RUN chmod 777 -R /usr/local/nginx/html/
#映射的容器端口
EXPOSE 80
#允许被挂载的目录
VOLUME [ "/usr/local/nginx/html/" ]
#开启容器后执行的命令
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

准备nginx.conf

搞一份nginx.conf配置文件
cp /opt/nginx.conf /opt/compose_lnmp/compose_nginx/nginx/
主要是增加一个动态页面的location块,这里的IP要写PHP服务的容器ip
Docker-Compose构建lnmp_第1张图片
主要配置代码以及少量解释

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   html;
            #这里要写index.php
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
	    #这个ip和端口就是php容器的ip和服务端口
            fastcgi_pass   172.20.0.30:9000;
            fastcgi_index  index.php;
	    #$fastcgi_script_name类似于外部变量,你在浏览器访问时输的uri是什么这里就是什么
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

Mysql

准备工作目录

mkdir -p /opt/compose_lnmp/compose_mysql/mysql
cp mysql-boost-5.7.20.tar.gz /opt/compose_lnmp/compose_mysql/mysql/

编写Dockerfile脚本

vim /opt/compose_lnmp/compose_mysql/mysql/Dockerfile

FROM centos:7
MAINTAINER this is mysql images 
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1;make -j4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD [ "/usr/sbin/init" ]

Dockerfile解释

#基础镜像
FROM centos:7
#维护者信息
MAINTAINER this is mysql images 
#安装依赖包,添加mysql用户
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
#向容器的/usr/local/src/目录添加mysql-boost-5.7.20.tar.gz压缩包(自动解压)
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
#进入这个目录
WORKDIR /usr/local/src/mysql-5.7.20/
#编译安装
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1;make -j4;make install
#向容器的/etc/my.cnf目录添加my.cnf
ADD my.cnf /etc/my.cnf
#映射端口
EXPOSE 3306
#改变所属用户
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
#进入这个目录
WORKDIR /usr/local/mysql/bin/
#初始化mysql数据库
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
#添加环境变量
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#允许挂载的目录
VOLUME [ "/usr/local/mysql" ]
#容器启动后执行
CMD [ "/usr/sbin/init" ]

准备my.cnf

搞一份my.cnf配置文件
cp /opt/my.cnf /opt/compose_lnmp/compose_mysql/mysql/
my.cnf里设置大概如下

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

PHP

准备工作目录

mkdir -p /opt/compose_lnmp/compose_php/php
cp /opt/php-7.1.10.tar.bz2 /opt/compose_lnmp/compose_php/php/
编写Dockerfile脚本
vim /opt/compose_lnmp/compose_php/php/Dockerfile

FROM centos:7
MAINTAINER this is php image 
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip ; make -j 4 ; make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
CMD /usr/local/php/sbin/php-fpm -F

Dockerfile解释

#基础镜像
FROM centos:7
#维护者信息
MAINTAINER this is php image 
#安装依赖包
RUN yum install -y gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel;useradd -M -s /sbin/nologin nginx
#向容器的/usr/local/src/目录添加php-7.1.10.tar.bz2 压缩包(自动解压)
ADD php-7.1.10.tar.bz2 /usr/local/src/
#进入这个目录
WORKDIR /usr/local/src/php-7.1.10
#编译安装
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip ; make -j 4 ; make install
#添加环境变量
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
#向容器的/usr/local/php/lib/目录添加php.ini
ADD php.ini /usr/local/php/lib/
#向容器的/usr/local/php/etc/目录添加php-fpm.conf
ADD php-fpm.conf /usr/local/php/etc/
##向容器的/usr/local/php/etc/php-fpm.d/目录添加www.conf
ADD www.conf /usr/local/php/etc/php-fpm.d/
#映射端口
EXPOSE 9000
#容器启动后执行的命令
CMD /usr/local/php/sbin/php-fpm -F

准备相关文件

cp /opt/php.ini /opt/compose_lnmp/compose_php/php/
vim /opt/compose_lnmp/compose_php/php/php.ini
进行如下修改
取消注释,修改时区
在这里插入图片描述
修改套接字路径
在这里插入图片描述
cp /opt/php-fpm.conf /opt/compose_lnmp/compose_php/php/
vim /opt/compose_lnmp/compose_php/php/php-fpm.conf
php-fpm.conf需要进行如下修改
取消注释
在这里插入图片描述
cp /opt/www.conf /opt/compose_lnmp/compose_php/php/
vim /opt/compose_lnmp/compose_php/php/www.conf
www.conf需要进行如下修改
修改用户和组
在这里插入图片描述
修改监听ip和端口
在这里插入图片描述

编写docker-compose.yml配置文件

这里坑很多,如果要自己修改,建议看懂了再改

version: '3'
services:
 nginx:
  container_name: nginx
  hostname: nginx
  build:
   context: /opt/compose_lnmp/compose_nginx/nginx
   dockerfile: Dockerfile
  ports:
   - 1216:80
   - 1217:443
  privileged: true
  networks:
   lnmp:
    ipv4_address: 172.20.0.10
  volumes:
   - nginx-data:/usr/local/nginx/html/wordpress
 mysql:
  container_name: mysql
  hostname: mysql
  build:
   context: /opt/compose_lnmp/compose_mysql/mysql
   dockerfile: Dockerfile
  ports:
   - 1226:3306
  privileged: true
  networks:
   lnmp:
    ipv4_address: 172.20.0.20
 php:
  container_name: php
  hostname: php
  build:
   context: /opt/compose_lnmp/compose_php/php
   dockerfile: Dockerfile
  ports:
   - 1236:9000
  privileged: true
  networks:
   lnmp:
    ipv4_address: 172.20.0.30
  volumes:
   - nginx-data:/usr/local/nginx/html/wordpress
   - mysql-data:/usr/local/mysql/
volumes:
 nginx-data:
 mysql-data:
networks:
 lnmp:
  driver: bridge
  ipam:
   config:
    - subnet: 172.20.0.0/16

解释如下

#语法版本,这个版本不一样可能会有一定影响
version: '3'
services:
#nginx的配置
 nginx:
#容器名
  container_name: nginx
#主机名
  hostname: nginx
#用来指定dockerfile的模块
  build:
#dockerfile的路径
   context: /opt/compose_lnmp/compose_nginx/nginx
#dockerfile的名字
   dockerfile: Dockerfile
#端口映射,前面是宿主机的端口,后面是容器端口
  ports:
   - 1216:80
   - 1217:443
#给容器root权限
  privileged: true
#网络相关
  networks:
   lnmp:
    ipv4_address: 172.20.0.10
#将nginx-data挂载到nginx容器的/usr/local/nginx/html/wordpress
  volumes:
   - nginx-data:/usr/local/nginx/html/wordpress
#mysql配置
 mysql:
  container_name: mysql
  hostname: mysql
  build:
   context: /opt/compose_lnmp/compose_mysql/mysql
   dockerfile: Dockerfile
  ports:
   - 1226:3306
  privileged: true
  networks:
   lnmp:
    ipv4_address: 172.20.0.20
#php的配置
 php:
  container_name: php
  hostname: php
  build:
   context: /opt/compose_lnmp/compose_php/php
   dockerfile: Dockerfile
  ports:
   - 1236:9000
  privileged: true
  networks:
   lnmp:
    ipv4_address: 172.20.0.30
#将nginx-data挂载到/usr/local/nginx/html/wordpress,将mysql-data挂载到/usr/local/mysql/
  volumes:
   - nginx-data:/usr/local/nginx/html/wordpress
   - mysql-data:/usr/local/mysql/
#创建两个卷用于挂载
volumes:
 nginx-data:
 mysql-data:
#自定义网络
networks:
 lnmp:
  driver: bridge
  ipam:
   config:
- subnet: 172.20.0.0/16

目录结构

Docker-Compose构建lnmp_第2张图片

启动

cd /opt/compose_lnmp/
docker-compose -f docker-compose.yml up -d
选项解释
-f, --file FILE :使用特定的 compose 模板文件,默认为 docker-compose.yml
-p, --project-name NAME :指定项目名称,默认使用目录名称
-d :在后台运行
在一段漫长的等待之后
在这里插入图片描述
看一下容器有没有都启动
在这里插入图片描述

测试

Mysql授权

进入mysql容器
docker exec -it web02 bash
mysql -u root -p

create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;

测试

浏览器访问http://192.168.188.17:1216/wordpress/index.php
Docker-Compose构建lnmp_第3张图片

问题

在实验过程中有两个问题纠缠了我很久

Mysql容器无权访问问题

具体报错:Can t connect to local MySQL server through socket ‘/usr/local/mysql/mysql.soc’
报错分析:字面上看就是单纯的没找到.sock文件,根据网上的方法进行排查,发现/etc/my.cnf中写的是对的,但是.sock文件就是没生成,当我重启mysqld服务时发现居然没有权限,我才意识到可能是容器没有root权限
解决方法:在vim docker-compose.yml中为mysql容器添加了权限,具体如下:
Docker-Compose构建lnmp_第4张图片
重新生成并测试,解决问题

浏览器访问file not found问题

具体问题:当浏览器输入http://192.168.188.17:1216/wordpress/index.php时出现File not found报错,nginx容器中的nginx/logs/error.log日志报错为:FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 192.168.188.1, server: localhost, request: “GET /wordpress/index.php HTTP/1.1”, upstream: “fastcgi://172.20.0.30:9000”, host: “192.168.188.17:1216”
报错分析:浏览器访问静态页面时正常访问,所以排除nginx服务状态问题,nginx.conf的location规则也多次检查没有问题,那么问题大概率就是出在php容器里,经过排查我发现问题可能是因为php容器没有共享到nginx容器中的nginx/html/目录导致的
解决方法:最简单的方法当然是直接在vim docker-compose.yml中用volumes_from挂载到nginx容器,但是这个方法好像只支持版本2,那么其他版本该怎么解决。其实只要将宿主机、nginx、php三者相互挂载就能用volumes实现volumes_from的功能,具体实现如下:
Docker-Compose构建lnmp_第5张图片
Docker-Compose构建lnmp_第6张图片
在这里插入图片描述
重新生成并测试,解决问题

你可能感兴趣的:(云Linux,docker,容器,运维)