自学内容网 自学内容网

Dockerfile制作部署wordpress-6.6

目录

一. 环境准备

二. 准备对应的配置文件

三. 编写Dockerfile

四. 构建镜像

五. 配置MySQL 

六. 安装wordpress 

七. 扩展


一. 环境准备

localhost192.168.226.25

rocky_linux9.4

Docker version 27.0.3

关闭防火墙和selinux,进行时间同步。

安装docker

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
# 验证docker
sudo docker version

下载wordpress上传至/root/目录

发行版本归档 – WordPress.org China 简体中文icon-default.png?t=N7T8https://cn.wordpress.org/download/releases/

[root@localhost ~]# ll
total 26700
-rw-r--r-- 1 root root      846 Jul 20 03:33 Dockerfile
-rw-r--r-- 1 root root      480 Jul 20 03:08 default.conf
-rw-r--r-- 1 root root      398 Jul 20 00:40 nginx.repo
-rw-r--r-- 1 root root      183 Jul 20 11:19 start.sh
-rw-r--r-- 1 root root 27323722 Jul 19 19:12 wordpress-6.6-zh_TW.zip

二. 准备对应的配置文件

nginx官方镜像源文件

[root@localhost ~]# vim /root/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@localhost ~]# vim /root/default.conf 
server {
    listen       80;
    server_name  localhost;


    location / {
        root   /usr/share/nginx/wordpress;
        index  index.php;
    }

        location ~ \.php$ {
            root          /usr/share/nginx/wordpress;
            fastcgi_pass   unix:///var/opt/remi/php80/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/wordpress;
    }
}

三. 编写Dockerfile

[root@localhost ~]# vim /root/Dockerfile
FROM rockylinux:9.3

LABEL \
  version="1.0" \
  description="wordpress" \
  maintainer="zzdict@gmail.com"

WORKDIR /root/
EXPOSE 80 443

# 安装 Remi 仓库和 PHP
RUN yum install -y http://rpms.remirepo.net/enterprise/remi-release-9.rpm && \
    yum install -y epel-release && \
    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
# 复制 nginx repo 配置文件并安装 nginx
COPY nginx.repo /etc/yum.repos.d/nginx.repo
RUN yum install -y nginx

# 复制 nginx 配置文件
COPY default.conf /etc/nginx/conf.d/default.conf

# 复制 WordPress 并设置适当的权限
ADD wordpress-6.6-zh_CN.tar.gz /usr/share/nginx
RUN chown -R nginx:nginx /usr/share/nginx/wordpress

# 复制启动脚本
COPY start.sh /start.sh
RUN chmod +x /start.sh

# 设置启动命令
CMD ["/start.sh"]

创建启动服务的脚本

#!/bin/bash

# 启动 PHP-FPM
/opt/remi/php80/root/usr/sbin/php-fpm
chmod 777 /var/opt/remi/php80/run/php-fpm/www.sock

# 启动 Nginx
nginx -g 'daemon off;'


四. 构建镜像

[root@localhost ~]# docker build -t wordpress:v1 .


[root@localhost ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
wordpress    v1        6f9a0e03d62a   3 minutes ago   891MB


#创建并启动容器
[root@localhost ~]# docker run -dit -P 6f9a0e03d62a
f0cd32e390ab49a671d44aee2c36c695a98ec3d3a62a7f15f48c35a4f26e90af

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS                                                                                NAMES
f0cd32e390ab   6f9a0e03d62a   "/start.sh"   42 seconds ago   Up 42 seconds   0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare

浏览器访问IP+映射的端口

五. 配置MySQL 

[root@localhost ~]# docker pull mysql:8.0

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
wordpress    v1        6f9a0e03d62a   7 hours ago   891MB
mysql        8.0       6c54cbcf775a   2 weeks ago   572MB

[root@localhost ~]# docker run --name mysql_wordpress -e MYSQL_ROOT_PASSWORD=Qaz123456+ -e MYSQL_DATABASE=wordpress -d mysql:8.0


[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                                                                NAMES
5d8b0b70665e   mysql:8.0      "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   3306/tcp, 33060/tcp                                                                  mysql_wordpress
f0cd32e390ab   6f9a0e03d62a   "/start.sh"              7 hours ago     Up 7 hours     0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare

#进入mysql容器验证是否创建成功
[root@localhost ~]# docker exec -it 5d8b0b70665e mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.38 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

按住ctrl + p + q 后台运行当前容器

#查看详细信息,找到IP
[root@localhost ~]# docker inspect 5d8b0b70665e

六. 安装wordpress 

注:如果出现下图中的提示,需要进入nginx的哪个容器里去编写对应的配置

 对应步骤如下:

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                                NAMES
5d8b0b70665e   mysql:8.0      "docker-entrypoint.s…"   11 minutes ago   Up 11 minutes   3306/tcp, 33060/tcp                                                                  mysql_wordpress
f0cd32e390ab   6f9a0e03d62a   "/start.sh"              7 hours ago      Up 7 hours      0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare
[root@localhost ~]# docker exec -it f0cd32e390ab /bin/bash
[root@f0cd32e390ab /]# 

[root@f0cd32e390ab wordpress]# vi /usr/share/nginx/wordpress/wp-config.php
#将页面提示的配置代码全部粘贴并复制到该文件里

然后按ctrl + p +q 后台运行该容即可。

当配置完成后,点击执行安装程序即可。 

七. 扩展

使用--link参数。

当数据库宕机了,我们重启一个数据库,那么容器的IP就会变,因此可以使用--link参数具体操作如下:

#查看镜像ID
[root@localhost ~]# docker images 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
wordpress    v1        6f9a0e03d62a   7 hours ago   891MB
mysql        8.0       6c54cbcf775a   2 weeks ago   572MB

#查看mysql容器名
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                                NAMES
5d8b0b70665e   mysql:8.0      "docker-entrypoint.s…"   32 minutes ago   Up 32 minutes   3306/tcp, 33060/tcp                                                                  mysql_wordpress
f0cd32e390ab   6f9a0e03d62a   "/start.sh"              7 hours ago      Up 7 hours      0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare

#重新创建一个容器,并和mysql容器名进行关联
[root@localhost ~]# docker run -dit -P --link=mysql_wordpress 6f9a0e03d62a
3ea2e9862b9a8b91c8b84e552665968a5ca6c10326ab367bd2bf602639638928


[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                                                                NAMES
3ea2e9862b9a   6f9a0e03d62a   "/start.sh"              About a minute ago   Up About a minute   0.0.0.0:32782->80/tcp, :::32782->80/tcp, 0.0.0.0:32783->443/tcp, :::32783->443/tcp   bold_mclaren
5d8b0b70665e   mysql:8.0      "docker-entrypoint.s…"   34 minutes ago       Up 34 minutes       3306/tcp, 33060/tcp                                                                  mysql_wordpress
f0cd32e390ab   6f9a0e03d62a   "/start.sh"              7 hours ago          Up 7 hours          0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare

查看到了刚创建的容器端口,现在浏览器进行访问IP+映射出来的端口、

这数据库宕了,可以重新起一个数据库容器,但是要求重新起的数据库容器名要和这里填写的mysql_wordpress一致。 

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                                NAMES
3ea2e9862b9a   6f9a0e03d62a   "/start.sh"              9 minutes ago    Up 9 minutes    0.0.0.0:32782->80/tcp, :::32782->80/tcp, 0.0.0.0:32783->443/tcp, :::32783->443/tcp   bold_mclaren
5d8b0b70665e   mysql:8.0      "docker-entrypoint.s…"   42 minutes ago   Up 42 minutes   3306/tcp, 33060/tcp                                                                  mysql_wordpress
f0cd32e390ab   6f9a0e03d62a   "/start.sh"              8 hours ago      Up 8 hours      0.0.0.0:32780->80/tcp, :::32780->80/tcp, 0.0.0.0:32781->443/tcp, :::32781->443/tcp   unruffled_poincare
[root@localhost ~]# docker exec -it 3ea2e9862b9a /bin/bash
[root@3ea2e9862b9a /]# vi /usr/share/nginx/wordpress/wp-config.php
#将页面提示的配置代码全部粘贴并复制到该文件里

然后按ctrl + p +q 后台运行该容即可。

 


原文地址:https://blog.csdn.net/Lzcsfg/article/details/140565989

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!