环境准备

systemctl stop firewalld

setenforce 0

#创建自定义网络,方便自定义容器网段

 docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

Nginx容器(容器ip和端口为172.18.0.10:80)

mkdir /opt/nginx
mkdir /opt/nginx/html                                 #创建首页文件目录
cd /opt/nginx/

#上传 nginx-1.12.0.tar.gz到/opt/nginx/目录中
#上传 wordpress-4.9.4-zh_CN.tar.gz到/opt/nginx目录中
mkdir /opt/nginx/html tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html #另外的主机使用源码编译安装nginx将起conf目录内所有文件传入到容器宿主机,方便挂载给nginx容器使用 vim Dockerfile FROM centos:7 MAINTAINER this is nginx image <dd>

RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make RUN 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 wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/ RUN chmod 777 -R /usr/local/nginx/html/ EXPOSE 80 EXPOSE 443 CMD nginx -g "daemon off;" docker build -t nginx:lnmp . docker images docker run -d --name nginx -m 500m --memory-swap=1.5g -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html -v /opt/nginx/conf/:/usr/local/nginx/conf/ --net mynetwork --ip 172.18.0.10 nginx:lnmp #挂载准备好的首页文件夹和配置文件夹 #-m 500m --memory-swap=1.5g限制使用最大内存500m,交换空间为1G docker ps -a docker inspect 容器名 #可以查看容器具体参数

Dockerfile运用(容器LNMP架构+wordpress)_html

Dockerfile运用(容器LNMP架构+wordpress)_mysql_02

 Mysql容器(容器ip和端口为172.18.0.20:3306)

mkdir /opt/mysql
cd /opt/mysql
#上传 mysql-boost-5.7.20.tar.gz到/opt/mysql目录中
#提前在/opt/mysql目录中准备好mysql的配置文件my.cnf,直接复制到mysql容器中

FROM centos:7
MAINTAINER this is mysql image <dd>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.20.tar.gz /opt
RUN cd /opt/mysql-5.7.20/;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
RUN cd /opt/mysql-5.7.20/;make -j 4 && make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
RUN systemctl enable mysqld
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

docker build -t mysql:lnmp . 
docker images
docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
#限制往/dev/sda磁盘中写的速度为10M/s
docker ps -a

Dockerfile运用(容器LNMP架构+wordpress)_html_03

php容器(容器ip和端口为172.18.0.30:9000)

mkdir /opt/php
cd /opt/php

#上传 php-7.1.10.tar.bz2到 /opt/php 目录中
#在另一台主机上安装php,在/opt/php 目录中提前配置好三个文件,直接复制给容器
php.ini            主配置文件  
php-fpm.conf    进程服务配置文件 
www.conf        扩展配置文件(优化用的)

vim Dockerfile
FROM centos:7
MAINTAINER this is php image <dd>
RUN yum -y install 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
RUN 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 2 && 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

docker build -t php:dd .
docker images
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:dd
docker ps -a

Dockerfile运用(容器LNMP架构+wordpress)_nginx_04

Dockerfile运用(容器LNMP架构+wordpress)_nginx_05

进入数据库创建wordpress数据库并授予远程访问的权限

docker exec -it mysql /bin/bash

mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;
#web页面进行访问验证
http://192.168.150.30/wordpress/index.php

Dockerfile运用(容器LNMP架构+wordpress)_docker_06

Dockerfile运用(容器LNMP架构+wordpress)_docker_07