简体   繁体   English

仅当容器重新启动时,本地更改才会传播到Docker容器

[英]Local changes propagate to docker container only when container is restarted

EDIT 编辑

I have created a repo for setting up WP with Docker on PHP7.3, latest WordPress on php-fpm and I get the same issue: https://github.com/dingo-d/wordpress-docker 我已经创建了一个仓库来在php7.3上使用Docker设置WP,在php-fpm上使用最新的WordPress来创建WP,并且遇到了相同的问题: https : //github.com/dingo-d/wordpress-docker

You can check it out to see what could be an issue. 您可以查看一下,看看可能是个问题。 My guess is something with PHP-FPM has to be the issue (since restarting that container makes the changes propagate). 我的猜测是PHP-FPM必须解决的问题(因为重新启动该容器会使更改传播)。


I have created a WordPress docker local dev environment by following the tutorial I found here . 我按照此处找到的教程创建了WordPress docker本地开发环境。

Basically, all my docker stuff is located in the .docker folder. 基本上,我所有的.docker东西都位于.docker文件夹中。

I have these files in the .docker folder 我在.docker文件夹中有这些文件

|--project-folder
|____.docker
| |____php-fpm
| | |____php.ini
| | |____Dockerfile
| |____nginx
| | |____Dockerfile
| | |____logs
| | | |____error.log
| | | |____access.log
| | |____certs
| | | |____dev.project.com.key
| | | |____dev.project.com.crt
| | |____scripts
| | | |____docker-nginx-entrypoint.sh
| | |____nginx.conf
| |____.dockerignore
| |____.env
| |____docker-compose.yml
| |____.env.example

After trying to set everything up with the self signed SSL certificates, I can log in to https://localhost:8443 (I have some issues with setting the hosts file so that I can go to https://dev.project.com but that's another issue). 尝试使用自签名SSL证书设置所有内容后,我可以登录https://localhost:8443 (设置主机文件时https://localhost:8443一些问题,以便可以转到https://dev.project.com但这是另一个问题)。

The problem is that when I go and change something in my theme, say add a print_r statement that should just print something out, I can see that change in the WP admin only after I restart my WordPress app container. 问题是,当我去更改主题中的内容时,比如说添加一条print_r语句,该语句应只打印出一些内容,只有重启我的WordPress应用程序容器后,我才能在WP管理员中看到该更改。 The folders are mapped, and when I change things locally and exec to container, those changes are there immediately (and when I delete them while exec'd in the container they get deleted in my editor immediately). 这些文件夹已映射,当我在本地更改内容并执行到容器时,这些更改将立即存在(当我在容器中执行时删除它们时,它们会立即在编辑器中删除)。

I've tested if the same thing will happen with a super simple WordPress Docker setup I found online 我测试了在网上找到的超级简单的WordPress Docker设置是否会发生同样的事情

version: '3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: password
       MYSQL_DATABASE: wordpress
       MYSQL_USER: user
       MYSQL_PASSWORD: password

   app:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     volumes:
       - ./wp-content/:/var/www/html/wp-content
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: user
       WORDPRESS_DB_PASSWORD: password
volumes:
    db_data:

And when I start this up, and go to localhost:8000 I can see the changes immediately without restarting the container (when I edit files in the editor). 当我启动它并转到localhost:8000无需更改容器即可立即看到更改(在编辑器中编辑文件时)。

My docker-compose.yml looks like this: docker-compose.yml看起来像这样:

version: '3.7'

services:

  wordpress:
    build:
      context: ..
      dockerfile: .docker/php-fpm/Dockerfile
      args:
        WP_VERSION: ${WP_VERSION}
    container_name: dev-project-wp
    working_dir: /var/www/html
    tty: true
    depends_on:
      - database
    volumes:
      - ../:/var/www/html
      - ./php-fpm/php.ini:/usr/local/etc/php/conf.d/local.ini
    environment:
      DB_PORT: ${DB_PORT}
      DB_HOST: ${DB_HOST}
      DB_NAME: ${DB_NAME}
      DB_USER: ${DB_USER}
      DB_PASSWORD: ${DB_PASSWORD}
      AUTH_KEY: ${AUTH_KEY}
      SECURE_AUTH_KEY: ${SECURE_AUTH_KEY}
      LOGGED_IN_KEY: ${LOGGED_IN_KEY}
      NONCE_KEY: ${NONCE_KEY}
      AUTH_SALT: ${AUTH_SALT}
      SECURE_AUTH_SALT: ${SECURE_AUTH_SALT}
      LOGGED_IN_SALT: ${LOGGED_IN_SALT}
      NONCE_SALT: ${NONCE_SALT}
      DB_PREFIX: ${DB_PREFIX}
      WP_VERSION: ${WP_VERSION}
    ports:
      - '9000'
    expose:
      - '80'

  nginx:
    build:
      context: ..
      dockerfile: .docker/nginx/Dockerfile
      args:
        DOCKER_IMAGE_NAME_PHP: 'docker_wordpress'
    container_name: dev-project-nginx
    working_dir: /var/www/html
    restart: always
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/certs/dev.project.com.crt:/etc/nginx/dev.project.com.crt
      - ./nginx/certs/dev.project.com.key:/etc/nginx/dev.project.com.key
      - ../:/var/www/html
      - ./nginx/logs:/var/log/nginx
    depends_on:
      - wordpress
    ports:
      - "${NGINX_HOST_HTTP_PORT}:80"
      - "${NGINX_HOST_HTTPS_PORT}:443"

  database:
    image: mariadb:10.3
    volumes:
      - projectdb:/var/lib/mysql
    restart: always
    container_name: ${DB_HOST}
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASSWORD}
    ports:
      - "${DB_HOST_PORT}:${DB_PORT}"

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: dev-project-phpmyadmin
    external_links:
      - database
    depends_on:
      - database
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_USERNAME: root
      PMA_HOST: ${DB_HOST}
    ports:
      - "${PHPMYADMIN_HOST_PORT}:80"

  mailhog:
    image: mailhog/mailhog
    container_name: dev-project-mailhog
    ports:
      - "${MAILHOG_HOST_PORT_SMTP}:1025"
      - "${MAILHOG_HOST_PORT_WEB}:8025"

volumes:
  projectdb:

I have an .env that looks like this: 我有一个.env看起来像这样:

# required so we can reach the nginx server from other containers via that hostname
APP_HOST=dev.project.com

# nginx
NGINX_HOST_HTTP_PORT=8180
NGINX_HOST_HTTPS_PORT=8443

# database
DB_HOST_PORT=33060
DB_PORT=3306
DB_HOST=dev-project-db
DB_NAME=docker-project
DB_USER=wp
DB_PASSWORD=wp

#phpmyadmin
PHPMYADMIN_HOST_PORT=8088

# wordpress - https://api.wordpress.org/secret-key/1.1/salt/
AUTH_KEY=':1k7<tW.#pE-O%*nZv7qM@me.#PLE;7).#g<4_.]04,2cM|]:*r8|:osljhB]s*.'
SECURE_AUTH_KEY='N~?~Z0(ijZS%|cHe#~F!O.31N#;VQSI~QBL%~oWZFGfU6R`%k#(eD)2Mcm}wLh0a'
LOGGED_IN_KEY='y7T8hoW|Ik4eBUGWUs6j~O*j)k{hrZ`E.ujW+Za{`WPn9Xk.&g]*F(HsV~q0fL8g'
NONCE_KEY='V0aau(w+|CAW_.+ilIkYaIh]8Bz}@,DdX@yBi+!dD5Zy:,YO+<CF+oYwP+~jYE,r'
AUTH_SALT='_zQ C^rzH%wBmmyjO,KH`J-EIZm$.MIzK[b(ar2+TgO=P&hHQ7d*lPsd8*+xu{4u'
SECURE_AUTH_SALT='EL~r.88e=TYM>W&LP]BI(u_f,PLQY|m%+2(2TF%,|S,Wc4uYV)hVBpZ .KA$cGhY'
LOGGED_IN_SALT='hEoqqkkJO~f`|p~43>gZx$;u&% {qJLe$OnreM,dfR`H?an+q3g`&9>?-v3iSoJ&'
NONCE_SALT='jfEVaR]Od2,yDPN|$o+g7Hd=XIwM,ow#a,,u|~d+pf/<T#NBcm(u9v?qpr#g^q5k'

DB_PREFIX=wp_
WP_VERSION=5.2.2

# mailhog
MAILHOG_HOST_PORT_SMTP=1028
MAILHOG_HOST_PORT_WEB=8028

My php dockerfile looks like this 我的php dockerfile看起来像这样

ARG WP_VERSION

# Stage 0, build app
FROM php:7.3-fpm-stretch as build-container

RUN curl -sS https://getcomposer.org/installer | php \
  && chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apt-get update && apt-get install -y gnupg
RUN curl -sL https://deb.nodesource.com/setup_11.x | bash - && \
    apt-get install -yq nodejs build-essential \
        git unzip \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        pkg-config \
        libmcrypt-dev \
        libpng-dev \
    && pecl install mcrypt-1.0.2 \
    && docker-php-ext-enable mcrypt \
    && docker-php-ext-install bcmath \
    && docker-php-ext-install -j$(nproc) mysqli \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

RUN npm install -g npm

WORKDIR /

WORKDIR /build
COPY . /build

RUN cp /build/wp-config.php.template /build/wp-config.php
# RUN bash /build/scripts/build-plugins.sh

# Stage 2, build app container
FROM php:7.3-fpm-stretch
ARG WP_VERSION

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        libzip-dev \
        unzip \
        mariadb-client \
        libmagickwand-dev \
    && docker-php-ext-configure gd --with-png-dir=/usr/include/ --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) \
        bcmath \
        exif \
        gd \
        mysqli \
        opcache \
        zip \
        pdo \
        pdo_mysql \
        mysqli \
    && docker-php-ext-install -j$(nproc) iconv \
    && export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \
    && rm -rf /var/lib/apt/lists/* \
    && pecl install imagick-3.4.4 \
    && docker-php-ext-enable imagick

ADD https://downloads.wordpress.org/release/wordpress-$WP_VERSION-no-content.zip /var/www/latest.zip
RUN cd /var/www && unzip latest.zip && rm latest.zip
RUN rm -rf /var/www/html
RUN mkdir -p /var/www/html \
    && mv /var/www/wordpress/* /var/www/html/

# Copy wp files
COPY --from=build-container /build/ /var/www/html/
RUN chown www-data:www-data /var/www/html/ -R
COPY .docker/php-fpm/php.ini /usr/local/etc/php/
WORKDIR /var/www/html/

CMD ["php-fpm"]

And the dockerfile for nginx looks like this Nginx的dockerfile看起来像这样

ARG DOCKER_IMAGE_NAME_PHP
FROM $DOCKER_IMAGE_NAME_PHP as php-image

FROM nginx:latest

COPY .docker/nginx/scripts/docker-nginx-entrypoint.sh /docker-nginx-entrypoint.sh
COPY .docker/nginx/nginx.conf /opt/nginx.conf

COPY --from=php-image /var/www/html/ /var/www/html/

CMD ["/bin/bash","/docker-nginx-entrypoint.sh"]

The nginx.conf looks like this: nginx.conf看起来像这样:

worker_processes auto;

events {
  worker_connections  2048;
}


http {

  include       mime.types;

  index index.php index.html index.htm;

  server {
    listen 80;
    listen [::]:80;

    listen 443 ssl;
    listen [::]:443 ssl;

    server_name dev.project.com;

    ssl_certificate /etc/nginx/dev.project.com.crt;
    ssl_certificate_key /etc/nginx/dev.project.com.key;

    root /var/www/html;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    client_max_body_size 128M;

    location = /favicon.ico {
      log_not_found off;
      access_log off;
    }

    location / {
      try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_intercept_errors on;
      fastcgi_pass wordpress:9000;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_param QUERY_STRING    $query_string;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
      expires max;
      log_not_found off;
    }
  }
}

And the docker-nginx-entrypoint.sh script looks like this docker-nginx-entrypoint.sh脚本看起来像这样

#!/bin/bash
set -e

cp /opt/nginx.conf /etc/nginx/conf.d/default.conf

exec nginx -g "daemon off;"

To start the docker I use 要启动我使用的docker

docker-compose -f .docker/docker-compose.yml --project-directory .docker up -d --build

I looked at the logs, and there is no error (in the wp container or nginx logs). 我查看了日志,没有错误(在wp容器或nginx日志中)。 The site works fine when I go to https://localhost:8443/wp-admin/ but the changes are propagated only on container reset. 当我转到https://localhost:8443/wp-admin/时,该站点工作正常,但更改仅在重置容器时传播。 And it makes no sense (not to mention that it's basically an unusable dev environment). 而且这没有任何意义(更不用说它基本上是一个无法使用的开发环境)。

PS 聚苯乙烯

This is a bit more complicated setup, if you have any advice on simplifying it, the advice is welcomed. 这有点复杂,如果您有任何简化建议,欢迎您提出建议。

EDIT 编辑

I'm bundling the assets using webpack, and when they are bundled, the changes are visible. 我正在使用webpack捆绑资产,将它们捆绑在一起后,更改便可见。 The changes to the PHP file are not... 对PHP文件的更改不是...

EDIT 2 编辑2

I've used the official images for nginx and for wordpress, and the PHP part is again only changed on restart, so it's not the problem in the Dockerfiles. 我已经将官方图像用于nginx和wordpress,并且PHP部分仅在重新启动时才更改,因此Dockerfile中不是问题。

So it turns out that the culprit was the opcache . 结果证明,罪魁祸首是操作opcache Or rather me finding some example on line and just adding it to my php.ini without trying to understand what this actually means. 或者更确切地说,我在线找到了一些示例,然后将其添加到我的php.ini而没有试图理解这实际上意味着什么。

After reading this article and this excellent article that explains some opcache settings, I realised that my php.ini file had 在阅读了这篇文章以及这篇出色的文章之后,我对php.ini文件进行了介绍

opcache.revalidate_freq=60

Which you shouldn't have on your local development environment! 在本地开发环境中不应该拥有的!

When used with opcache.validate_timestamps , PHP checks the revalidate_freq setting and uses it as a timestamp - if you made a request within 60 seconds (like the above example), the code will be pulled from the opcache - and you won't see any change in your browser. opcache.validate_timestamps使用时,PHP会检查revalidate_freq设置并将其用作时间戳记-如果您在60秒内发出了请求(如上述示例),则代码将从opcache中提取-并且您将看不到任何内容在浏览器中进行更改。 Only after 60 seconds will this change be visible. 只有60秒后,此更改才可见。

So setting it to 0 (checking code on every single request) solves the issue. 因此,将其设置为0(在每个请求中检查代码)就可以解决此问题。

Point of the story: check everything and read about every settings that you use! 故事的重点:检查所有内容并了解您使用的每种设置!

'Unfortunately' for me I was reading the nginx config because I thought I messed something up there 😁 对我来说,“不幸的是”我正在阅读nginx配置,因为我以为我在那里弄乱了东西something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM