目录
一:项目环境
1、主机ip需求
2、 任务需求
二:多级构建Dockerfile实验部署 lnmp
1、先部署一个有所有依赖包的镜像
2、搭建nginx
3、搭建mysql
4、搭建php
三:一级构建安装lnmp
1、构建自定义docker网络
2、构建nginx容器(172.18.0.10)
3、构建mysql(172.18.0.20)
4、构建php(172.18.0.30)
一:项目环境
公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工
1、主机ip需求
nginx容器 ip地址:172.18.0.10
mysql容器 ip地址:172.18.0.20
php容器 ip地址:172.18.0.30
2、 任务需求
1)使用Docker构建LNMP环境并运行Wordpress网站平台
2)限制Nginx容器最多使用500Mb的内存和1G的Swap
3)限制Mysql容器写 /dev/sda 的速率为 10 MB/s
4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地
二:多级构建Dockerfile实验部署 lnmp
1、先部署一个有所有依赖包的镜像
###前提本地已经安装docker,配置lnmp的文件目录
cd /opt/
mkdir mylnmp
cd /opt/mylnmp/
mkdir nginx mysql php
###部署一个有所有软件依赖包的镜像,以便进行分阶段构建镜像
cd /opt/
vim Dockerfile
FROM centos:7 ###从公有仓库pull一个centos:7的镜像
COPY qinghua-yum.sh /opt/ ###上传修改yum源的脚本
RUN chmod +x /opt/qinghua-yum.sh && /opt/qinghua-yum.sh && \
yum -y install pcre-devel zlib-devel openssl-devel \
gcc gcc-c++ make ncurses ncurses-devel bison cmake gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel
docker build -t centos7:mylnmp
docker images
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mylnmp
docker network ls
2、搭建nginx
###nginx目录下已上传nginx安装包、论坛安装包、nginx配置文件
cd /opt/mylnmp/nginx/
ls
###部署 nginx 的 Dockerfile
cd /opt/mylnmp/nginx/
vim Dockerfile
FROM centos7:mylnmp
ADD nginx-1.24.0.tar.gz /usr/local/
RUN useradd -M -s /sbin/nologin nginx && \
cd /usr/local/nginx-1.24.0 && \
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
FROM centos:7
COPY --from=0 /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx
EXPOSE 80
#EXPOSE 443
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
docker build -t nginx:lnmp
docker images
docker run -d --name nginx --network=myLnmp --ip=172.18.0.10 -p 80:80 -v /opt/mylnmp/nginx/html/:/usr/local/nginx/html/ -m 500m - -memory-swap=1.5g centos7:myLnmp
docker ps -a
3、搭建mysql
###mysql目录下已上传mysql安装包、mysql配置文件
cd /opt/mylnmp/mysql/
ls
###部署 mysql 的 Dockerfile
cd /opt/mylnmp/mysql/
vim Dockerfile
FROM centos7:mylnmp
ADD mysql-boost-5.7.41.tar.gz /opt/
RUN cd /opt/mysql-5.7.41/ && \
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
FROM centos:7
COPY --from=0 /usr/local/mysql/ /usr/local/mysql/
COPY my.cnf /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && \
chown -R mysql:mysql /usr/local/mysql/ && \
chown mysql:mysql /etc/my.cnf && \
/usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysgl \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
docker build -t mysql:lnmp
docker images
docker run -d --name mysql --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql --device-write-bps /dev/sda:10mb mysql:lnmp
docker ps -a
docker exec -it 5066206ee1b6 bash
mysqladmin -u root password "123"
mysql -u root -p123
CREATE DATABASE wordpress;
GRANT all ON wordpress.* TO 'admin'@'%' IDENTIFIED BY 'abc123';
flush privileges;
exit
4、搭建php
###mysql目录下已上传php安装包、php配置文件
cd /opt/mylnmp/php/
ls
###部署 php 的 Dockerfile
cd /opt/mylnmp/php/
vim Dockerfile
FROM centos7:mylnmp
ADD php-7.1.10.tar.bz2 /opt
RUN cd /opt/php-7.1.10 && \
./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
COPY php.ini /usr/local/php/lib/php.ini
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf
FROM centos:7
COPY --from=0 /usr/local/php /usr/local/php
RUN useradd -M -s /sbin/nologin php && yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
docker build -t php:lnmp
docker images
docker run --name php -d -p 9000:9000 --network mylnmp --ip 172.18.0.30 --volumes-from nginx --volumes-from mysql php:lnmp
docker ps -a
docker exec -it 850d14f706b7 bash
ls /usr/local
ls /usr/local/nginx/html
ls /usr/local/mysql
exit
三:一级安装lnmp
1、构建自定义docker网络
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
2、构建nginx容器(172.18.0.10)
1.mkdir /opt/nginx
cd /opt/nginx/
把nginx-1.24.0.tar.gz和wordpress-4.9.4-zh_CN.tar.gz安装包移进来
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
2.vim Dockerfile
#基于基础镜像
FROM centos:7
#用户信息
MAINTAINER this is nginx image <gxd>
#添加环境包
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin nginx
#上传nginx软件压缩包,并解压
ADD nginx-1.24.0.tar.gz /usr/local/src/
#指定工作目录
WORKDIR /usr/local/src/nginx-1.24.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
ENV PATH /usr/local/nginx/sbin:$PATH #指定路径
ADD nginx.conf /usr/local/nginx/conf/ #指定修改后的nginx配置文件
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/ #指定网页内容
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80 #指定http端口
EXPOSE 443 #指定https端口
CMD nginx -g "daemon off;"
3.修改nginx配置文件(与php对接)
在本机或者其他主机安装nginx,把nginx配置文件移进本机
vim nginx.conf
...
location / {
root html;
index index.html index.php;
}
...
location ~ \.php$ {
root html;
fastcgi_pass 172.18.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
...
4.创建新镜像
docker build -t nginx:lnmp .
5.启动容器
docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp #限制Nginx容器最多使用500Mb的内存和1G的Swap
3、构建mysql(172.18.0.20)
#创建mysql容器
mkdir -p /data/mysql
#下载mysql源码包,直接拖入都可
#编写Dockerfile配置文件
vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make;useradd -M -s /sbin/nologin mysql
ADD mysql-boost-5.7.41.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.41/
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 -j 2;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"]
#编写mysql配置文件
vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[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 = 1
sql_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
#生成mysql镜像文件并启动镜像容器(需要在对应目录中完成)
docker build -t mysql:lnmp .
docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net lnmp --ip 172.18.0.20 mysql:lnmp
4、构建php(172.18.0.30)
1.mkdir /opt/php
cd /opt/php
上传 php-7.1.10.tar.bz2 到/opt/php目录中
2.vim Dockerfile
FROM centos:7
MAINTAINER this is php image <wl>
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 && 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
3.创建新镜像
docker build -t php:lnmp .
4.创建新容器
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
docker exec -it mysql /bin/bash
mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
http://192.168.231.102/wordpress/index.php