【发布时间】:2021-09-02 00:38:51
【问题描述】:
出于各种原因(主要是我懒惰)我想将我的 wordpress 文件挂载到 /var/www/html/blog 而不是 /var/www/html 然后使用 sidecar 模式来拥有 nginx 和wordpress-fpm 共享一个目录。我将一个空目录安装到 /var/www/html 我希望它是空的(Duh!),然后将我的文件复制到 /var/www/html/blog
我的 Dockerfile:
FROM wordpress:5.7.2-fpm-alpine
LABEL author="wayne@...co.uk"
COPY public/wordpress /app/blog
Wordpress 的 dockerfile:
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM php:7.4-fpm-alpine
# persistent dependencies
RUN set -eux; \
apk add --no-cache \
# in theory, docker-entrypoint.sh is POSIX-compliant, but priority is a working, consistent image
bash \
# BusyBox sed is not sufficient for some of our sed expressions
sed \
# Ghostscript is required for rendering PDF previews
ghostscript \
# Alpine package for "imagemagick" contains ~120 .so files, see: https://github.com/docker-library/wordpress/pull/497
imagemagick \
;
# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)
RUN set -ex; \
\
apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
freetype-dev \
imagemagick-dev \
libjpeg-turbo-dev \
libpng-dev \
libzip-dev \
; \
\
docker-php-ext-configure gd \
--with-freetype \
--with-jpeg \
; \
docker-php-ext-install -j "$(nproc)" \
bcmath \
exif \
gd \
mysqli \
zip \
; \
pecl install imagick-3.4.4; \
docker-php-ext-enable imagick; \
rm -r /tmp/pear; \
\
runDeps="$( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)"; \
apk add --no-network --virtual .wordpress-phpexts-rundeps $runDeps; \
apk del --no-network .build-deps
# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN set -eux; \
docker-php-ext-enable opcache; \
{ \
echo 'opcache.memory_consumption=128'; \
echo 'opcache.interned_strings_buffer=8'; \
echo 'opcache.max_accelerated_files=4000'; \
echo 'opcache.revalidate_freq=2'; \
echo 'opcache.fast_shutdown=1'; \
} > /usr/local/etc/php/conf.d/opcache-recommended.ini
# https://wordpress.org/support/article/editing-wp-config-php/#configure-error-logging
RUN { \
# https://www.php.net/manual/en/errorfunc.constants.php
# https://github.com/docker-library/wordpress/issues/420#issuecomment-517839670
echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_RECOVERABLE_ERROR'; \
echo 'display_errors = Off'; \
echo 'display_startup_errors = Off'; \
echo 'log_errors = On'; \
echo 'error_log = /dev/stderr'; \
echo 'log_errors_max_len = 1024'; \
echo 'ignore_repeated_errors = On'; \
echo 'ignore_repeated_source = Off'; \
echo 'html_errors = Off'; \
} > /usr/local/etc/php/conf.d/error-logging.ini
RUN set -eux; \
version='5.7.2'; \
sha1='c97c037d942e974eb8524213a505268033aff6c8'; \
\
curl -o wordpress.tar.gz -fL "https://wordpress.org/wordpress-$version.tar.gz"; \
echo "$sha1 *wordpress.tar.gz" | sha1sum -c -; \
\
# upstream tarballs include ./wordpress/ so this gives us /usr/src/wordpress
tar -xzf wordpress.tar.gz -C /usr/src/; \
rm wordpress.tar.gz; \
\
# https://wordpress.org/support/article/htaccess/
[ ! -e /usr/src/wordpress/.htaccess ]; \
{ \
echo '# BEGIN WordPress'; \
echo ''; \
echo 'RewriteEngine On'; \
echo 'RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]'; \
echo 'RewriteBase /'; \
echo 'RewriteRule ^index\.php$ - [L]'; \
echo 'RewriteCond %{REQUEST_FILENAME} !-f'; \
echo 'RewriteCond %{REQUEST_FILENAME} !-d'; \
echo 'RewriteRule . /index.php [L]'; \
echo ''; \
echo '# END WordPress'; \
} > /usr/src/wordpress/.htaccess; \
\
chown -R www-data:www-data /usr/src/wordpress; \
# pre-create wp-content (and single-level children) for folks who want to bind-mount themes, etc so permissions are pre-created properly instead of root:root
# wp-content/cache: https://github.com/docker-library/wordpress/issues/534#issuecomment-705733507
mkdir wp-content; \
for dir in /usr/src/wordpress/wp-content/*/ cache; do \
dir="$(basename "${dir%/}")"; \
mkdir "wp-content/$dir"; \
done; \
chown -R www-data:www-data wp-content; \
chmod -R 777 wp-content
VOLUME /var/www/html
COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["php-fpm"]
我的部署
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
replicas: 1
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
selector:
matchLabels:
app: wordpress
revisionHistoryLimit: 5
template:
metadata:
labels:
app: wordpress
spec:
volumes:
- name: shared
emptyDir: {}
containers:
- name: nginx
image: nginx
volumeMounts:
- name: shared
mountPath: /var/www/html
- name: wordpress
image: redacted/imagename:0.0.1
volumeMounts:
- name: shared
mountPath: /var/www/html
# Important! After this container has started, the PHP files
# in our Docker image aren't in the shared volume.
# If we tried to write directly to this volume from our Docker image
# the files wouldn't appear in the nginx container.
# So, after the container has started, copy the PHP files from this
# container's local filesystem
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "cp -r /app/. /var/www/html"]
imagePullSecrets:
- name: regcred
现在奇怪的地方来了……系好安全带……
当我 kubectl exec 进入容器并列出 /var/www/html 的内容时,我得到:
/var/www/html# ls
blog readme.html wp-blog-header.php wp-content wp-links-opml.php wp-mail.php wp-trackback.php
index.php wp-activate.php wp-comments-post.php wp-cron.php wp-load.php wp-settings.php xmlrpc.php
license.txt wp-admin wp-config-sample.php wp-includes wp-login.php wp-signup.php
有我的博客文件夹,但也有大量的 wordpress 文件,就像它已将文件从 /var/lib/docker/volumes 复制到我的 emptydir... 但这不是文档所说的 emptydir 应该如何工作。文档说:
空目录
emptyDir 卷是在将 Pod 分配给节点时首先创建的,并且只要该 Pod 在该节点上运行就存在。顾名思义,emptyDir 卷最初是空的。 Pod 中的所有容器都可以在 emptyDir 卷中读取和写入相同的文件,尽管该卷可以安装在每个容器中相同或不同的路径上。当 Pod 因任何原因从节点中移除时,emptyDir 中的数据将被永久删除。
所以最后我的问题......那么这里发生了什么???
【问题讨论】:
-
你好@WayneTheisinger。您确定该卷最初不是空的吗?我从您的部署
yaml中看到postStart部分正在将一些文件处理到/var/www/html。因此,emptyDir可能会在安装后立即填充。 -
这是在
/blog文件夹中复制的命令。如果您查看我的 dockerfile,您会看到我从本地复制到图像COPY public/wordpress /app/blog- 但您的评论确实让我想知道docker-entrypoint.sh是否正在移动任何文件...
标签: kubernetes dockerfile