目录
- 实验前准备
- 部署Nginx
- 环境准备
- 准备nginx.conf配置文件
- 生成并启动镜像
- 验证nginx
- 部署Mysql
- 准备工作目录
- 编写Dockerfile脚本
- 准备my.cnf文件
- 生成并启动镜像
- 启动镜像容器
- 验证mysql
- 部署php
- 建立工作目录
- 编写Dockerfile脚本
- 准备配置文件
- 生成并启动镜像
- 验证php
- 启动wordpress
- mysql授权
- 测试
实验前准备
宿主机:192.168.188.17
所有安装包下载:
wget http://101.34.22.188/lnmp_wordpress/mysql-boost-5.7.20.tar.gz
wget http://101.34.22.188/lnmp_wordpress/nginx-1.12.0.tar.gz
wget http://101.34.22.188/lnmp_wordpress/php-7.1.10.tar.bz2
wget http://101.34.22.188/lnmp_wordpress/wordpress-4.9.4-zh_CN.tar.gz
或者
wget -r -np http://101.34.22.188/lnmp_wordpress/
环境准备
容器操作系统 IP地址 主要软件
nginx Centos7 172.20.0.10 Docker-Nginx
mysql Centos7 172.20.0.20 Docker-Mysql
php Centos7 172.20.0.30 Docker-php
关闭防火墙
systemctl stop firewalld
setenforce 0
创建自定义网段
docker network create --subnet=172.20.0.0/16 --opt “com.docker.network.bridge.name”=“docker1” mynetwork
部署Nginx
环境准备
mkdir /opt/nginx
上传nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中
cd /opt/nginx
mkdir /opt/nginx/html
tar -zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
编写Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <lnmp>
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;" ]
准备nginx.conf配置文件
复制一份nginx.conf配置文件到/opt/nginx/目录下
主要是增加一个动态页面的location块,这里的IP要写PHP服务的容器ip
生成并启动镜像
docker build -t nginx:lnmp .
docker images
启动镜像容器
docker run --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.20.0.10 -d nginx:lnmp
docker ps
验证nginx
curl 192.168.188.17:80
部署Mysql
准备工作目录
mkdir /opt/mysql
cd /opt/mysql
把mysql-boost-5.7.20.tar.gz复制到/opt/mysql
编写Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql images <wl>
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" ]
准备my.cnf文件
在/opt/mysql下准备一个
vim 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 = 1sql_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
生成并启动镜像
docker build -t mysql:lnmp .
启动镜像容器
docker run --name=mysql --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.20.0.20 -d mysql:lnmp
验证mysql
docker exec -it mysql bash
systemctl status mysqld
部署php
建立工作目录
mkdir /opt/php
cd /opt/php
复制php-7.1.10.tar.bz2到/opt/php
编写Dockerfile脚本
vim Dockerfile
FROM centos:7
MAINTAINER this is php image <wl>
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
准备配置文件
php.ini需要进行如下修改
取消注释,修改时区
修改套接字
php-fpm.conf需要进行如下修改
取消注释
www.conf需要进行如下修改
修改用户和组
修改监听ip和端口
运行访问的ip
生成并启动镜像
docker build -t php:lnmp .
启动镜像容器
docker run --name php --net mynetwork --ip 172.20.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql -itd php:lnmp
验证php
docker exec -it php bash
ps -aux
启动wordpress
mysql授权
进入mysql容器
docker exec -it mysql 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/wordpress/index.php