虚拟化容器,大数据,DBA,中间件,监控。

Docker Dockefile制作nginx php镜像搭建Wordpress网站

21 12月
作者:admin|分类:容器虚拟化

这里的NGINX PHP镜像需要自己构建,mysql 5.7使用官方镜像即可

前端项目镜像构建与部署:Nginx


[root@localhost _data]# cd /nginx/

[root@localhost _data]# cd /nginx/
[root@localhost nginx]# ls
Dockerfile  nginx-1.15.5.tar.gz  nginx.conf  php.conf

#这里nginx.conf需要COPY拷贝到编译好的nginx替换掉其默认配置文件,php.conf文件是为了搭建wordpress博客网站使用的虚拟主机配置文件

Dockerfile

[root@localhost nginx]# cat Dockerfile 
FROM ansible/centos7-ansible
LABEL maintainer Complicated
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*

ADD nginx-1.15.5.tar.gz /
RUN cd /nginx-1.15.5 && \
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    mkdir /usr/local/nginx/conf/vhost && \
    cd / && rm -rf nginx* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]


(1)将依赖包和调试工具全部安装上,清理其环境。清理yum的缓存
(2)软连接一个上海时区到当前时区
(3)这里采用了nginx前台启动的命令,如果直接执行nginx这条命令,那么就是放在后台运行。这里加上了参数-g daemon off表示禁止后台启动。所以前台启动就是hang住容器。这样容器启动就不会被退出。

nginx.conf  

[root@localhost nginx]# cat nginx.conf 
user                 nobody;
worker_processes     4;
worker_rlimit_nofile 65535;

error_log  logs/error.log  notice;

pid        /var/run/nginx.pid;

events {
    use epoll;
    worker_connections  4096;
}

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 off;
    keepalive_timeout  65;

    client_max_body_size         64m;
    include /usr/local/nginx/conf/vhost/*.conf;  #这个配置了一个虚拟主机来进行博客搭建
    
    server {
        listen 80;
        server_name localhost;
        index index.html;

        location / {
            root html;
        }
    }

}

这里就不看虚拟主机的配置信息了,后面可以看到,就直接制作镜像了

[root@localhost nginx]# docker build -t centos7-nginx:v1 .
[root@localhost ~]# docker images
REPOSITORY                    TAG                 IMAGE ID            CREATED             SIZE
centos7-nginx                 v1                  6f88efbb3e42        14 hours ago        634MB

 

PHP项目镜像构建:PHP


#这里就没有像nginx一样需要把自己的配置文件COPY进去,但是还是需要编译和nginx没多大区别
[root@localhost ~]# cd /php/
[root@localhost php]# ls
Dockerfile  php-5.6.36.tar.gz

Dockerfile

[root@localhost php]# cat Dockerfile 
FROM ansible/centos7-ansible
LABEL MAINTAINER Complicated
RUN yum install epel-release -y && \
    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel \
    libmcrypt-devel libxslt-devel libtidy-devel autoconf \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*

ADD php-5.6.36.tar.gz /
RUN cd /php-5.6.36 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --enable-fpm --enable-opcache \
    --with-mysql --with-mysqli --with-pdo-mysql \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-freetype-dir \
    --enable-mbstring --with-mcrypt --enable-hash && \
    make -j 4 && make install && \
    cp php.ini-production /usr/local/php/etc/php.ini && \
    cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
    sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    mkdir /usr/local/php/log && \
    cd / && rm -rf php* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]

(1)可以看到和nginx的Dockerfile大致相同
(2)Make -j 4指定编译时候的线程数,如果是多核CPU可以将其值调大。这样编译速度会更加快一些
(3)这个指令是在前台运行的,就是不加任何参数在前台运行。也是使用CMD去启动就行。在写Dokcefile的时候大部分情况下使用CMD启动就行。
[root@localhost php]# docker build -t centos7-php .

Php里面其实有两个重要的部分,一个是PHP引擎,安装完php之后可以使用php去执行你的脚本。另外一个是Php-fpm,类似于一个动态的服务器。可以解析php相关脚本

 

案例:容器化搭建个人博客系统


1、自定义网络
几个容器都加入到这个网络,加入到这个网络就可以通过容器名字通信
[root@localhost ~]# docker network create lnmp
7cd0585b8fa79c19403e5772b970e99a1d86e13192001e03073fb3052531931c
2、创建Mysql容器
[root@localhost ~]# docker run -d --name lnmp_mysql  --net lnmp --mount src=mysql-vol,dst=/var/lib/mysql  -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 --character-set-server=utf8
f6b15807c039ee940983637f2b5c75565d1d1fb9acc701a1b357848cc91d3677

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
f6b15807c039        mysql:5.7           "docker-entrypoint.s…"   3 seconds ago       Up 2 seconds        3306/tcp, 33060/tcp   lnmp_mysql

3、创建PHP容器 

[root@localhost nginx]# docker run -d --name lnmp_php --net lnmp --mount src=wwwroot,dst=/wwwroot centos7-php:v1
4、创建Nginx容器
这个是虚拟主机配置文件,可以使用-v来进行挂载
[root@localhost nginx]# cat php.conf 
server {
    listen 80;
    server_name example.lnmp.com;
    index index.php index.html;

    access_log logs/www.lmp.com_access.log;
    error_log logs/www.lnmp.com_error.log;

    location / {
        root /wwwroot;
    }

    location ~* \.php$ {
        root /wwwroot;
        fastcgi_pass lnmp_php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

几个需要注意的地方

这个可以通过容器名字访问到
fastcgi_pass lnmp_php:9000;

静态这块处理
location / {
        root /wwwroot;
}

动态这块处理
    location ~* \.php$ {
        root /wwwroot;
        fastcgi_pass lnmp_php:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

创建nginx容器 

[root@localhost ~]# docker run -d --name lnmp_nginx --net lnmp -p 88:80 --mount src=wwwroot,dst=/wwwroot --mount type=bind,src=/nginx/php.conf,dst=/usr/local/nginx/conf/vhost/php.conf centos7-nginx:v1
38ac48bc2d1be9f4b9704f07aebd6ece33a1008c599134a1004fa94d2e052c4c

现在三个容器都创建完成了

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
38ac48bc2d1b        centos7-nginx:v1    "nginx -g 'daemon of…"   17 seconds ago      Up 14 seconds       0.0.0.0:88->80/tcp    lnmp_nginx
bb974424e80f        centos7-php:v1      "php-fpm"                13 hours ago        Up 13 hours         9000/tcp              lnmp_php
f6b15807c039        mysql:5.7           "docker-entrypoint.s…"   14 hours ago        Up 14 hours         3306/tcp, 33060/tcp   lnmp_mysql

登入nginx容器可以看到通过容器名称是可以和对应的容器进行通信 

[root@localhost nginx]# docker exec -it lnmp_nginx /bin/bash

[root@38ac48bc2d1b nginx]# ping lnmp_mysql
PING lnmp_mysql (172.18.0.2) 56(84) bytes of data.
64 bytes from lnmp_mysql.lnmp (172.18.0.2): icmp_seq=1 ttl=64 time=4.85 ms
64 bytes from lnmp_mysql.lnmp (172.18.0.2): icmp_seq=2 ttl=64 time=0.090 ms
^C

[root@38ac48bc2d1b nginx]# ping lnmp_php  
PING lnmp_php (172.18.0.3) 56(84) bytes of data.
64 bytes from lnmp_php.lnmp (172.18.0.3): icmp_seq=1 ttl=64 time=0.180 ms
64 bytes from lnmp_php.lnmp (172.18.0.3): icmp_seq=2 ttl=64 time=0.088 ms
^C

5、部署PHP wordpress网站

 查看之前nginx容器的挂载的卷,这个卷是来存放php代码供nginx访问

[root@localhost ~]# docker volume ls
DRIVER              VOLUME NAME
local               mysql-vol
local               wwwroot


[root@localhost ~]# docker volume inspect wwwroot
 "Mountpoint": "/var/lib/docker/volumes/wwwroot/_data",

来测试一个简单的页面

[root@localhost ~]# vim /var/lib/docker/volumes/wwwroot/_data/test.php
[root@localhost ~]# cat  /var/lib/docker/volumes/wwwroot/_data/test.php
<?php phpinfo();?>

可以看到可以正常访问 

 上传你的wordpress代码进行部署

[root@localhost ~]# cd  /var/lib/docker/volumes/wwwroot/_data/
[root@localhost _data]# rz
z waiting to receive.**B0100000023be50
[root@localhost _data]# ls
test.php  wordpress-4.9.4-zh_CN.tar.gz
[root@localhost _data]# tar xf wordpress-4.9.4-zh_CN.tar.gz 
[root@localhost _data]# mv wordpress/* .
[root@localhost _data]# ls
index.php    test.php                      wp-activate.php     wp-comments-post.php  wp-cron.php        wp-load.php   wp-settings.php   xmlrpc.php
license.txt  wordpress                     wp-admin            wp-config-sample.php  wp-includes        wp-login.php  wp-signup.php
readme.html  wordpress-4.9.4-zh_CN.tar.gz  wp-blog-header.php  wp-content            wp-links-opml.php  wp-mail.php   wp-trackback.php

将里面的内容复制粘贴进去

[root@localhost _data]# vim wp-config.php 

浏览362 评论0
返回
目录
返回
首页
Docker 为你的镜像仓库Harbor部署HTTPS Docker 之间如何共享数据?