Reloading code in a dockerized node.js app with docker-compose

Viewed 12714

I have a trivial node.js docker app.

I am able to run it successfully, however the code reloading inside the container does not work despite mounting the volume as described in the docker-compose docs.

Directory layout:

my-test-app
| docker-compose.yml
| Dockerfile
| index.js
| package.json

Dockerfile:

FROM mhart/alpine-node:8
WORKDIR /app
COPY . .
EXPOSE 5000
CMD ["node", "index.js"]

docker-compose.yml:

version: '3'
services:
  node-app:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
  redis:
    image: "redis:alpine"

index.js:

const http = require('http');
const server = http.createServer((req, res) => {
  res.end("hello world");
});
server.listen(5000);

package.json:

{
  "name": "my-test-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "author": "",
  "license": "MIT"
}

I have also tried the following with same result (app runs, not able to live reload code)

  • using the official node image, instead of alpine-node.
  • putting the application code under ./app folder inside my-test-app and updating the COPY inside Dockerfile and volumes inside docker-compose.yml accordingly.
3 Answers

I had a similar problem even though I was using nodemon. The solution that worked for me was to change the volume of the node app inside the docker compose file to read from /src or wherever your app files are in.

Related