美文网首页
dockerfile构建lnmp环境并部署wordpress

dockerfile构建lnmp环境并部署wordpress

作者: 唯爱熊 | 来源:发表于2020-01-15 16:22 被阅读0次

一.构建基础镜像

先构建一个基础镜像,添加repo的环境和编译的环境,而centos镜像就是初始的官方镜像,后面构建php、nginx、mysql都使用该镜像为base image:

[root@docker01 /data/dockerfile/lnmp]# cat Dockerfile #查看dockerfile文件
FROM centos:centos7.6.1810
RUN yum install wget epel-release -y \
    && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo \
    && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo \
    && yum clean all && yum makecache
RUN yum install -y  wget gcc gcc-c++ glibc make autoconf openssl openssl-devel ntpdata crontabs
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

构建基础镜像

[root@docker01 /data/dockerfile/lnmp]# docker build -t centos:v1 .  #构建基础镜像并打标签

查看构建结果

[root@docker01 /data/dockerfile/lnmp]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE

centos                         v1                  dddddf0d20d8        41 hours ago        880MB

二.构建nginx镜像

查看nginx的目录结构

[root@docker01 /data/dockerfile/nginx]# tree .
.
├── dockerfile
├── fastcgi_params
├── nginx-1.17.7.tar.gz
├── nginx.conf
└── www.conf
0 directories, 5 files

构建nginx的dockfile

#基于之前构建的镜像
FROM centos:v1
#安装工具和依赖包
RUN yum -y install wget gcc gcc-c++ glibc make autoconf openssl openssl-devel libxml2 libxml2-dev libxslt-devel gd-devel GeoIP GeoIP-devel GeoIP-data
#创建用户
RUN useradd -M -s /sbin/nologin www
#拷贝文件并解压
ADD nginx-1.17.7.tar.gz /usr/local/src 
#切换目录
WORKDIR /usr/local/src/nginx-1.17.7
#编译安装
RUN ./configure --user=www --group=www --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
#删除源码包
RUN rm -rf /opt/nginx/nginx-1.17.7.tar.gz
#设置环境变量
ENV PATH=/usr/local/nginx/sbin:$PATH
#复制文件
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
COPY fastcgi_params /usr/local/nginx/conf/fastcgi_params
#创建目录
RUN mkdir /usr/local/nginx/conf/conf.d
#复制文件
COPY www.conf /usr/local/nginx/conf/conf.d/www.conf
#对外暴露的端口
EXPOSE 80
ENTRYPOINT ["nginx"]
CMD ["-g","daemon off;"]

这里nginx采用了编译安装,创建了用户www和安装了nginx的一些依赖包,copy了一些配置文件到镜像中。详细配置文件参考:
nginx.conf

[root@docker01 /data/dockerfile/nginx]# cat nginx.conf

user  www;
worker_processes  auto;

error_log  logs/error.log error;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
    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;
    tcp_nopush     on;
    keepalive_timeout  65;
    send_timeout 60;
    gzip  on;
    include /usr/local/nginx/conf/conf.d/*.conf;
}

fastcgi_params

[root@docker01 /data/dockerfile/nginx]# cat fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

www.conf

[root@docker01 /data/dockerfile/nginx]# cat www.conf
server {
    listen   80;
    server_name _;
    root /usr/local/nginx/html;
    index index.htm index.html index.php;
    location ~ \.php$ {
        root /usr/local/nginx/html;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

可以发现fastcgi_pass php:9000;

这是因为php也是一个容器,和nginx是隔离的,后面启动容器的时候nginx将会通过--link的方式与镜像进行互联访问。
构建nginx镜像

[root@docker01 /data/dockerfile/nginx]# docker build -t nginx:v1 .

查看镜像

[root@docker01 /data/dockerfile/nginx]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
nginx                          v1                  661f93782161        4 hours ago         1.18GB
centos                         v1                  dddddf0d20d8        42 hours ago        880MB

三.构建php镜像

查看PHP的目录结构

[root@docker01 /data/dockerfile/php]# tree .
.
├── dockerfile
├── libmcrypt-2.5.8.tar.gz
├── php-5.6.35.tar.gz
├── php-fpm.conf.default
└── php.ini-production

0 directories, 5 files

查看构建php的dockerfile

[root@docker01 /data/dockerfile/php]# cat dockerfile
FROM centos:v1

ADD libmcrypt-2.5.8.tar.gz /usr/local/src

WORKDIR /usr/local/src/libmcrypt-2.5.8
RUN ./configure && make && make install

ADD php-5.6.35.tar.gz /usr/local/src

RUN yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel

WORKDIR /usr/local/src/php-5.6.35
RUN ./configure --prefix=/usr/local/php --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-mcrypt --with-zlib --with-libxml-dir=/usr --enable-xml  --enable-sockets --enable-fpm --with-config-file-path=/usr/local/php/etc --with-bz2 --with-gd && make && make install
COPY php.ini-production /usr/local/php/etc/php.ini
COPY php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

RUN useradd -M -s /sbin/nologin www
RUN sed -i -e 's@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g' -e 's@nobody@www@g' -e 's@listen = 127.0.0.1:9000@listen = 0.0.0.0:9000@g' /usr/local/php/etc/php-fpm.conf
RUN sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf

EXPOSE 9000

CMD ["/usr/local/php/sbin/php-fpm"]

构建的服务必须运行在前台,而对于nginx来说:daemon off 表示将后台运行关闭了,于是运行在前台,而对于php:sed -i 's@;daemonize = yes@daemonize = no@g' /usr/local/php/etc/php-fpm.conf
这里也是将daemon模式关闭了,于是/usr/local/php/sbin/php-fpm 运行在前台。
php与nginx隔离类似于nginx与PHP安装在不同的服务器一样,需要修改如下配置

vim www.conf
.....
#修改监听地址
listen = 0.0.0.0:9000
#注释如下配置
listen.allowed_clients = 127.0.0.1
.....

构建PHP镜像

[root@docker01 /data/dockerfile/php]# docker build -t php:v1 .

查看镜像

[root@docker01 /data/dockerfile/php]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
nginx                          v1                  661f93782161        5 hours ago         1.18GB
php                            v1                  577689c46c02        21 hours ago        1.64GB
centos                         v1                  dddddf0d20d8        42 hours ago        880MB

四.启动nginx容器和php容器

利用构建的镜像启动php、nginx服务:

[root@docker01 ~]# docker run -d --name php -v /www:/usr/local/nginx/html php:v1  #启动一个php容器并通过-v进行映射
cf77c85e7c2f63a7ac2db2edaa37d22418503e8cfa35d87b9cfad541d03e004a
[root@docker01 ~]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1  #启动一个nginx容器并通过-v进行映射及--link进行关联php容器
3bbc12db8e9cc118a7824c5a314d13c4e37f3a6397f32bbfe939f9e424b306e0

上面启动php的容器时,使用了-v进行映射,如果这里不进行映射,那么php的程序会启动,但是遇到php结尾的文件将不会解析,出现file not found的错误。
查看容器状态

[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
32212cad6eff        nginx:v1            "nginx -g 'daemon of…"   5 hours ago         Up 5 hours          0.0.0.0:80->80/tcp       nginx
5e62acb4ca3d        php:v1              "/usr/local/php/sbin…"   5 hours ago         Up 5 hours          9000/tcp                 php

宿主机上设置测试文件

[root@docker01 /www]# ll
total 10852
-rw-r--r-- 1 1000 1000       24 Jan 14 17:21 index.php
-rw-r--r-- 1 1000 1000       32 Jan 14 16:59 test.html
[root@docker01 /www]# cat index.php 
<?php
    phpinfo();
?>
[root@docker01 /www]# cat test.html 
<h1>Hello Docker nginx-php</h1>

测试访问


进入nginx容器查看/etc/hosts
[root@32212cad6eff nginx-1.17.7]# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3  php 5e62acb4ca3d  
172.17.0.4  32212cad6eff

说明:www.conf配置fastcgi_pass php:9000的原因就在这里。

五.构建mysql镜像

查看mysql目录结构

[root@docker01 /data/dockerfile/mysql]# tree .
.
├── dockerfile
├── MariaDB-10.0.33-centos7-x86_64-client.rpm
├── MariaDB-10.0.33-centos7-x86_64-common.rpm
├── MariaDB-10.0.33-centos7-x86_64-compat.rpm
├── MariaDB-10.0.33-centos7-x86_64-server.rpm
├── ori.sql
├── server.cnf
└── setup.sh
0 directories, 8 files

获取mysql安装包:链接:https://pan.baidu.com/s/16R9m-Xx_-_flYkIfz5Ox1g
提取码:w385
查看构建mysql的dockerfile

[root@docker01 /data/dockerfile/mysql]# cat dockerfile
FROM centos:v1

COPY MariaDB-10.0.33-centos7-x86_64-client.rpm /root/MariaDB-10.0.33-centos7-x86_64-client.rpm
COPY MariaDB-10.0.33-centos7-x86_64-common.rpm /root/MariaDB-10.0.33-centos7-x86_64-common.rpm 
COPY MariaDB-10.0.33-centos7-x86_64-compat.rpm  /root/MariaDB-10.0.33-centos7-x86_64-compat.rpm
COPY MariaDB-10.0.33-centos7-x86_64-server.rpm  /root/MariaDB-10.0.33-centos7-x86_64-server.rpm
WORKDIR /root
RUN yum remove mysql-libs -y
RUN yum -y install *.rpm
ADD ori.sql /root/ori.sql
ADD server.cnf /etc/my.cnf.d/server.cnf
ADD setup.sh /root/setup.sh

RUN yum clean all
RUN chmod +x /root/setup.sh
EXPOSE 3306

CMD ["/bin/bash","/root/setup.sh"]

说明:1.配置文件通过后面启动时挂载进去,(如果后面有更改,只需要在某个目录创建好配置文件,然后进行挂载即可);2.ori.sql是关于执行的sql(赋予root密码,创建数据库等等);3.setup.sh是执行了重新初始mysql(mariadb),最后重启了服务(使用了 --user=mysql,避免出现了使用root用户启动mariadb)

setup.sh脚本原理:Dockerfile用来安装数据库服务,安装完成后,通过setup.sh脚本重新将mysql进行初始化 初始化后,开启MySQL服务执行setup.sh中的sql,关闭mysql数据库 重新以前台的方式启动MySQL数据库服务。
setup.sh

[root@docker01 /data/dockerfile/mysql]# cat setup.sh
#!/bin/sh
useradd -M -s /sbin/nologin mysql
chown -R mysql:mysql /var/lib/mysql

mysql_install_db --user=mysql > /dev/null

mysqld_safe --user=mysql &

sleep 5

mysql < /root/ori.sql

sleep 5

ps -wef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9

mysqld_safe --user=mysql

ori.sql

[root@docker01 /data/dockerfile/mysql]# cat ori.sql
create database wordpress DEFAULT CHARACTER SET utf8;

USE mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

UPDATE user SET password=PASSWORD("root") WHERE user='root';
FLUSH PRIVILEGES;

说明:这里可以根据需求自行修改sql语句,这里是为了后面部署wordpress做创库和授权的操作。
server.conf

[root@docker01 /data/dockerfile/mysql]# cat server.cnf
[mysqld]
bind-address=0.0.0.0
console=1
general_log=1
general_log_file=/dev/stdout
collation-server=utf8_unicode_ci
character-set-server=utf8

构建mysql镜像

[root@docker01 /data/dockerfile/mysql]# docker build -t mysql:v1 .

查看镜像

[root@docker01 /data/dockerfile/mysql]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
nginx                          v1                  661f93782161        5 hours ago         1.18GB
mysql                          v1                  8e69a43533db        6 hours ago         1.6GB
php                            v1                  577689c46c02        22 hours ago        1.64GB
centos                         v1                  dddddf0d20d8        43 hours ago        880MB

六.创建lnmp环境并部署wordpress

1.基于当前镜像构建lnmp

#启动一个mysql容器
[root@docker01 ~]# docker run -d --name mysql -v /root/mysql/server.cnf:/etc/my.cnf.d/server.cnf -v /data/mysql:/var/lib/mysql -p 3306:3306 mysql:v1 

#启动一个PHP容器
[root@docker01 ~]# docker run -d --name php -v /www:/usr/local/nginx/html --link=mysql:mysql php:v1

#启动一个nginx容器
[root@docker01 ~]# docker run -d --name nginx -p 80:80 -v /www:/usr/local/nginx/html --link=php:php nginx:v1

这里阐述下这里启动的这三个容器:(1)先启动一个mysql容器, 并且将上面的server.cnf配置文件和数据目录挂载到了容器内部,这样就防止了数据的丢失;(2)接着启动一个php容器,使用了-v进行映射,如果这里不进行映射,那么php的程序会启动,但是遇到php结尾的文件将不会解析,出现file not found的错误,并且使用了 --link 与mysql 关联,这样php容器就可以连接mysql数据库了;(3)最后启动一个nginx容器,同样使用了-v 进行映射,将网站目录映射到宿主机,通过 --link与php 关联。
查看容器状态

[root@docker01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
32212cad6eff        nginx:v1            "nginx -g 'daemon of…"   5 hours ago         Up 5 hours          0.0.0.0:80->80/tcp       nginx
5e62acb4ca3d        php:v1              "/usr/local/php/sbin…"   5 hours ago         Up 5 hours          9000/tcp                 php
865ae86fc2a5        mysql:v1            "/bin/bash /root/set…"   5 hours ago         Up 5 hours          0.0.0.0:3306->3306/tcp   mysql

2.部署wordpress

宿主机上传源码包并解压

[root@docker01 /www]# ll
total 10852
-rw-r--r-- 1 1000 1000       24 Jan 14 17:21 index.php
-rw-r--r-- 1 1000 1000       32 Jan 14 16:59 test.html
drwxr-xr-x 5 1006 1006     4096 Jan 15 12:00 wordpress
-rw-r--r-- 1 root root 11098483 Jan 15 11:44 wordpress-5.0.3-zh_CN.tar.gz

浏览器访问http://ip:wordpress
web界面设置


省略其它步骤...
设置完成后用户名密码登录

相关文章

网友评论

      本文标题:dockerfile构建lnmp环境并部署wordpress

      本文链接:https://www.haomeiwen.com/subject/grpoactx.html