3

I created a multi-container application which is about php. When I modify the php file, I can see the changes in the browser. But, when I modify the static files such as css and js, there are no changes in the browser. The following is my Dockerfile code:

Dockerfile

`FROM nginx:1.8
`ADD default.conf /etc/nginx/conf.d/default.conf
`ADD nginx.conf /etc/nginx/nginx.conf
`WORKDIR /Code/project/
`RUN chmod -R 777 /Code/project/
`VOLUME /Code/project

default.conf

`server {
listen       80;
server_name  localhost;

root   /Code/project/public;
index  index.html index.htm index.php;
#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

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


#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page   500 502 503 504  /50x.html;
#location = /50x.html {
 #   root   /usr/share/nginx/html;
#}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass   fpm:9000;
    #fastcgi_pass   127.0.0.1:9000;   
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
 }

nginx.conf

user  root;
worker_processes  8;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
     worker_connections  1024;
}


http {
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

sendfile        on;
#tcp_nopush     on;

keepalive_timeout  65;
client_max_body_size 20m;

#gzip  on;

include /etc/nginx/conf.d/*.conf;
}`

docker-compose.yml

webphp:
    image: index.alauda.cn/chuanty/local_php
    #image: index.alauda.cn/chuanty/local_nginx:snapshot
    ports:
     - "9000:9000"
volumes:
     - .:/Code/project
links:
     - cache:cache
     - db:db
     - es:localhost
extra_hosts:
     - "chaunty.taurus:192.168.99.100"  

cache:
     image: redis
ports:
     - "6379:6379"
db:
  #image: mysql
  image: index.alauda.cn/alauda/mysql
  ports:
      - "3306:3306"
environment:
   MYSQL_ROOT_PASSWORD: chuantaiyidev
   MYSQL_USER: cty
   MYSQL_PASSWORD: chuantaiyidev
   MYSQL_DATABASE: taurus
es:
   image: index.alauda.cn/chuanty/local_elasticsearch
ports:
   - "9200:9200"
   - "9300:9300"

server:
   #image: index.alauda.cn/ryugou/nginx
   image: index.alauda.cn/chuanty/local_nginx:1.1
ports:
   - "80:80"
   - "443:443"
links:
   - webphp:fpm  
volumes_from:
   - webphp:rw
interpolack
  • 876
  • 10
  • 26
RyuGou
  • 71
  • 1
  • 6

3 Answers3

4

I guess your problem is "sendfile on" in your nginx.conf.

For development purpose try to set it off in the server-directive of your server block:

server {
    ...

    sendfile off;
}

This will force to reload the static files such as css and js. nginx won't load the file from memory instead.

http://nginx.org/en/docs/http/ngx_http_core_module.html#sendfile

GreedyBytes
  • 101
  • 6
1

One option is to copy the modified static files directly to the Docker container:

docker cp web/static/main.js container_name:/usr/src/app/static/main.js

  • web/static is the static directory on your host machine
  • main.js is the modified file
  • container_name is the name of the container which can be found with docker ps
  • /usr/src/app/static is the static directory in your Docker container

If you want to determine exactly where your static files are on your Docker container, you can use docker exec -t -i container_name /bin/bash to explore its directory structure.

interpolack
  • 876
  • 10
  • 26
0

I really hope you actually do not copy configuration file into an image!

Docker Best Practice

Docker images are supposed to be immutable, hence at deploy the configuration file (that are depend on a lot [environment] of variables) should be passed to/shared with the container thanks to the docker run option -v|--volume instead.

As for your issue

If you want to see any change when static files are modified you need to docker build and then docker compose up on every modifications in order to actually change the web page. You may not want that (clearly). I suggest you to use shared directories (through the -v option).

Auzias
  • 3,638
  • 14
  • 36