Conceptual questions about bind mounts and development workflow (dev-mode) containers in Getting Started tutorial

So according to the tutorial a good way to quickly make persistent changes to our source code and immediately view those changes without having to rebuild the image every time is to utilize a bind mount linking our host app directory to the corresponding container directory, like so:

host machine directory ====== bind mount ======> container directory

docker run -dp 3000:3000 \
-w /app -v $(pwd):/app \
node:18-alpine \
sh -c “yarn install && yarn run dev”

However the problem with this is that due to the bind mount, running a “yarn install” inside the container ends up also copying those dependencies into my host directory and thereby polluting my local environment with a “node_modules” folder full of production and dev packages (express, nodemon, etc.) I didn’t necessarily ask for.

What I’d like to know is:

  1. How does using Docker solve the problem of isolating project dependencies on the host machine where I’m running Docker Desktop and modifying my source code? I’m not really familiar with Node and how Node dependency management works (will “node_modules” appearing in my host directory screw things up??), but with Python for instance I’m able to encapsulate my project dependencies inside a virtual environment so as not to clash with global dependencies.

  2. According to this development workflow, do we develop and modify our source code in our local host system or inside the container itself? The tutorial seems to suggest that we make changes in the host directory that’s been bind-mounted to the container (the so-called “dev-mode” container), but then again they could also be saying that adding a bind mount changes the game and makes the host system redundant for everything except as storage.

Thanks for your help!

If you bind a host folder into a container, you get the exact same folder inside the container - as in: the container does not use a copy, it uses the original. A bind will not filter subfolders.

The next best thing could be docker compose watch:
https://www.docker.com/blog/docker-desktop-4-24-compose-watch-resource-saver-and-docker-engine/

It allows syncing host folders or re-building the image, if changes are detected for one or more a specified folder.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.