'Esbuild not running on docker compose

I am new to building with esbuild and can't seem to get a script that can watch and rebuild my files working. I also need to get this working in docker.

In my package.json I have the following:

"start": "node dist/server.js",
"start:watch": "nodemon ./dist/server.js",
"build": "esbuild server.ts --bundle --platform=node --outfile=dist/server.js",
"build:watch": "yarn build --watch",
"dev": "yarn build && run-p start:watch build:watch"

And in my dockerfile:

FROM node:16

WORKDIR /app

COPY package.json .
COPY yarn.lock .
RUN yarn

COPY . .

RUN yarn build

CMD ["yarn", "dev"]

I am using a docker-compose file, which for this service looks like:

  actions:
    build:
      context: ./actions
    depends_on:
      - "my-other-app"
    volumes:
      - ./actions:/app
      - actions_node_modules:/app/node_modules
    environment:
      NODE_ENV: ${NODE_ENV}
      PORT: 5000

From a glance at the logs when docker is up, it does appear to be working.

yarn run v1.22.18
yarn build && run-p start:watch build:watch
esbuild server.ts --bundle --platform=node --outfile=dist/server.js

dist/server.js  1.2mb ⚠️

nodemon ./dist/server.js
yarn build --watch
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./dist/server.js`
esbuild server.ts --bundle --platform=node --outfile=dist/server.js --watch
Server listening at http://localhost:5000

However changes to my .ts files are not watched, and if I make a change to my package.json the sever restarts but nothing is rebuilt.

Can anyone advise where I am going wrong? If I run the yarn dev script outside of docker it works perfectly. There is something with the docker part that is going wrong.



Solution 1:[1]

I was able to find a solution. To add stdin_open: true to the docker-compose.yml. The reason for needing to add this is because Docker closes stdin and esbuild needs it to remain open to work.

  actions:
    build:
      context: ./actions
    depends_on:
      - "my-other-app"
    volumes:
      - ./actions:/app
      - actions_node_modules:/app/node_modules
    environment:
      NODE_ENV: ${NODE_ENV}
      PORT: 5000
    stdin_open: true

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charklewis