由于各种原因(主要是我很懒)我想将我的wordpress文件挂载到/var/www/html/blog而不是/var/www/html,然后使用sidecar模式让nginx和wordpress-fpm 共享一个目录。我将一个空目录安装到/var/www/html ,我希望它是空的(废话!),然后将我的文件复制到/var/www/html/blog
我的 Dockerfile:
FROM wordpress:5.7.2-fpm-alpine
LABEL author="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9dec8d0c7cce9878787cac687dcc2" rel="noreferrer noopener nofollow">[email protected]</a>"
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
An emptyDir volume is first created when a Pod is assigned to a node, and exists as long as that Pod is running on that node. As the name says, the emptyDir volume is initially empty. All containers in the Pod can read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each container. When a Pod is removed from a node for any reason, the data in the emptyDir is deleted permanently.
最后我的问题是……这里发生了什么???
最佳答案
您从 Docker Hub wordpress
image 开始,其中有 its Docker image setup in GitHub .
重要的细节是 Dockerfile结尾为
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["php-fpm"]
这是使用 shell 脚本作为包装器进行首次设置并为其提供实际运行命令的标准模式 (Docker passes the CMD
as arguments to the ENTRYPOINT
)。 Wordpress image docker-entrypoint.sh
依次有片段:
if [ ! -e index.php ] && [ ! -e wp-includes/version.php ]; then
echo >&2 "WordPress not found in $PWD - copying now..."
...
for contentPath in \
/usr/src/wordpress/.htaccess \
/usr/src/wordpress/wp-content/*/*/ \
; do
...
done
tar cf - ... . | tar xf -
fi
该片段查看当前目录;如果它没有 index.php
文件,它会复制 /usr/src/wordpress
到那里。它在容器启动时、安装任何卷之后以及 postStart
Hook 触发之前运行。
您可以通过将自己的内容复制到 Wordpress 基础树中来利用此设置,而不是设置单独的 Hook :
FROM wordpress:5.7.2-fpm-alpine
COPY public/wordpress /usr/src/wordpress/blog
关于kubernetes - 为什么挂载到 dockerfile 卷时,emptydir 不为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68015436/