用容器构建wordpress项目
#准备两个镜像
#数据库和centos
docker pull mysql:5.7
docker pull centos:7
#创建一个wordpress文件夹,在wordpress文件里面写一个Dockerfile文件
vim Dockerfile
FROM centos:7 #基于centos环境
RUN yum -y install epel-release ;\ #安装扩展源
yum -y install nginx ;\ #安装gnix和php
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm ; \
yum install php80-php-xsl php80-php php80-php-cli php80-php-devel php80-php-gd php80-php-pdo php80-php-mysql php80-php-fpm -y
COPY nginx.conf /etc/nginx/nginx.conf #把nginx的配置文件拷贝的镜像里面
COPY ./start.sh / #拷贝启动php和nginx的脚本到容器里面
ADD ./wordpress-6.5.3-zh_CN.tar.gz /usr/share/nginx/ #拷贝nginx的项目到nginx的发布目录下,不用解压ADD有自动解压的功能
EXPOSE 80 #容器暴露的端口
VOLUME /usr/share/nginx/wordpress
CMD ["sh","/start.sh"]
#写完Dockerfile文件后,构建镜像
docker build -t wordpress-centos:1.0 .
启动脚本文件
#!/bin/bash
chmod 777 -R /usr/share/nginx/wordpress
/opt/remi/php80/root/usr/sbin/php-fpm && \
echo "php启动成功"
nginx && \
echo "nginx启动成功"
tail -f /var/log/nginx/*
nginx的配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name www.itnzs.top;
location / {
root /usr/share/nginx/wordpress;
index index.php;
}
location ~ \.php$ {
root /usr/share/nginx/wordpress; #指定网站目录
fastcgi_pass 127.0.0.1:9000; #指定访问地址
fastcgi_index index.php; #指定默认文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
include fastcgi_params; #包含nginx常量定义
}
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
启动容器
#启动mysql容器
docker run -itd -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress mysql:5.7 #在启动容器的时候创建wordpress库和用户密码
#启动wordpress-centos镜像
docker run -it -p 80:80 --link=gracious_raman:mysql.host wordpress-cenos:3.0 #--link是用来连接两个容器,:前面是源容器,后面是接受容器 -p 是做端口映射
测试
用docker compose编排部署wordpress
#创建一个wordpress-compose目录,把前面的wordpress目录移动到此目录下
#在wordpress-compose下创建一个docker-compose.yml文件
vim docker-compose.yml
services:
centos: #要用到的镜像
hostname: wordpress-centos #容器里面的主机名
container_name: wordpress-centos #容器名
build: #构建
context: ./wordpress #dockerfile文件在当前目录的wordpress目录下
ports: #暴露的端口
- "80:80"
- "443:443"
links: #主节解析
- "database:mysql.host"
depends_on: #这个镜像的启动依赖于
- database
database: #直接使用的镜像,不用构建
image: mysql:5.7
hostname: wordpress-database
container_name: wordpress-database
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=wordpress
volumes:
- /opt/wordpress-data:/var/lib/mysql:rw
docker compose up #构建启动容器
DATABASE=wordpress
volumes:
- /opt/wordpress-data:/var/lib/mysql:rw
docker compose up #构建启动容器
[外链图片转存中...(img-kQTNI6mw-1717159551293)]
![image-20240531204417403](https://img-blog.csdnimg.cn/img_convert/5415227924a407104d7b56783aeca4cc.png)