docker多容器部署lnmp环境

为什么80%的码农都做不了架构师?>>>   hot3.png

环境:RHEL7.5  ip:192.168.10.102,主机名:lb02

最终的目录文件结构:

[root@lb02 ~]# tree mariadb/
mariadb/
├── Dockerfile
└── start.sh

0 directories, 2 files
[root@lb02 ~]# tree nginx
nginx
├── Dockerfile
├── nginx.conf
└── server.conf

0 directories, 3 files
[root@lb02 ~]# tree php7/
php7/
└── Dockerfile

0 directories, 1 file
[root@lb02 ~]# 

一、创建web、数据库目录

web网站目录为:/wwwroot,属主属组:www,用户和组的id均为:1080

[root@lb02 ~]# mkdir /wwwroot
[root@lb02 ~]# groupadd -g 1080  www
[root@lb02 ~]# useradd -g 1080 -u 1080 -s /sbin/nologin www
[root@lb02 ~]# chown -R www.www /wwwroot/
[root@lb02 ~]#

下载wordpress并解压到/wwwroot目录中

[root@lb02 ~]# curl -O https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@lb02 ~]# tar xf wordpress-4.9.4-zh_CN.tar.gz -C /wwwroot/
[root@lb02 ~]# mv /wwwroot/wordpress/* /wwwroot/
[root@lb02 ~]# chown -R www.www /wwwroot/*
[root@lb02 ~]# 

数据库数据目录:/mariadb/3306/data,/mariadb/3307/data,属主属组:mysql,用户和组id均为:3306

[root@lb02 ~]# groupadd -g 3306  mysql
[root@lb02 ~]# useradd -g 3306 -u 3306 -s /sbin/nologin mysql
[root@lb02 ~]# mkdir /mariadb/{3306,3307}/data -p
[root@lb02 ~]# chown -R mysql.mysql /mariadb/
[root@lb02 ~]#

安装mariadb客户端命令工具

[root@lb02 ~]# yum install mariadb -y

二、安装docker

1、下载docker的yum源文件

这里使用阿里云镜像站yum源。

repo文件地址地址:https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

[root@lb02 ~]# curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@lb02 ~]# yum clean all && yum makecache

2、安装docker-ce

[root@lb02 ~]# yum install docker-ce -y

3、设置加速器

创建/etc/docker/daemon.json文件,内容如下:

[root@lb02 ~]# mkdir /etc/docker
[root@lb02 ~]# vim /etc/docker/daemon.json
{

 "registry-mirrors": ["https://registry.docker-cn.com","https://dhq9bx4f.mirror.aliyuncs.com"]

}

4、启动docker

[root@lb02 ~]# systemctl start docker

三、nginx、mariadb、php镜像

nginx、mariadb、php各自的Dockerfile放到各自的目录

[root@lb02 ~]# mkdir nginx
[root@lb02 ~]# mkdir mariadb
[root@lb02 ~]# mkdir php7
[root@lb02 ~]#

(1)Dockerfile

1、nginx主配置文件及其虚拟主机配置文件

nginx使用1.14.0版本。使用编译安装nginx

nginx主配置文件内容如下:

[root@lb02 ~]# vim nginx/nginx.conf
user  www www;
worker_processes  1;
worker_rlimit_nofile 65535;
error_log  /var/log/nginx/error.log notice;
events {
    use epoll;
    worker_connections  65535;
}
http {
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    

    include /usr/local/nginx/conf.d/*.conf;

}

虚拟主机配置文件:server.conf

[root@lb02 ~]# vim nginx/server.conf
server {
listen       80;
server_name  localhost;
location / {
    root   /usr/local/nginx/html;
    index  index.php index.html index.htm;
}
location ~ \.php$ {
    root           /usr/local/nginx/html;
    fastcgi_pass   php7:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   /usr/local/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}
}

2、创建nginx镜像的Dockerfile

[root@lb02 ~]# cat nginx/Dockerfile
###  Set the base image to CentOS
FROM centos

#File Author / Maintainer
MAINTAINER caomuzhong www.logmm.com

#Install necessary tools
RUN yum install -y gcc gcc-c++ pcre-devel openssl-devel libxml2-devel openssl libcurl-devel make zlib zlib-devel gd-devel

#Install Nginx
RUN groupadd -g 1080 www && useradd  -g 1080 -u 1080 -s /sbin/nologin www \
   &&  mkdir -p /usr/local/nginx/ \
   &&  mkdir -p /var/log/nginx  \
   &&  chown www.www /var/log/nginx \
   &&  touch /var/log/nginx/error.log \
   &&  chown www.www /var/log/nginx/error.log
ADD http://nginx.org/download/nginx-1.14.0.tar.gz .
RUN tar xzvf nginx-1.14.0.tar.gz \
   &&  cd nginx-1.14.0 && ./configure --prefix=/usr/local/nginx \
       --user=www \
       --group=www \
       --http-log-path=/mydata/logs/nginx/access.log \
       --error-log-path=/mydata/logs/nginx/error.log \
       --with-http_ssl_module \
       --with-http_realip_module \
       --with-http_flv_module \
       --with-http_mp4_module \
       --with-http_gunzip_module \
       --with-http_gzip_static_module \
       --with-http_image_filter_module \
       --with-http_stub_status_module &&  make && make install

RUN rm -f /usr/local/nginx/conf/nginx.conf && mkdir /usr/local/nginx/conf.d/
COPY nginx/nginx.conf  /usr/local/nginx/conf/nginx.conf
COPY nginx/server.conf /usr/local/nginx/conf.d/

#Expose ports
EXPOSE 80 443

#Front desk start nginx
ENTRYPOINT ["/usr/local/nginx/sbin/nginx","-g","daemon off;"] 

3、php镜像Dockerfile

这里使用php7.2.7版本,编译安装。注意,创建php镜像时候要创建一个跟nginx root目录同名的目录。

[root@lb02 ~]# vim php7/Dockerfile
###  Set the base image to CentOS
FROM centos

#File Author / Maintainer
MAINTAINER caomuzhong www.logmm.com

#Install necessary tools
RUN yum install -y bzip2-devel openssl-devel gnutls-devel gcc gcc-c++ cmake ncurses-devel bison-devel libaio-devel openldap  openldap-devel autoconf bison libxml2-devel libcurl-devel libevent libevent-devel gd-devel  expat-devel

ADD http://iweb.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz .
RUN tar xf libmcrypt-2.5.8.tar.gz \
    && cd libmcrypt-2.5.8 && ./configure && make && make install

#Create dir the same for nginx's root dir
RUN  mkdir -p /usr/local/nginx/html && groupadd -g 3306 mysql && useradd -g 3306 -u 3306 -s /sbin/nologin mysql && groupadd -g 1080 www && useradd  -g 1080 -u 1080 -s /sbin/nologin www
#Install PHP7.2.7
ADD http://cn.php.net/distributions/php-7.2.7.tar.gz .
RUN tar xf php-7.2.7.tar.gz
RUN useradd -r -s /sbin/nologin php-fpm \
    && cd php-7.2.7 \
    && ./configure  --prefix=/usr/local/php7 \
        --with-config-file-path=/etc/php7 \
        --with-config-file-scan-dir=/etc/php7.d \
        --with-mysqli=mysqlnd  \
        --with-pdo-mysql=mysqlnd \
        --with-mysql-sock=/tmp/mysql.sock \
        --with-iconv-dir \
        --with-freetype-dir \
        --with-jpeg-dir \
        --with-png-dir \
        --with-zlib \
        --with-bz2 \
        --with-libxml-dir \
        --with-curl \
        --with-gd \
        --with-openssl \
        --with-mhash  \
        --with-xmlrpc \
        --with-pdo-mysql \
        --with-libmbfl \
        --with-onig \
        --with-pear \
        --enable-xml \
        --enable-bcmath \
        --enable-shmop \
        --enable-sysvsem \
        --enable-inline-optimization \
        --enable-mbregex \
        --enable-fpm \
        --enable-mbstring \
        --enable-pcntl \
        --enable-sockets \
        --enable-zip \
        --enable-soap \
        --enable-opcache \
        --enable-pdo \
        --enable-mysqlnd-compression-support \
        --enable-maintainer-zts  \
        --enable-session \
        --with-fpm-user=www \
        --with-fpm-group=www  && make -j 2  && make -j 2 install
#Config file
RUN mkdir /etc/php7{,.d}
RUN cd php-7.2.7 && cp php.ini-production  /etc/php7/php.ini \
    && cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm && chmod +x /etc/rc.d/init.d/php-fpm && chkconfig --add php-fpm
RUN sed -i '/post_max_size/s/8/16/g;/max_execution_time/s/30/300/g;/max_input_time/s/60/300/g;s#\;date.timezone.*#date.timezone \= Asia/Shanghai#g' /etc/php7/php.ini
RUN cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf \
    && cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf \
    && sed -i -e 's/listen = 127.0.0.1:9000/listen = 9000/' /usr/local/php7/etc/php-fpm.d/www.conf
#EXPOSE
EXPOSE 9000
#Start php-fpm
ENTRYPOINT ["/usr/local/php7/sbin/php-fpm", "-F", "-c", "/etc/php7/php.ini"]

4、mariadb镜像Dockerfile

创建一个判断是否要进行数据库初始化的脚本:

[root@lb02 ~]# vim mariadb/start.sh
#!/bin/bash
if [ ! -f mariadb/3306/data/ibdata1 ]; then
       chown -R mysql.mysql /mariadb/3306/data/
       /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mariadb/3306/data/
        /etc/rc.d/init.d/mariadb start
        #sleep 10s
        /usr/local/mysql/bin/mysql -e "grant all on *.* to 'root'@'%' identified by '123456' with grant option;"
        /usr/local/mysql/bin/mysql -e "flush privileges;"
        #/etc/rc.d/init.d/mariadb stop
        #sleep 10s
fi
/etc/rc.d/init.d/mariadb restart
tail -f /etc/passwd

Dockerfile:

[root@lb02 ~]# vim mariadb/Dockerfile
###  Set the base image to CentOS
FROM centos

#File Author / Maintainer
MAINTAINER caomuzhong www.logmm.com

RUN yum install -y net-tools
#Create mysql user and Data dir
RUN groupadd -g 3306 mysql && useradd -g 3306 -u 3306 -s /sbin/nologin mysql && mkdir /mariadb/3306/data -pv \
   && chown -R mysql.mysql /mariadb/

#Download mariadb5.5.60 package
ADD http://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-5.5.60/bintar-linux-x86_64/mariadb-5.5.60-linux-x86_64.tar.gz .
#http://mirrors.neusoft.edu.cn/mariadb//mariadb-5.5.60/bintar-linux-x86_64/mariadb-5.5.60-linux-x86_64.tar.gz
#Unzip
RUN tar xf mariadb-5.5.60-linux-x86_64.tar.gz -C /usr/local/ \
  && cd /usr/local/ && ln -sv mariadb-5.5.60-linux-x86_64/ mysql \
  && cd mysql/ && chown -R mysql.mysql ./* \
  && chown -R mysql.mysql /usr/local/mysql
#Initialization
#RUN /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mariadb/3306/data/
#Config file
RUN cd /usr/local/mysql && /usr/bin/cp support-files/my-large.cnf /etc/my.cnf \
  && sed -i '/thread_concurrency = 8/adatadir = /mariadb/3306/data/\ninnodb_file_per = on\nskip_name_resolve = on' /etc/my.cnf \
 # && chown -R mysql.mysql /mariadb/3306/data/* \
  && /usr/bin/cp support-files/mysql.server /etc/rc.d/init.d/mariadb \
  && chmod +x /etc/rc.d/init.d/mariadb \
  && touch /var/log/mariadb.log && chown mysql.mysql /var/log/mariadb.log \
  && chkconfig --add mariadb
#expose
EXPOSE 3306
RUN rm -f mariadb-5.5.60-linux-x86_64.tar.gz
#Start mariadb 
#RUN /etc/rc.d/init.d/mariadb start
 #  && /usr/local/mysql/bin/mysql -e "grant all on *.* to 'root'@'localhost' identified by '123456';" \
 #  && /usr/local/mysql/bin/mysql -e "grant all on *.* to 'root'@'%' identified by '123456';" \
 #  && /usr/local/mysql/bin/mysql -e "flush privileges;" \
 #  && ping www.baidu.com
#   && /etc/rc.d/init.d/mariadb stop
#CMD ["/usr/local/mysql/bin/mysqld_safe"]
#ENTRYPOINT /etc/rc.d/init.d/mariadb start  && tail -f /etc/passwd
ADD mariadb/start.sh  /opt/startup.sh
RUN chmod +x /opt/startup.sh
CMD ["/bin/bash","/opt/startup.sh"]

(2)镜像构建

 docker build -t 标签 -f 指定Dockerfile  .

[root@lb02 ~]# docker build -t centos_nginx -f nginx/Dockerfile .
[root@lb02 ~]# docker build -t centos_mariadb -f mariadb/Dockerfile . 
[root@lb02 ~]# docker build -t centos_php7 -f php7/Dockerfile .

(3)启动容器

nginx、php、mariadb三个容器的启动是有先后顺序的。

nginx容器-->php容器,php容器-->mariadb容器。两两之间的数据通信通过容器启动命令docker run 加参数--link来解决。

1、启动mariadb容器:

[root@lb02 ~]# docker run  --name mariadb -p 3306:3306 -v /mariadb/3306/data/:/mariadb/3306/data/:rw --privileged=true -itd centos_mariadb
607cb139ef51df53acbcdef7725e7b0365539f2a9e873ad160495d1a597ad249
[root@lb02 ~]# 

查看ip是多少,然后在宿主机连接数据库,看看是否成功。并创建WordPress的数据库和用户。

[root@lb02 ~]# docker exec -it 607cb139ef51df53acbcd ifconfig
eth0: flags=4163  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 26  bytes 2037 (1.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14  bytes 1431 (1.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

宿主机连接数据库:

[root@lb02 ~]# mysql -uroot -p123456 -h172.17.0.3 -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
[root@lb02 ~]#

OK,数据连接成功。

登录容器创建WordPress的数据库和用户:

[root@lb02 ~]# docker exec -it 607cb139ef51df53  bash
[root@1c745fa967d8 /]# /usr/local/mysql/bin/mysql -uroot   
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wpdb character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wpdb.* to 'admin'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

2、启动php容器

[root@lb02 ~]#  docker run  --name php7 -p 9000:9000 -v  /wwwroot/:/usr/local/nginx/html:rw  --link mariadb:mariadb --privileged=true -itd centos_php7
999ee3c6d11ea9245dec18c4c2eef68276c951ea674af08f066e94e27a2df5fa
[root@lb02 ~]# 

3、启动nginx容器

[root@lb02 ~]#  docker run  --name nginx -p 80:80 -p 443:443 -v  /wwwroot/:/usr/local/nginx/html:rw  --link php7:php7 --privileged=true -itd centos_nginx
012ddc943afc656784994b62b4b5ac42c4457d1a90a5919d596819afaf5594b4
[root@lb02 ~]#

查看容器运行情况:

[root@lb02 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                         NAMES
f2420ddcc043        centos_nginx        "/usr/local/nginx/sb…"   36 seconds ago       Up 35 seconds       0.0.0.0:80->80/tcp, 443/tcp   nginx
999ee3c6d11e        centos_php7         "/usr/local/php7/sbi…"   About a minute ago   Up About a minute   0.0.0.0:9000->9000/tcp        php7
607cb139ef51        centos_mariadb      "/bin/bash /opt/star…"   5 minutes ago        Up 5 minutes        0.0.0.0:3306->3306/tcp        mariadb
[root@lb02 ~]# 

OK,已经运行了。

(4)安装WordPress

浏览器打开:192.168.10.102

docker多容器部署lnmp环境_第1张图片

填写相关信息

docker多容器部署lnmp环境_第2张图片

填写相关信息

docker多容器部署lnmp环境_第3张图片

登录看看

docker多容器部署lnmp环境_第4张图片

OK,登录成功。

docker多容器部署lnmp环境_第5张图片

至此,使用docker容器化部署lnmp完成。

这里使用nginx、mariadb、php三个独立的容器。管理起来有点麻烦,可以使用docker-compose管理。

四、docker-compose

为了防备编排,可以使用docker-compose

1、下载docker-compose

[root@lb02 ~]# curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
[root@lb02 ~]# chmod +x /usr/local/bin/docker-compose

2、创建docker-compose.yaml文件

[root@lb02 ~]# vim docker-compose.yml
version: "2"
services:
    mariadb: 
       image: centos_mariadb
       container_name: mariadb
       ports:
         - "3306:3306"
       environment:
         - MYSQL_ROOT_PASSWORD=123456
       volumes:
         - /mariadb/3306/data/:/mariadb/3306/data/
       restart: always
    php7:
       image: centos_php7
       container_name: php7
       ports:
         - "9000:9000"
       links:
         - mariadb
       volumes:
         - /wwwroot/:/usr/local/nginx/html
       restart: always
    nginx:
       image: centos_nginx
       container_name: nginx
       ports:
         - "80:80"
         - "443:443"
       links:
         - php7
       volumes:
         - /wwwroot/:/usr/local/nginx/html
       restart: always

3、启动

[root@lb02 ~]# docker-compose up -d 
Creating network "root_default" with the default driver
Creating mariadb ... done
Creating php7    ... done
Creating nginx   ... done
[root@lb02 ~]# 

有个严重的问题:

宿主机重启后,再启动容器,则数据库连接失败。

转载于:https://my.oschina.net/logmm/blog/2089171

你可能感兴趣的:(docker多容器部署lnmp环境)