colima icon indicating copy to clipboard operation
colima copied to clipboard

NodeJS hot reload issue

Open tcastelly opened this issue 2 years ago • 4 comments

Hello,

I switched from Docker Desktop to Colima. I noticed an issue with "watcher" on bind volume. I created a repository: https://github.com/shenron/colima-hot-reload-issue

The container is not able to see when a file has been updated. But if I touch the file from the container it works. I guess it's linked to the volume.


I'm using the Macbook Pro M1 Max with:

colima version 0.3.4
git commit: 5a4a70481ca8d1e794677f22524e3c1b79a9b4ae

runtime: docker
arch: aarch64
client: v20.10.14
server: v20.10.11
limactl version 0.9.2

tcastelly avatar Apr 20 '22 09:04 tcastelly

The way colima binds host directories into the guest VM means that certain filesystem events are not observed / generated.

You can work around this by using --legacy-watch in nodemon that will fall back to polling instead of the fsevents integration.

ggoodman avatar May 03 '22 18:05 ggoodman

Hello, thank you for your answer.

The legacy is a workaround, but unfortunately in my use case it's very slow and use a lot of cpu. It's not the best, but for now when I finished to edit my code I "touch" the entrypoint of the application.

docker exec -ti $CONTAINER_ID touch /usr/src/app/src/index.ts

tcastelly avatar May 03 '22 19:05 tcastelly

I'm facing the same issue with ts-node-dev. Is this an issue that will eventually be solved by colima or we will never receive the same events when using colima?

luiz290788 avatar Aug 18 '22 13:08 luiz290788

Is this an issue that will eventually be solved by colima or we will never receive the same events when using colima?

I am working on a workaround taking a similar approach to https://github.com/abiosoft/colima/issues/261#issuecomment-1116466858 while leveraging inotify.

I paused due to some issues with the Go library at the time, but I will revisit it.

abiosoft avatar Aug 18 '22 14:08 abiosoft

Is this an issue that will eventually be solved by colima or we will never receive the same events when using colima?

I am working on a workaround taking a similar approach to #261 (comment) while leveraging inotify.

I paused due to some issues with the Go library at the time, but I will revisit it.

It would be great to have this feature completed. Is there a work-in-progress branch that I could possibly help continue/test?

matthewdickinson avatar Oct 26 '22 15:10 matthewdickinson

Hi, is there any progress on that? If you need some help, I may push our development team to help you. We need this so our frontend team could work using colima. Docker for mac is not an option for us)

alikhanich avatar Nov 16 '22 20:11 alikhanich

@djirik support for virtiofs mounts is imminent and would be part of the next release.

It has been merged into master upstream in Lima and currently being integrated and tested in Colima, the early results have been quite impressive.

Virtiofs is same tech used by Docker Desktop for the fast volume speeds.

abiosoft avatar Nov 17 '22 01:11 abiosoft

@abiosoft thank you very much

alikhanich avatar Nov 17 '22 05:11 alikhanich

@abiosoft Hi, do you have an idea when would it be available?

alikhanich avatar Dec 13 '22 07:12 alikhanich

@djirik within the next week or two https://github.com/abiosoft/colima/issues/497#issuecomment-1345257182.

You can actually test it now ~~if you can build Colima from source (and brew install --HEAD lima), it's on the support-vz branch~~.

brew install --HEAD colima

abiosoft avatar Dec 13 '22 07:12 abiosoft

@abiosoft hi again, I've succesfully switched to virtiofs on macOs 13. However nodemon still does not see updated files.

colima status
INFO[0000] colima is running using macOS Virtualization.Framework
INFO[0000] arch: aarch64
INFO[0000] runtime: docker
INFO[0000] mountType: virtiofs
INFO[0000] socket: unix:///Users/hacker/.colima/default/docker.sock

--legacy-watch for nodemon doesn't seem to be helping at all. btw we are using nuxt framework, maybe it is something related with it?

alikhanich avatar Dec 13 '22 16:12 alikhanich

@abiosoft hi again, I've succesfully switched to virtiofs on macOs 13. However nodemon still does not see updated files.

@djirik hmmm 🤔 did you reset. i.e. colima delete && colima start ?

Is your project something that I can try to reproduce? I have tested with a number of development environments that provides live reload and it has worked fine.

abiosoft avatar Dec 13 '22 16:12 abiosoft

@djirik also, kindly confirm lima --version is v0.14.0?

abiosoft avatar Dec 13 '22 16:12 abiosoft

@abiosoft yes, I have reset it. Unfortunately, I can not share source code, as it is under NDA.

limactl version 0.14.0

I can ask frontend team if they can make sample project without production code.

alikhanich avatar Dec 13 '22 16:12 alikhanich

@abiosoft I have recreated colima again just to be sure, seems like it changed things a bit, now I'm having a lot of permission/access rights errors, I will fix it and return with more feedback.

alikhanich avatar Dec 13 '22 16:12 alikhanich

@abiosoft File permissions are hell with virtiofs. I can't even launch node project with volume mounts. Can not figure out how to set user:group in compose file.

alikhanich avatar Dec 15 '22 07:12 alikhanich

@abiosoft File permissions are hell with virtiofs. I can't even launch node project with volume mounts. Can not figure out how to set user:group in compose file.

Do you have an idea of the userId in the container? You can check by running id -u in the container. What docker image are you using?

abiosoft avatar Dec 15 '22 07:12 abiosoft

version: '3.9'
services:
  nuxt:
    build:
      dockerfile: ../docker/nuxt/Dockerfile
      context: ./source
      target: node_base
    image: ${NUXT_IMAGE_NAME:-localhost/nuxt}:${NUXT_IMAGE_TAG:-latest}
    # user: $DEV_LOCAL_UID:$DEV_LOCAL_GID # here I've tried 501:20, 1000:1000, 501:1000, 0:0
    restart: 'no'
    environment:
      FORCE_COLOR: '1'
      NPM_TOKEN: $NPM_TOKEN
    entrypoint: sh
    command:
      - -xec
      - |-
        npm run npm-auth-repo --loglevel verbose
        npm install --loglevel verbose
        npm run dev --loglevel verbose
    volumes:
      - ./source:/app
      - ./volumes/nuxt/HOME:/home/node
FROM node:16.17.1-alpine AS node_base
WORKDIR /app

ENV HOST 0.0.0.0

EXPOSE 3000

Everytime I'm getting either 243 error from node, or permission denied for creating files in ./volumes/nuxt/HOME. I've also tried chmod +rwx on HOME dir without effect.

alikhanich avatar Dec 15 '22 07:12 alikhanich

@djirik as an unrecommended stopgap, the permission denied error should go away if you run chmod -R 777 ./volumes/nuxt/HOME.

Give me a moment, let me try to reproduce your error using the node:16.17.1-alpine image.

abiosoft avatar Dec 15 '22 07:12 abiosoft

@djirik out of curiosity, why is /home/node getting mounted?

abiosoft avatar Dec 15 '22 07:12 abiosoft

@abiosoft I've tried with 777, but it didn't have any effect. Yes, /home/node is mounted. .npm dir is created and after that it says permission denied)

alikhanich avatar Dec 15 '22 08:12 alikhanich

@djirik I am unable to reproduce your permission issues. You can send me an email at the email on my GitHub profile and we can schedule a session to troubleshoot this.

As for the hot reload issue, I can confirm it is not yet fixed with virtiofs. Apparently, I was testing with a dotnet project and it works fine, and thereby assumed it catered for all other environments (including the node stack) as well.

abiosoft avatar Dec 15 '22 09:12 abiosoft

I can confirm that also on my nodejs project mounted volumes are not "touched" in the container. I encounter no permission issues though.

  • I resetted colima with colima delete && colima start
  • lima --version => limactl version 0.14.1

codingluke avatar Dec 15 '22 22:12 codingluke

@codingluke what is your node version? can you please share you Dockerfile and docker-compose?

alikhanich avatar Dec 16 '22 06:12 alikhanich

@abiosoft I have sent you an email

alikhanich avatar Dec 16 '22 06:12 alikhanich

@djirik I use the latest 18.12.1. It is a docusaurus/marp project.

node version

codingluke avatar Dec 16 '22 08:12 codingluke

@codingluke I get it now, you are not mounting sources and NODE_HOME into container, this is why you don't have permission issues I guess.

alikhanich avatar Dec 16 '22 09:12 alikhanich

correct and I would recommend to only mount the part which is edited locally. I don't see the need to share node_modules and co. For persistence I would use docker volumes. Of course there might be some reason to do it differently.

codingluke avatar Dec 16 '22 16:12 codingluke

@abiosoft I have skipped mounting /home/node, as it was found unnecessary, but hot reload is still not working :(.

The working behavior is like this:

  1. update file
  2. touch file in colima ssh
  3. refresh is triggered.

Can we steal something from Docker Desktop, so it would work?

alikhanich avatar Dec 20 '22 06:12 alikhanich

I assumed virtiofs will enable hot reload out of the box. Since that has now turned out not to be the case, this would be resumed https://github.com/abiosoft/colima/issues/261#issuecomment-1219565057.

abiosoft avatar Dec 20 '22 07:12 abiosoft