4

I have seen a lot of codebase on Docker-compose + Nodejs but none of them are simple or basic enough to understand the flow of commands from docker-compose. I have created a very basic nodejs app which I intend to enable hot-reloading with containers.

My package.json looks like:

{
"name": "docker_web_app",
"version": "1.0.0",
"description": "Node.js on Docker",
"author": "First Last <[email protected]>",
"main": "server.js",
"scripts": {
    "start": "node server.js",
    "watch": "nodemon"
},
"dependencies": {
    "express": "^4.16.1",
    "nodemon": "^2.0.7"
}
}

Dockerfile looks like:

FROM node:14 as base

WORKDIR /home/node/app

COPY package*.json ./

RUN npm install -g nodemon && npm install

# Bundle app source
COPY . .

EXPOSE 4000
# RUN npm run start
CMD [ "npm", "start" ]

FROM node:14 as dev

WORKDIR /home/node/app

EXPOSE 4000

CMD ["npm", "watch"]

and the docker-compose.yml file looks like:

version: '3.7'
services:
  ts-node-docker:
    build:
      context: .
      dockerfile: Dockerfile
      target: base
    volumes:
      - ./:/home/node/app/
    container_name: ts-node-docker
    expose:
      - '4000'
    ports:
      - '4000:4000'
    command: npm run watch

What changes should I do to enable hot reloading in this codebase? Thanks!

1
  • 1
    Can you install Node on the host? That's usually a very easy install, and you may even have it installed already for other projects. Then you can just run npm run watch without needing to work around Docker's isolation features. Commented Jun 17, 2021 at 9:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.