1、dockerfile部署lnmp
mkdir /opt/lnmp
cd /opt/lnmp
mkdir nginx mysql php
docker network create --subnet=20.0.0.0/24 lnmp-net
将wordpress文件夹拷贝到nginx、php文件夹
/opt/nginx/Dockerfile:
# 使用官方的nginx镜像作为基础镜像
FROM nginx:latest# 复制默认配置文件
COPY ./nginx.conf /etc/nginx/nginx.conf# 复制静态文件到容器中
COPY ./wordpress /var/www/html
# 指定容器启动时执行的命令
CMD ["nginx", "-g", "daemon off;"]
# 暴露容器的端口
EXPOSE 80
/opt/lnmp/nginx/nginx.conf:
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;events {worker_connections 1024;
}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"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;location / {root /var/www/html;index index.php index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000location ~ \.php$ {root /var/www/html;fastcgi_pass php:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}
}
/opt/lnmp/mysql/Dockerfile:
# 使用官方的mysql/mysql-server镜像
FROM mysql:5.7# 设置MYSQL_ROOT_PASSWORD
ENV MYSQL_ROOT_PASSWORD=Admin!123# 复制数据初始化脚本
COPY ./init.sql /docker-entrypoint-initdb.d/
EXPOSE 3306
/opt/lnmp/mysql/init.sql
-- 创建数据库
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by 'Admin!123';
flush privileges;
/opt/lnmp/php/Dockerfile
# 使用官方的php:fpm镜像
FROM php:7.4-fpm# 安装必要的扩展
RUN docker-php-ext-install mysqli pdo_mysql# 设置时区
RUN echo 'Asia/Shanghai' > /etc/timezone && \dpkg-reconfigure --frontend noninteractive tzdataCOPY ./wordpress /var/www/html# 设置工作目录
WORKDIR /var/www/html
# 暴露端口9000
EXPOSE 9000
cd /opt/lnmp
docker build -t my-php -f ./php/Dockerfile ./php
docker build -t my-mysql -f ./mysql/Dockerfile ./mysql
docker build -t my-nginx -f ./nginx/Dockerfile ./nginx
docker run -d --name mysql --network lnmp-net --ip 20.0.0.3 my-mysql
docker run -d --name php --network lnmp-net --ip 20.0.0.4 -p 9000:9000 my-php
docker run -d --name nginx --network lnmp-net --ip 20.0.0.2 -p 80:80 my-nginx
2、docker-compose构建nginx镜像和容器
mkdir -p /opt/compose_nginx/wwwroot/
echo "<h1>this is test web</h1>" > /opt/compose_nginx/wwwroot/index.html
docker-compose.yml
version: '3'
services:nginx:container_name: web1hostname: nginxbuild:context: ./nginxdockerfile: Dockerfileports:- 1216:80- 1217:443networks:lnmp:ipv4_address: 172.20.0.10volumes:- ./wwwroot:/usr/local/nginx/html
networks:lnmp:driver: bridgeipam:config:- subnet: 172.20.0.0/16
/opt/compose_nginx/nginx/Dockerfile
FROM centosRUN sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*RUN yum clean all && yum makecache
RUN yum install -y nginxCMD ["nginx", "-g", "daemon off;"]
# 暴露容器的端口
EXPOSE 80
cd /opt/compose_nginx/
docker-compose -f docker-compose.yml up -d