calebolson123 / BabySleepCoach

DIY Baby Sleep Tracking
https://calebolson.com
Other
201 stars 35 forks source link

Docker volume overrides node_modules #24

Closed romainreignier closed 6 months ago

romainreignier commented 11 months ago

During the build step of the Docker image, yarn install is called and generates a node_modules directory in webapp. This operation installs react-scripts used to start the web app. But by mounting the current host dir in the container overrides the /usr/app/babysleepcoach directory content and the webapp/node_modules is not present anymore, resulting in the error on docker-compose up:

baby-sleep-coach    | yarn run v1.22.19
baby-sleep-coach    | $ react-scripts start&
baby-sleep-coach    | /bin/sh: 1: react-scripts: not found
baby-sleep-coach    | Done in 0.07s.

Only mounting single files is working but tedious:

    volumes:
      - ./main.py:/usr/app/babysleepcoach/main.py
      - ./sleep_logs.csv:/usr/app/babysleepcoach/sleep_logs.csv
      - ./user_defined_crop_area.txt:/usr/app/babysleepcoach/user_defined_crop_area.txt
calebolson123 commented 11 months ago

Thanks for documenting this, yeah that was a last minute sloppy patch.. I have been meaning to circle back & clean this up. I believe this was causing issues here: https://github.com/calebolson123/BabySleepCoach/issues/21

calebolson123 commented 11 months ago

Regarding mounting single files, I agree it's tedious & ugly. Do you have any cleaner ideas for preserving modifications to these files through container destruction?

romainreignier commented 11 months ago

Move these files to a subdir and only mount this subdir. It could be src.

6 oct. 2023 15:18:45 calebolson123 @.***>:

Regarding mounting single files, I agree it's tedious & ugly. Do you have any cleaner ideas for preserving modifications to these files through container destruction?

— Reply to this email directly, view it on GitHub[https://github.com/calebolson123/BabySleepCoach/issues/24#issuecomment-1750669534], or unsubscribe[https://github.com/notifications/unsubscribe-auth/ACDPSYAVRJIFRXKQLAMPWFDX6AALHAVCNFSM6AAAAAA5VWQBAKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJQGY3DSNJTGQ]. You are receiving this because you authored the thread. [Image de pistage][https://github.com/notifications/beacon/ACDPSYCSVSFK7UZEABH5IWLX6AALHA5CNFSM6AAAAAA5VWQBAKWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTTILEMN4.gif]

sabes23 commented 9 months ago

Possible Solutions:

  1. Avoid Overwriting node_modules: Modify the Docker Compose volume configuration to not overwrite the node_modules directory. This can achieve this by excluding node_modules from the volumes or by using a named volume for the node_modules.

Example in docker-compose.yml:

services:
  webapp:
    build: .
    volumes:
      - .:/usr/app/babysleepcoach
      - /usr/app/babysleepcoach/webapp/node_modules

This configuration mounts the current directory into the container but excludes the node_modules directory from being overwritten.

  1. Install Dependencies at Runtime: Alternatively, modify the container startup process to install dependencies when the container starts. This approach is less efficient but can ensure that the necessary modules are always present.

Modify the container's startup command to run yarn install before starting the application. This ensures that node_modules is populated each time the container starts.

Considerations:

The first approach is more efficient since it avoids reinstalling dependencies every time the container starts, which can be time-consuming. This is what I'm currently doing and it seems to be working fine.

The second approach ensures that the container always has the most up-to-date dependencies but can slow down the startup process.

appleimperio commented 7 months ago

in case anyone is having the same issue. Using the @sabes23 suggestion, the docker compose look like this.

services:

  baby-sleep-coach:
    build:
      dockerfile: ./Dockerfile
      context: .
    ports:
      - "0.0.0.0:${PORT}:80"
      - "0.0.0.0:8000:8000"
      - "0.0.0.0:8001:8001"
    env_file:
      - .env
    volumes:
      - .:/usr/app/babysleepcoach
      - /usr/app/babysleepcoach/webapp/node_modules
    # To use a webcam, uncomment these lines
    # devices:
    #   - /dev/video0
calebolson123 commented 6 months ago

Thanks @appleimperio @sabes23 @romainreignier, brought these changes into https://github.com/calebolson123/BabySleepCoach/pull/36 along w/ some other snags i ran into on testing this project on a fresh install