1

What is the right way to develop app locally in the container, when it is possible to have node_modules on my laptop file system.

Currently I have following settings in the Dockerfile

COPY package.json /app/
RUN npm install
COPY . /app/

And the following docker-compose

 volumes:
    - "/app/node_modules"

In that case I'm able to run npm commands in the container, and changes in the package.json are reflected in my local source tree. Which is good. But one thing is missing: I don't have locally node_modules folder and it means my IDE can't see these files.

Is it possible to fix somehow?

6
  • Is there a reason why you wouldnt develop it locally as normal and then dockerize it? Commented Aug 10, 2016 at 11:12
  • With that configuration, changes to to package.json inside the container will not be reflected to the host. The volume is a container volume that will not survive a docker rm -v. Perhaps you intended to use a host volume? Commented Aug 10, 2016 at 12:17
  • @BMitch Fortunately it works, and I can easily see changes, what I've made in the container, on my host Commented Aug 10, 2016 at 12:49
  • That would requre some configuration you have not included in the question above. Commented Aug 10, 2016 at 13:00
  • @BMitch I'm really not sure what do you mean. I think I've mentioned everything already Commented Aug 11, 2016 at 13:45

1 Answer 1

1

You can copy all your local environment to the container:

COPY ./package.json /app/package.json
WORKDIR /app/
RUN npm install
COPY ./src/ /app/src/
CMD ["npm","run","start"]

This approach will literally copy your local environment to the image. Everytime you would make a change to your sources - you would have to rebuild the image and restart the container.

I recommend that you mount your project folder as a volume to the container - with all dependencies, and every change in the sources/dependencies would require just restarting the container (or you can use a supervisor inside the container):

VOLUME /app/
WORKDIR /app/
CMD ["npm","run","start"]

When running the container - remember to connect your project directory to the container:

And the docker-compose.yml file

version: '2'

services:
  app:
    build: .
    image: app
    volumes: [".:/app/"]

(I assumed that the Dockerfile for your Node.js app is in the same directory as sources, node_modules and docker-compose.yml )


EDIT: The solution posted above is good for local development. If you want to dockerize and deploy your application there are two approaches:

Copy your local node_modules and all sources to the container:

COPY ./node_modules /app/
COPY ./src /app/
COPY ./package.json /app/
WORKDIR /app/
CMD ["npm","run","start"]

Or to use the template from the top of my answer(it will install all the node_modules during the build - when package.json changes) this build is way slower.

Sign up to request clarification or add additional context in comments.

8 Comments

it does not work, node_modules folder hidden because of mounting local folder over it. If you can make a github repo with sample express or anything else app it would be great. Thanks.
Could you please mention how you gonna run it? cause there is no npm install in the Dockerfile, it means you have install somehow (how?) after your container is up?
@user1016265 I added a better Readme to the repo. Report if you have any problem. Make sure you understand volumes
@jan-sch Well I think you misunderstood my original message, I don't to install modules locally, I want to install it in the container. Furthermore, in your solution it's unclear how you will deploy your image, If there is no modules installed there?
The thing is that in second your option you won't see folder node_modules on your host after your run your container. The first solution is silly because of you need to install modules locally which is not acceptable. If you try my original solution you'll see that it works like it has to be, only one issue there, node_modules is not available locally, and this brings some issues with IDE
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.