自定义Dockerfile 搭建LNMP架构
Posted 我的紫霞辣辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义Dockerfile 搭建LNMP架构相关的知识,希望对你有一定的参考价值。
自定义Dockerfile搭建LNMP架构
1. 创建挂载目录,将wordpress解压到挂载目录
# 下载wordpress压缩包
[root@docker01 ~]# wget https://cn.wordpress.org/latest-zh_CN.tar.gz
# 创建wordpress目录
[root@docker01 ~]# mkdir /tmp/html
[root@docker01 ~]# tar -xf latest-zh_CN.tar.gz -C /tmp/
[root@docker01 ~]# cd /tmp/html
[root@docker01 html]# mv /tmp/wordpress/* .
2. 创建网桥
[root@docker01 html]# docker network create lnmp
eac4d96569c9e7ac2868dc1108cd2984de2f97f049bcd42187e51faa31d2f13b
3. 构建PHP镜像
# 创建nginx和php目录
[root@docker01 html]# mkdir /docker
[root@docker01 html]# cd /docker/
[root@docker01 docker]# mkdir nginx
[root@docker01 docker]# mkdir php
# 创建PHP镜像的Dockerfile
[root@docker01 docker]# cd nginx/
[root@docker01 nginx]# vim Dockerfile
FROM centos:7
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
RUN yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
RUN useradd www
ADD www.conf /etc/php-fpm.d/
EXPOSE 9000
CMD php-fpm -F
# 创建Dockerfile推送的配置文件
[root@docker01 nginx]# vim www.conf
[www]
user = www
group = www
listen = 9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
request_terminate_timeout = 0
# 生成PHP镜像
[root@docker01 nginx]# docker build -t "lnmp_php" .
4. 构建Nginx镜像
# 创建Nginx镜像的Dockerfile
[root@docker01 nginx]# cd /docker/php/
[root@docker01 php]# vim Dockerfile
FROM centos:7
COPY nginx.repo /etc/yum.repos.d/
RUN yum makecache
RUN yum -y install nginx
RUN useradd www
ADD default.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/
EXPOSE 80 443
WORKDIR /root
CMD nginx -g "daemon off;"
# 创建Nginx的yum源文件
[root@docker01 php]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 创建Nginx的主配置文件
[root@docker01 php]# vim nginx.conf
user www;
worker_processes auto;
worker_cpu_affinity auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
worker_rlimit_nofile 35535;
events {
use epoll;
worker_connections 10240;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format json_access '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"url":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"status":"$status"}';
access_log /var/log/nginx/access.log json_access;
server_tokens off;
client_max_body_size 200m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_disable "MSIE [1-6]\\.";
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_buffers 16 8k;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg;
include /etc/nginx/conf.d/*.conf;
}
# 创建Nginx的server文件
[root@docker01 php]# vim default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
location ~ \\.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 生成nginx镜像
[root@docker01 php]# docker build -t "lnmp_nginx" .
5. 下载官网上的MySQL镜像
[root@docker01 php]# docker pull mysql:5.7
6. 查看本地所有的镜像
[root@docker01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
lnmp_php latest 71403cb24b4d 44 minutes ago 568MB
lnmp_nginx latest 46461885c43a 46 minutes ago 615MB
mysql 5.7 8cf625070931 10 days ago 448MB
centos 7 8652b9f0cb4c 8 months ago 204MB
7. 创建所有的容器
# 创建Mysql容器
[root@docker01 php]# docker run -d -e MYSQL_ROOT_PASSWORD=123 -e MYSQL_DATABASE=wordpress --network lnmp --name mysql mysql:5.7
# 创建Nginx容器
[root@docker01 php]# docker run -d -v /tmp/html:/usr/share/nginx/html --network lnmp --name php lnmp_php
# 创建PHP容器
[root@docker01 php]# docker run -d -p 80:80 -v /tmp/html/:/usr/share/nginx/html --network lnmp --name nginx lnmp_nginx
# 查看所有正在运行的容器
[root@docker01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f5ef77c4dfb9 lnmp_nginx "/bin/sh -c 'nginx -…" 43 minutes ago Up 43 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
85a54baaf950 lnmp_php "/bin/sh -c 'php-fpm…" 43 minutes ago Up 43 minutes 9000/tcp php
cb5a1d14d1e8 mysql:5.7 "docker-entrypoint.s…" 43 minutes ago Up 43 minutes 3306/tcp, 33060/tcp mysql
8. 根据浏览器图形界面提示,创建wp-config.php文件
13. 打开浏览器输入ip: 192.168.15.80
步骤如图所示
14. 创建浏览器指定的wp-config.php文件
[root@docker01 ~]# cd /tmp/html/
# 将浏览器生成的配置文件内容复制到wp-conf.php文件中
[root@docker01 html]# vim wp-config.php
以上是关于自定义Dockerfile 搭建LNMP架构的主要内容,如果未能解决你的问题,请参考以下文章