Why dotenv is not loaded in docker but works fine in host machine? Why dotenv is not loaded in docker but works fine in host machine? docker docker

Why dotenv is not loaded in docker but works fine in host machine?


I think this is caused by:

volumes:  - ".:/usr/src/app"

in your docker-compose file. This links the current dir to the app folder, meaning everything including node_modules is replaced.

Try removing the volumes from docker-compose and see if it works.

When you run docker run -it myimage /bin/sh it will not link the volume, meaning it will be using the correct node_modules, which is why it works when using that.