【云原生】Dockerfile制作WordPress镜像,实现Compose + K8s编排部署(上)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 【云原生】Dockerfile制作WordPress镜像,实现Compose + K8s编排部署

关于作者


大家好,我是秋意零。

😈 CSDN作者主页

👿 简介

  • 👻 普通本科生在读
  • 在校期间参与众多计算机相关比赛,如:🌟 “省赛”、“国赛”,斩获多项奖项荣誉证书
  • 🔥 各个平台,秋意临 账号创作者
  • 🔥 云社区 创建者
点赞、收藏+关注下次不迷路!

欢迎加入云社区


前言

今天给各位带来一个出色网站、博客系统 WordPress,不过不使用 Docker Hub 提供的 WordPress Docker镜像,我们使用 Dockerfile 自己制作,实现 LNMP WordPress 运行环境,并将 WordPress 部署再其基础之上

为什么不使用 Docker Hub 提供的 WordPress 镜像部署呢?

环境准备

  • Linux 7.5
  • docker v23.0.1
  • docker compose v2.17.0
  • WordPress v6.2

注意:这里的环境是博主使用环境,不限于此

新手小白教程

Centos7.5安装教程

Docker安装教程

Docker-Compose安装教程

目录结构

[root@master01 ~]# tree docker
docker
├── db.sh   #数据库启动、配置脚本
├── default.conf  #nginx配置文件,配置支持 php 
├── docker-compose.yaml  # compose 文件
├── Dockerfile-mariadb  # maraidb dockerfile文件
├── Dockerfile-service  # nginx+php+wordpress dockerfile文件
├── wordpress-6.2-zh_CN.zip  # wordpress安装包
├── wp-config.php  # wordpress配置文件,这里主要配置数据库部分
└── yum.sh  #yum源配置脚本
0 directories, 8 files

dockerfile制作镜像

yum 脚本

yum脚本是两个 dockerfile 文件公用的脚本,因为这里都是使用 yum 安装的服务

# 清除默认yum
rm -rf /etc/yum.repos.d/*
# 阿里云 centos7 yum
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#nginx yum
cat  > /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx
baseurl=https://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF
#mariadb yum
cat > /etc/yum.repos.d/mariadb.repo <<EOF
[mariadb]
name=mariadb
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.5.19/yum/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF
#php yum
yum -y install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Dockerfile-mariadb 镜像

yum.sh、db.sh 是 Dockerfile-mariadb 构建镜像时所需要的文件

db.sh 启动配置脚本

db.sh 是数据库启动、设置密码、创建数据库以及授权的脚本

cat > db.sh << EOF
#!/bin/bash
mysql_install_db --user=root
mysqld_safe --user=root &
sleep 3
mysqladmin -u root password '000000'
mysql -uroot -p000000 -e "create database wordpress;"
mysql -uroot -p000000 -e "grant all on wordpress.* to root@localhost identified by '000000';"
mysql -uroot -p000000 -e "grant all on wordpress.* to root@'%' identified by '000000';"
EOF

Dockerfile-mariadb

cat > Dockerfile-mariadb << EOF
FROM centos:centos7.9.2009
MAINTAINER qyl
COPY yum.sh /opt/
COPY db.sh /opt
RUN sh /opt/yum.sh && yum clean all
RUN yum install -y mariadb-server
RUN sh /opt/db.sh
EXPOSE 3306
CMD ["mysqld_safe","--user=root"]
EOF

构建镜像

docker build -t wp-mariadb:v1 -f Dockerfile-mariadb .

Dockerfile-service 镜像

yum.sh、default.conf 、wp-config.php 是 Dockerfile-service 构建镜像时所需要的文件

default.conf

这是配置 nginx 能代理 php 网页的配置

cat  > default.conf << EOF
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm; #修改部分
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           /usr/share/nginx/html;  #修改部分
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #修改部分
        include        fastcgi_params;
    }
}
EOF

wp-config.php

在安装 wordpress 时 wordpress 配置文件无法自动写入时,使用这种方式手动写入(主要配置数据库部分)

注意:数据库部分的配置

cat > wp-config.php << EOF
<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the installation.
 * You don't have to use the web site, you can copy this file to "wp-config.php"
 * and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * Database settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/documentation/article/editing-wp-config-php/
 *
 * @package WordPress
 */
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' ); # 根据自己数据库修改
/** Database username */
define( 'DB_USER', 'root' );  # 根据自己数据库修改
/** Database password */
define( 'DB_PASSWORD', '000000' ); # 根据自己数据库修改
/** Database hostname */
define( 'DB_HOST', 'mariadb:3306' );  # 这个 mariadb 对应 compose 里面的服务名
/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );
/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
/**#@+
 * Authentication unique keys and salts.
 *
 * Change these to different unique phrases! You can generate these using
 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
 *
 * You can change these at any point in time to invalidate all existing cookies.
 * This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );
/**#@-*/
/**
 * WordPress database table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';
/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/documentation/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );
/* Add any custom values between this line and the "stop editing" line. */
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOF


相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
1天前
|
Kubernetes Cloud Native Shell
云原生 - K8s命令合集
云原生 - K8s命令合集
6 0
|
1天前
|
存储 Kubernetes Cloud Native
云原生 - Kubernetes基础知识学习
云原生 - Kubernetes基础知识学习
5 0
|
7天前
|
存储 Kubernetes 安全
云上攻防-云原生篇&K8s安全&Config泄漏&Etcd存储&Dashboard鉴权&Proxy暴露
云上攻防-云原生篇&K8s安全&Config泄漏&Etcd存储&Dashboard鉴权&Proxy暴露
|
7天前
|
Kubernetes 安全 Cloud Native
云上攻防-云原生篇&Kubernetes&K8s安全&API&Kubelet未授权访问&容器执行
云上攻防-云原生篇&Kubernetes&K8s安全&API&Kubelet未授权访问&容器执行
|
8天前
|
运维 Kubernetes Cloud Native
云原生时代的技术革命:Kubernetes与容器编排
【6月更文挑战第17天】在数字化转型的浪潮中,云原生技术正成为推动企业IT架构现代化的核心力量。本文将深入探讨Kubernetes作为云原生生态中的佼佼者,如何引领容器编排的技术革命,并分析其在现代应用部署、管理和扩展中的关键作用。通过实例和案例分析,我们将揭示Kubernetes如何助力企业实现更高效、灵活和可靠的云原生应用管理。
|
26天前
|
存储 Kubernetes 监控
【云原生】Kubernetes----PersistentVolume(PV)与PersistentVolumeClaim(PVC)详解
【云原生】Kubernetes----PersistentVolume(PV)与PersistentVolumeClaim(PVC)详解
|
26天前
|
Kubernetes Cloud Native 开发者
构建高效云原生应用:Kubernetes与微服务架构的融合
【5月更文挑战第31天】 在数字化转型和技术迭代的大潮中,企业对于敏捷、可扩展的IT基础设施需求日益增长。云原生技术以其独特的优势成为推动这一进程的关键力量。本文深入探讨了如何通过结合Kubernetes容器编排和微服务架构来构建和维护高效、可靠的云原生应用。我们将剖析这种技术整合的必要性,揭示其背后的原理,并讨论在实际部署过程中可能遇到的挑战及解决方案。通过案例分析和最佳实践的分享,旨在为开发者和架构师提供一套行之有效的云原生应用构建指南。
|
28天前
|
Kubernetes Cloud Native 开发者
构建高效的云原生应用:Docker与Kubernetes的完美搭档
【5月更文挑战第29天】 在现代软件开发领域,"云原生"这一术语已经成为高效、可扩展和弹性的代名词。本文将深入探讨如何通过Docker容器化技术和Kubernetes集群管理工具实现云原生应用的构建和管理。我们将剖析Docker的核心原理,揭示其轻量级和易于部署的特点,并进一步探索Kubernetes如何为这些容器提供编排,保证应用的高可用性与自动扩缩容。文章不仅讨论了二者的技术细节,还提供了实践案例,帮助开发者理解并运用这些技术构建和维护自己的云原生应用。
|
28天前
|
域名解析 Kubernetes 网络协议
【域名解析DNS专栏】云原生环境下的DNS服务:Kubernetes中的DNS解析
【5月更文挑战第29天】本文探讨了Kubernetes中的DNS解析机制,解释了DNS如何将服务名转换为网络地址,促进集群内服务通信。Kubernetes使用kube-dns或CoreDNS作为内置DNS服务器,每个Service自动分配Cluster IP和DNS条目。通过示例展示了创建Service和使用DNS访问的流程,并提出了优化DNS解析的策略,包括使用高性能DNS解析器、启用DNS缓存及监控日志,以实现更高效、可靠的DNS服务。
|
29天前
|
Kubernetes Cloud Native PHP
构建高效云原生应用:基于Kubernetes的微服务治理实践深入理解PHP中的命名空间
【5月更文挑战第28天】 在当今数字化转型的浪潮中,云原生技术以其独特的弹性、可扩展性和敏捷性成为了企业IT架构的重要选择。本文深入探讨了如何在云平台之上,利用Kubernetes这一容器编排工具,实现微服务架构的有效治理。通过分析微服务设计原则与Kubernetes特性的融合,提出了一套系统的微服务部署、监控和管理策略。文章不仅阐述了关键技术点,还提供了具体实施步骤和最佳实践,以期帮助企业构建出既高效又稳定的云原生应用。 【5月更文挑战第28天】在PHP的编程世界中,命名空间是管理代码和避免名称冲突的强大工具。本文将探讨PHP命名空间的核心概念、实现方式及其在现代PHP开发中的应用。通过深

热门文章

最新文章