一.项目模拟
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/
2.服务器环境
容器 | 操作系统 | IP地址 | 主要软件 |
nginx | CentOS 7 | 172.18.0.10 | Docker-Nginx |
mysql | CentOS 7 | 172.18.0.20 | Docker-Mysql |
php | CentOS 7 | 172.18.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
三.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
2.编写Dockerfile脚本
[root@docker nginx]# vim DockerfileFROM 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;" ]
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.18.0.30:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;include fastcgi_params;}}
}
4.设置自定义网段和创建镜像和容器
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetworkdocker network ls
查看
[root@docker nginx]# docker build -t nginx:lnmp .
[root@docker nginx]# docker images
5.启动镜像容器
[root@benet23 nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.111.0.10 nginx:lnmp
24cbedd7982b1cf658cff4efd1ea75a5bfe252b6d01b2a222ed4cde63215479d
[root@benet23 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
24cbedd7982b nginx:lnmp "/usr/local/nginx/sb…" 4 seconds ago Up 3 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx······[root@benet23 nginx]# docker inspect nginx"Networks": {"mynetwork": {"IPAMConfig": {"IPv4Address": "172.111.0.10"
·