一、项目背景和要求
公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。
然后对此服务进行相关的性能调优和管理工作
二、项目环境
主机 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
宿主机 | CentOS 7.3 x86_64 | 192.168.2.106 | Docker 19.03 |
Nginx容器 | 172.18.0.10 | ||
Mysql容器 | 172.18.0.20 | ||
PHP容器 | 172.18.0.30 |
三、部署过程
1)创建自定义网络
#关闭防火墙和selinux
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
容器IP:172.18.0.10
Step1 创建工作目录并上传相关软件包
mkdir /opt/nginx
cd /opt/nginx
mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
Step2 编写Dockerfile文件
vim Dockerfile
FROM centos:7
MAINTAINER this is nginx image <wl>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN useradd -M -s /sbin/nologin 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/
#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
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
Step3 编写配置文件nginx.conf
vim nginx.conf
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.byyb.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
Step4 创建nginx镜像
docker build -t nginx:lnmp .
Step5 运行容器
#以nginx容器 在后台运行镜像
docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
- "-d" 表示以后台运行的方式启动容器。
- "--name nginx" 设置容器的名称为 "nginx"。
- "-p 80:80" 将主机的 80 端口映射到容器的 80 端口,使得可以通过主机的 IP 地址访问 NGINX 服务器。
- "-v /opt/nginx/html:/usr/local/nginx/html" 将主机上的 /opt/nginx/html 目录与容器内的 /usr/local/nginx/html 目录进行挂载,实现文件共享。
- "--net mynetwork --ip 172.18.0.10" 将容器连接到名为 "mynetwork" 的用户自定义网络,并指定容器的 IP 地址为 172.18.0.10。
- "nginx:lnmp" 指定了要使用的 NGINX 服务器镜像和标签。
3)部署Mysql
容器IP 为 172.168.0.20
多阶段构造
Step1 创建工作目录
mkdir /opt/mysqld
cd /opt/mysqld
上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中
Step2 编写Dockerfie文件(多阶段构建)
vim Dockerfile
# 第一阶段 - 构建阶段
FROM centos:7 AS builder
MAINTAINER this is mysql image <byyb>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
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_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 && make install
# 第二阶段 - 配置阶段
FROM centos:7 AS config
COPY --from=builder /usr/local/mysql /usr/local/mysql
COPY my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && \
chown mysql:mysql /etc/my.cnf && \
chown -R mysql:mysql /usr/local/mysql/ && \
/usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
Step3 编写配置文件
vim my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysql]
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
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
Step4 创建镜像
docker build -t mysql:lnmp .
#名称:标签
Step5 使用新的镜像运行容器并测试
docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
- `--name=mysql`:指定容器的名称为mysql。
- `-d`:以后台模式运行容器。
- `--privileged`:在容器内部启用特权模式,可以执行一些底层操作。
- `-v /usr/local/mysql`:将宿主机的`/usr/local/mysql`目录挂载到容器的相应位置。
- `--net mynetwork`:将容器连接到名为mynetwork的网络。
4)PHP安装
容器IP:172.168.0.30
Step1 创建工作目录并上传代码包
mkdir /opt/php
cd /opt/php
上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
Step2 编写相关配置文件
php-fpm.conf
www.conf
php.ini
Step3 编写Dockerfile文件
vim Dockerfile
FROM centos:7 AS first
MAINTAINER this is php image <byyb 20231021>
ADD php-7.1.10.tar.bz2 /opt/
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 && \
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 && \
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini && \
sed -i -e '939c date.timezone = Asia/Shanghai' -e '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini && \
cd /usr/local/php/etc/ && \
cp php-fpm.conf.default php-fpm.conf && \
sed -i '17 s/^;//' php-fpm.conf && \
cd /usr/local/php/etc/php-fpm.d/ && \
cp www.conf.default www.conf && \
sed -i -e '23c user = nginx' -e '24c group = nginx' -e '36c listen = 192.168.3.103:9000' -e '62c listen.allowed_clients = 192.168.3.106' www.conf
FROM centos:7
COPY --from=first /usr/local/php/ /usr/local/php/
RUN yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel && \
useradd -M -s /sbin/nologin nginx && \
rm -rf /var/cache/yum/*
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
Step4 创建php镜像
docker build -t php:lnmp .
Step5 运行容器
docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
5)数据库授权
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 'abc123';
flush privileges;
6)运行Wordpress
浏览器访问:
http://192.168.2.105/wordpress/index.php
文章来源:https://www.toymoban.com/news/detail-721504.html
文章来源地址https://www.toymoban.com/news/detail-721504.html
到了这里,关于【Dockerfile镜像实战】构建LNMP环境并运行Wordpress网站平台的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!