一、项目模拟
1. 项目环境
公司在实际的生产环境中,需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。然后对此服务进行相关的性能调优和管理工作。
所有安装包下载:
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/
123456
2. 服务器环境
容器 | 操作系统 | IP地址 | 主要软件 |
---|---|---|---|
nginx | CentOS 7.9 x86_64 | 172.111.0.10 | Docker-Nginx |
mysql | Centos 7.9 x86_64 | 172.111.0.20 | Docker-Mysql |
php | Centos 7.9 x86_64 | 172.111.0.30 | Docker-php |
3. 任务需求
(1)使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台。
(2)限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap。
(3)限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s。
(4)将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地。
二、【Linux】 系统基础镜像
[root@docker ~]# systemctl disable --now firewalld
[root@docker ~]# setenforce 0
setenforce: SELinux is disabled
[root@docker ~]# docker pull centos:7
#从公有仓库中下载 centos7 作为系统基础镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
12345678
三、Nginx
1. 建立工作目录
[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 nginx 安装包 nginx-1.12.0.tar.gz
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz
12345678
2. 编写 Dockerfile 脚本
[root@docker nginx]# 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;" ]
12345678910111213141516171819
3. 准备 nginx.conf 配置文件
[root@docker nginx]# ls
Dockerfile nginx-1.12.0.tar.gz nginx.conf wordpress-4.9.4-zh_CN.tar.gz
[root@docker nginx]# egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 172.111.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
123456789101112131415161718192021222324252627282930313233
4. 生成镜像
[root@docker nginx]# docker build -t nginx:lnmp .
[root@docker nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp 35a6404fcfa1 5 seconds ago 522MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
12345
5. 创建自定义网络
[root@docker nginx]# docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
0cbe1bd0bd782bf1c8e69916d99427970196de22deb312f970e01030406d9b45
[root@docker nginx]# docker network ls
NETWORK ID NAME DRIVER SCOPE
dd7a55d01f86 bridge bridge local
63ddf1e359e9 host host local
0cbe1bd0bd78 mynetwork bridge local
a4b66a8a6cd2 none null local
[root@docker nginx]# ifconfig docker1
docker1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.111.0.1 netmask 255.255.0.0 broadcast 172.111.255.255
ether 02:42:40:dc:e8:1d txqueuelen 0