一、项目环境
公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。
1. 环境配置
主机 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
Docker | CentOS 7.3 x86_64 | 192.168.145.15 | Docker 19.03 |
容器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包备份到本地。
二、 部署过程
1. 环境配置
#前提本地已经安装docker,配置lnmp的文件目录
cd /opt/
mkdir lnmp
cd lnmp/
mkdir nginx mysql php
#构建yum安装的依赖包容器
cd /opt/lnmp/
mkdir centos
cd centos/
vim Dockerfile
FROM centos:7
#nginx安装包
RUN yum install -y pcre-devel \
zlib-devel \
openssl-devel \
gcc \
gcc-c++ \
make \
#mysql安装包
ncurses \
ncurses-devel \
bison \
cmake \
#php安装包
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
#安装镜像
docker build -t centos7:pack .
docker images
2. 部署构建 nginx 容器
#nginx目录下已上传nginx安装包以及论坛安装包
cd nginx/
ls
#修改nginx配置文件
vim nginx.conf
...
server {
...
location / {
root html;
index index.php index.html index.htm;
}
...
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;
}
#构建自定义网络
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mylnmp
1d31b24a91f3c5c38def3d3b853d470ebfa03605e6a14c57d6b6f0ea9939d98b
docker network ls
NETWORK ID NAME DRIVER SCOPE
7fcb679c6080 bridge bridge local
e06421ce591e host host local
b084bede80c1 mylnmp bridge local
ad41911b801f none null local
#创建nginx镜像
cd /opt/lnmp/nginx
vim Dockerfile
FROM centos7:pack
MAINTAINER this is nginx image <datang 2023-07-24>
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 && \
yum clean all
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
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
#构建nginx容器
docker build -t nginx:pack .
docker images
#创建并启动容器
docker run -d -P nginx:pack
077368b80269fd3d871abba1cdc8249fabe3152bc85b99c3e1cf1c283fd6512e
#查看容器
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
077368b80269 nginx:pack "/usr/local/nginx/sb…" 5 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp romantic_fermat
#浏览器访问
http://192.168.145.15:32768/
#解压论坛压缩包
ls
mkdir html
tar xf wordpress-4.9.4-zh_CN.tar.gz
mv wordpress html/
#删除其余的容器,重新根据自定义网络创建容器
docker rm -f $(docker ps -aq)
docker run -d --name nginx --network=mylnmp --ip=172.18.0.10 -p 80:80 -v /opt/lnmp/nginx/html/:/usr/local/nginx/html/ nginx:pack
da398761b9a04cb7bd5a48f33cfd776b8048567cc67f300d55a4b989d06a64b1
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da398761b9a0 nginx:pack "/usr/local/nginx/sb…" 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
2. 部署构建 mysql 容器
#准备muysql的配置文件
cd /opt/lnmp/mysql/
ls
mysql-boost-5.7.41.tar.gz
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
vim Dockerfile
FROM centos7:pack
MAINTAINER this is mysql image <datang 2023-07-24>
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 -j2 && 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=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
docker build -t mysql:centos7 .
#构建 mysql 容器,--privileged 指定root权限
docker run -d --privileged --network mylnmp --ip 172.18.0.20 mysql:centos7
412c0a21662b163429257c809fbf5402769cecc7b6b7bde95d53f02e6565b36f
docker ps -a
#测试mysql
docker exec -it 412c0a21662b bash
ps aux
mysql
docker rm -f 412c0a21662b
docker ps -a
#重新设置mysql的容器
docker run -d --name mysql --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql mysql:centos7
9d39b0103585f35e8fc22022faf33cbd94e3f3eaba1ad03dcc211e896eb42ef3
docker exec -it mysql bash
#设置登录密码
mysqladmin -u root -p password "abc123"
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysql -u root -pabc123
#添加mysql授权登录
mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all on wordpress.* to 'admin'@'%' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3. 部署构建 php 容器
#准备php安装包
cd /opt/lnmp/php/
ls
php-7.1.10.tar.bz2
#安装环境依赖包
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
#解压并编译
tar xf php-7.1.10.tar.bz2
cd 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 -j2
make install
#将编译安装的php相关文件复制到/opt/lnmp/php/目录下
cd /opt/lnmp/php/
mv /usr/local/php/etc/ /opt/lnmp/php/
cd etc/
mv php-fpm.conf.default php-fpm.conf
ls
vim php-fpm.conf
pid = run/php-fpm.pid
cd php-fpm.d/
mv www.conf.default www.conf
vim www.conf
#将php相关配置文件复制到/opt/lnmp/php/目录下
cd /opt/lnmp/php/php-7.1.10/
mv php.ini-development /opt/lnmp/php/php.ini
cd /opt/lnmp/php
mv etc/php-fpm.conf /opt/lnmp/php/
mv etc/php-fpm.d/www.conf /opt/lnmp/php/
ls
etc php-7.1.10 php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
vim php.ini
#939行,修改时区
date.timezone = Asia/Shanghai
#1170行
mysqli.default_socket = /usr/local/mysql/mysql.sock
#构建php镜像
vim Dockerfile
FROM centos7:pack
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 -j2 && 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"]
#构建php镜像
docker build -t php:centos .
#查看镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php centos b1cae07dd109 2 minutes ago 800MB
mysql centos7 f8c99d9f8a8a 18 minutes ago 4.24GB
nginx pack ec077bb0e047 About an hour ago 208MB
centos7 pack 5725aec209e6 About an hour ago 754MB
centos 7 eeb6ee3f44bd 22 months ago 204MB
#构建php容器
docker run --name php -d -p 9000:9000 --network mylnmp --ip 172.18.0.30 php:centos
ff2422bed9cdb9f733e488be2ff9b98ff9b0ca736b47ef4bf656491297ea2b37
#查看容器
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ff2422bed9cd php:centos "/usr/local/php/sbin…" 5 seconds ago Up 5 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
da398761b9a0 nginx:pack "/usr/local/nginx/sb…" 57 minutes ago Up 57 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
#测试php网页
cd /opt/lnmp/nginx/html/
vim index.php
cat index.php
<?php
phpinfo();
?>
#浏览器访问
http://192.168.145.15/index.php
4. 连接所有容器
#删除所有容器
docker rm -f $(docker ps -aq)
docker run -d --name nginx --network mylnmp --ip 172.18.0.10 -v /usr/local/nginx/html/ -p 80:80 nginx:pack
4d9eb17785e2ce9b88152644b8a9c02674212606a3390e0c089e48269e6eac91
docker run -d --name mysql --privileged --network mylnmp --ip 172.18.0.20 -v /usr/local/mysql/ mysql:centos7
51dbe9aed78636a27457a1279e0c1b09f8298d76ae829a3f9dc0ce4bbdbd9686
docker run -d --name php --network mylnmp --ip 172.18.0.30 --volumes-from nginx --volumes-from mysql -p 9000:9000 php:centos 9d134d25d84a2c418e9c41b58bfaaf30f57ccd33c56e99705ecc406e305eafb0
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d134d25d84a php:centos "/usr/local/php/sbin…" 2 seconds ago Up 1 second 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
51dbe9aed786 mysql:centos7 "/usr/local/mysql/bi…" 2 minutes ago Up 2 minutes 3306/tcp mysql
4d9eb17785e2 nginx:pack "/usr/local/nginx/sb…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx
#将网页和论坛配置文件上传至容器内部
cd /opt/lnmp/nginx/html/
docker cp index.php nginx:/usr/local/nginx/html/
Successfully copied 2.05kB to nginx:/usr/local/nginx/html/
docker cp wordpress/ nginx:/usr/local/nginx/html/
Successfully copied 29.4MB to nginx:/usr/local/nginx/html/
文章来源:https://www.toymoban.com/news/detail-605774.html
#浏览器访问
http://192.168.145.15/wordpress/index.php
文章来源地址https://www.toymoban.com/news/detail-605774.html
总结
如何缩小镜像体积的大小
1.尽可能的使用小体积的基础镜像
2.尽可能减少Dockerfile文件中指令的数量
3.构建镜像步骤最后添加清空系统和应用程序的缓存命令
4.使用多阶段(多级)构建
FROM 第一阶段的基础镜像 [AS 别名]
...
FROM 第二阶段的基础镜像
COPY --from=别名/0 第一阶段构建的文件/目录 当前阶段的文件/目录
到了这里,关于【Docker】基于Dockerfile搭建LNMP架构的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!