首页 > 解决方案 > 卷文件更改不会在浏览器中传播

问题描述

我的问题是正如标题所说,如果我不重建图像,无论我在文件中进行什么更改,浏览器都不会显示它。我使用 sh 来观察容器内是否确实存在更改,是的,它们确实存在。

我正在使用 docker-compose:

version: '3.7'
services:
  postgres:
    image: postgres:alpine
    restart: always
    environment:
      POSTGRES_DB: db
      POSTGRES_USER: root
      POSTGRES_PASSWORD: pw123
  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: always
    env_file:
      - ./app/.env
    user: 1000:1000
  nginx:
    image: nginx:1.15.3-alpine
    restart: always
    volumes:
      - ./app:/usr/src/app
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - 80:80
    depends_on:
      - php

这是我的 php 服务 dockerfile:

# ./docker/php/Dockerfile
FROM php:7-fpm-alpine

RUN apk update && apk add build-base

RUN apk add postgresql postgresql-dev \
  && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pdo pdo_pgsql pgsql

#RUN docker-php-ext-install opcache

#RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
#&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
# Make sure we're installing what we think we're installing!
#&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
#&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --snapshot \
#&& rm -f /tmp/composer-setup.*

COPY docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini

COPY app /usr/src/app

WORKDIR /usr/src/app
#install usermod
#RUN echo http://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories
#RUN apk --no-cache add shadow && usermod -u 1000 www-data

#RUN composer install --no-plugins --no-scripts

#ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}"

#RUN usermod -u 1000 www-data
#RUN chown -R www-data:www-data /var/cache
#RUN chown -R www-data:www-data /var/log
EXPOSE 9000
CMD ["php-fpm","--nodaemonize"]

这可能不是 docker 的问题,因为我对 web 服务器(nginx)、php fpm、opcache 有基本的了解,其中之一可能会产生问题,所以我将发布其他 conf 文件:

nginx 的 default.conf:

# ./docker/nginx/default.conf
server {
 server_name ~.*;

 location / {
     root /usr/src/app;

     try_files $uri /index.php$is_args$args;
 }

 location ~ ^/index\.php(/|$) {
     client_max_body_size 50m;

     fastcgi_pass php:9000;
     fastcgi_buffers 16 16k;
     fastcgi_buffer_size 32k;
     include fastcgi_params;
     fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
 }

 error_log /dev/stderr debug;
 access_log /dev/stdout;
}

opcache.ini:

[opcache]
opcache.enable=1
; 0 means it will check on every request
; 0 is irrelevant if opcache.validate_timestamps=0 which is desirable in production
opcache.revalidate_freq=0
opcache.validate_timestamps=1
opcache.max_accelerated_files=10000
opcache.memory_consumption=192
opcache.max_wasted_percentage=10
opcache.interned_strings_buffer=16
opcache.fast_shutdown=1

标签: phpdockernginxdocker-composedocker-container

解决方案


您正在将应用程序卷映射到 NGINX,但您没有从那里提供文件。default.conf 中的这一行fastcgi_pass PHP:9000意味着所有流量都(正确地)重定向到 PHP 容器。

由于您从 PHP 容器提供文件,因此应该映射而​​不是复制卷(COPY app /usr/src/app)。

你能从 php Dockerfile 中删除 (COPY ...) 行,然后试试这个docker-compose

version: '3.7'
services:
  postgres:
    image: postgres:alpine
    restart: always
    environment:
      POSTGRES_DB: db
      POSTGRES_USER: root
      POSTGRES_PASSWORD: pw123
  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: always
    env_file:
      - ./app/.env
    volumes:
      - ./app:/usr/src/app
    user: 1000:1000
  nginx:
    image: nginx:1.15.3-alpine
    restart: always
    volumes:
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - 80:80
    depends_on:
      - php

推荐阅读