目录
一、项目模拟
1. 项目环境
2. 服务器环境
3. 任务需求
二、Linux系统基础镜像
三、Nginx
1. 建立工作目录
2. 编写Dockerfile脚本
3. 准备nginx.conf配置文件
4. 生成镜像
5. 创建自定义网络
6. 启动镜像容器
7. 验证nginx
三、Mysql
1. 建立工作目录
2. 编写Dockerfile
3. 准备my.cnf文件
4. 生成镜像
5. 启动镜像容器
6. 验证mysql
四、Php
1. 建立工作目录
2. 编写Dockerfile脚本
3. 准备php.ini、php-fpm.conf、www.conf配置文件
4. 生成镜像
5. 启动镜像容器
6. 验证php
五、启动wordpress服务
1. mysql授权
2. 浏览器访问测试
3. 备份镜像至本地
一、项目模拟
1. 项目环境
公司在实际的生产环境中,需要使用Docker技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。
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 |
|
#从公有仓库中下载centos 7作为系统基础镜像 |
|
[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 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 -j4 && 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.111.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.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 (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@docker nginx]# docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.111.0.10 nginx:lnmp |
|
57616d4ea225c82a50b731472b003dabfd681e8dc6c6ef85a9bb2f665354334b |