目录
1.准备nginx
2.准备mysql
3.准备php
4.构建各镜像
5.运行wordpress
1、项目环境: | |||||||||
1.1 | (1)公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。 (2)环境描述
| ||||||||
2、任务需求 | |||||||||
2.1 | (1)使用Docker构建LNMP环境并运行Wordpress网站平台。 (2)限制Nginx容器最多使用500Mb的内存和1G的Swap。 (3)限制Mysql容器写 /dev/sda 的速率为 10 MB/s。 (4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。 |
1.准备nginx
注:可以以上篇博客中nginx构建为基础
上传nginx.conf文件、上传阿里云镜像、上传html目录
2.准备mysql
cd /opt
mkdir mysql
vim Dockerfile
FROM centos:7 as first
ADD mysql-boost-5.7.44.tar.gz /opt
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN yum -y install ncurses ncurses-devel bison cmake openssl-devel gcc gcc-c++ make && \
yum clean all && \
cd /opt/mysql-5.7.44/ && \
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 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && \
make -j 2 && make install
FROM centos:7
COPY --from=first /usr/local/mysql/ /usr/local/mysql/
COPY my.cnf /etc/my.cnf
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=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
ENV PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
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压缩包
3.准备php
vim Dockerfile
FROM centos:7 as first
ADD php-7.1.10.tar.bz2 /opt/
COPY CentOS-Base.repo /etc/yum.repos.d/
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 && \
yum clean all && \
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=first /usr/local/php/ /usr/local/php/
COPY CentOS-Base.repo /etc/yum.repos.d/
RUN useradd -M -s /sbin/nologin nginx &&\
yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel &&\
yum clean all
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
上传php压缩包、阿里云镜像、php-fpm.conf php.ini www.conf
4.构建各镜像
cd /opt/nginx_bian #构建nginx镜像
docker build -t nginx:lnmp .
cd /opt/php #构建php镜像
docker build -t php:lnmp .
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork #自定义网络
docker network ls
测试nginx、php
cd /opt/nginx_bian/
docker run --name nginx -d -p 80:80 -v /opt/nginx_bian/html:/usr/local/nginx/html/ --network mynetwork --ip 172.18.0.10 nginx:lnmp
docker ps -a
docker run --name php -d -p 9000:9000 --volumes-from nginx --network mynetwork --ip 172.18.0.30 php:lnnmp
docker ps -a
docker run --name php -d -p 9000:9000 --volumes-from nginx --network mynetwork --ip 172.18.0.30 php:lnnmp
docker ps -a
cd /opt/mysql
docker build -t mysql:lnmp . #构建mysql镜像
docker run --name mysql -d --privileged -v /usr/local/mysql --network mynetwork --ip 172.18.0.20 mysql:lnmp #启动容器
docker ps -a
mysql配置
docker exec -it mysql bash
mysqladmin -u root -p password "abc123" #设置密码
mysql -u root -pabc123 #登录mysql
grant all on *.* to 'root'@'%' identified by 'abc123';
quit
exit
docker stop php
docker rm php
docker run --name php -d -p 9000:9000 --volumes-from nginx --volumes-from mysql --network mynetwork --ip 172.18.0.30 php:lnnmp
浏览器验证
5.运行wordpress
浏览器访问:http://192.168.9.113/wordpress/index.php
docker exec -it mysql bash
mysql -u root -pabc123
create database wordpress;
grant all on wordpress.* to 'wordpress'@'%' identified by 'admin123';
flush privileges;
quit
exit
cd /opt/nginx_bian/html
chmod 777 wordpress/