一、项目
1.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/
1.2 服务器环境
1.3 任务需求
使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap
限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地
二、Linux 系统基础镜像
systemctl disable --now firewalld setenforce 0 docker pull centos:7 #从公有仓库中下载 centos7 作为系统基础镜像 docker images
三、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 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;" ]
网络异常,图片无法展示
|
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.20.0.30:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
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
5、创建自定义网络
[root@docker nginx]# docker network create --subnet=172.20.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork cdc7b80633abf6c1f573528234f024f6088340475acae277d9710f0d2d5dc400 [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@benet23 nginx]# ifconfig docker1 docker1: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.20.0.1 netmask 255.255.0.0 broadcast 172.20.255.255 ether 02:42:4a:c3:31:e7 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6、启动镜像容器
[root@benet23 nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.20.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.20.0.10"
7、验证 nginx
[root@docker nginx]# curl http://192.168.147.105:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
四、Mysql
1、建立工作目录
[root@benet23 nginx]# mkdir /opt/mysql [root@benet23 nginx]# cd /opt/mysql/ [root@benet23 mysql]# rz -E rz waiting to receive. #传入mysql安装包mysql-boost-5.7.20.tar.gz
2、编写 Dockerfile
[root@docker mysql]# vim Dockerfile FROM centos:7 MAINTAINER this is mysql image <lnmp> RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql 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_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 -j4;make install ADD my.cnf /etc/my.cnf EXPOSE 3306 RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf WORKDIR /usr/local/mysql/bin/ RUN ./mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH VOLUME [ "/usr/local/mysql" ] CMD ["/usr/sbin/init"]