Kunkka Kunkka - 3 months ago 21
Node.js Question

nginx doesnt see updated static content

I'm using docker-compose to set up nginx and node

services:
nginx:
container_name: nginx
build: ./nginx/
ports:
- "80:80"
- "443:443"
links:
- node:node
volumes_from:
- node
volumes:
- /etc/nginx/ssl:/etc/nginx/ssl

node:
container_name: node
build: .
env_file: .env
volumes:
- /usr/src/app
- ./logs:/usr/src/app/logs
expose:
- "8000"
environment:
- NODE_ENV=production

command: npm run package


I have node and nginx share the same volume so that nginx can serve the static content generated by node.

When i update the source code in node. I remove the node container and rebuild it via the below

docker rm node


docker-compose -f docker-compose.prod.yml up --build -d node


I can see that the new node container has the updated source code with the proper updated static content

docker exec -it node bash


root@e0cd1b990cd2:/usr/src/app# cat public/style.css


this shows the updated content i want to see

.project_detail .owner{color:#ccc;padding:10px}


However, when i login to the nginx container

docker exec -it nginx bash


root@a459b271e787:/# cat /usr/src/app/public/style.css


.project_detail .owner{padding:10px}


as you can see , nginx is not able to see the newly updated static files served by node - despite the node update. It however works if i restart the nginx container as well.

Am i doing something wrong? Do i have to restart both nginx and node containers to see the updated content?

Answer

Instead of sharing volume of one container with another, share a common directory on the host with both the containers. For example, if the directory is at /home/user/app, then it should be present in volumes section like:

volumes:
  - /home/user/app:/usr/src/app

This should be done for both the containers.