Search code examples
dockerdocker-composedocker-volume

Docker Volumes - Changes not reflected


Using node.js

I am having an issue with docker volumes. I set up a volume in my docker-compose.yml file, but for some reason, changes I am making locally are not being reflected. Any idea why?

I have the current docker-compose.yml file

version: "3"
services:
  posts:
    build:
      dockerfile: Dockerfile.dev
      context: ./posts
    ports:
      - "4000:4000"
    volumes:
      - /app/node_modules
      - ./posts:/app
  ...// more services here

Excerpt from index.js in posts

app.get("/posts", (req, res) => {
  console.log("Quackss!");
  res.send(posts);
});

Now lets say I run

docker-compose up --build posts

When I make my first request via postman to /posts I see "Quackss!" in my console.

Now when I change the code to

app.get("/posts", (req, res) => {
  console.log("Double Quack");
  res.send(posts);
});

and save, then make a request via postman I still see "Quacks!!" instead of "Double Quack".

I do have nodemon setup, so I didn't think that was the issue.

I ran docker ps to see the name of the container Then ran docker exec -it <container name> sh then cat index.js If volumes were setup correctly, I'd expect to see

app.get("/posts", (req, res) => {
  console.log("Double Quack");
  res.send(posts);
});

Instead, I saw the original

app.get("/posts", (req, res) => {
  console.log("Quackss!");
  res.send(posts);
});

Here is my posts package.json

{
  "name": "posts",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.19.2",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "nodemon": "^2.0.4"
  }
}

And dockerfile.dev

FROM    node:alpine
WORKDIR /usr/app/
COPY    ./package.json ./
RUN     npm install
COPY    ./ ./
CMD     ["npm", "start"]

Any idea why this is happening?


Solution

  • Your dockerfile.dev has a WORKDIR of /usr/app/ and you're copying the source code into that directory. Your docker-compose.yml file is mapping the volumes to /app instead.

    One solution is to change the WORKDIR to /app/ and rebuild your image.