-1

My problem is as the title says, whatever change i make in a file the browser won't display it if i'm not rebuilding the image.I used sh to watch if the changes do exist inside the container and yes, they do.

I'm using 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

Here is my php service 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"]

It might not be a problem with docker, since i have basic understanding of web servers(nginx), php fpm, opcache one of those might create the problem so i'll post other conf files:

default.conf for nginx:

# ./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
Petru Lebada
  • 2,167
  • 8
  • 38
  • 59

1 Answers1

1

You are mapping the app volume to NGINX but you are not serving files from there. This line fastcgi_pass PHP:9000 in your default.conf means that all traffic is (correctly) redirect to the PHP container.

Since you are serving files from your PHP container, that is where your volume should be mapped instead of copied (COPY app /usr/src/app).

Can you remove the (COPY ...) line from the php Dockerfile and then try this 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
Amin.MasterkinG
  • 805
  • 1
  • 12
  • 23
Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Hey @Mihai, yes it worked, but i'm a bit confused... When i started learning about docker i decided i wanna build a symfony app in it so i came across this tutorial: https://knplabs.com/en/blog/how-to-dockerise-a-symfony-4-project , thats why i mounted the volume in nginx. Was i missing something? is the tutorial wrong? and what do u mean by: 'im not serving files from there', i thought the web browser is serving the files when a user does a request... – Petru Lebada May 01 '19 at 13:39
  • That tutorial is a bit confusing for me as well... As for NGINX, it does serve files to the user but what changes is where it gets them from: it can get them from its own folders or it can proxy the request to a different server (like in your case) then get the response and simply pass it back to the user who requested it in the first place. In the tutorial you mention the volumes are mapped both in the php and in the nginx container and that makes is a bit confusing in my opinion. – Mihai May 01 '19 at 13:50
  • oh and about the COPY line inside DockerFile, it doesn't work without it, it cant build the php image because i got a composer install RUN there and it says it cant find a composer.json file .... idk why he cant find it since i mounted the volume where my app surely have a composer.json... – Petru Lebada May 01 '19 at 14:02
  • 1
    That RUN command is commented out in your post ;) But to explain what happens: The volume is mapped at runtime (inside the container). Building an image is kind of a compile time and then the volume is not available. That is why you need the COPY command. Also be aware that the volume mapping will overwrite what you copied inside the container at build time. So if you see weird behaviour it could be that. – Mihai May 01 '19 at 14:09
  • yeah sorry about that, i was playing around trying to figure the main problem and i forgot to remove the #. Thanks a lot for the help and while i did understand your explanation for this particular issue, i fear i don't understand enough of docker to solve other problems that might appear, back to school i guess :D – Petru Lebada May 01 '19 at 14:16
  • 1
    @PetruLebada Learn to walk before running ;) Start playing with 1 container until you understand the concepts. You started already with 3 containers and that makes it difficult. In any case, see you around then :) Good luck – Mihai May 01 '19 at 14:19