Install node_modules inside Docker container and synchronize them with host

前端 未结 10 1384
旧巷少年郎
旧巷少年郎 2020-12-12 11:33

I have the problem with installing node_modules inside the Docker container and synchronize them with the host. My Docker\'s version is 18.03.1-ce, build

10条回答
  •  既然无缘
    2020-12-12 12:05

    I wouldn't suggest overlapping volumes, although I haven't seen any official docs ban it, I've had some issues with it in the past. How I do it is:

    1. Get rid of the external volume as you are not planning on actually using it how it's meant to be used - respawning the container with its data created specifically in the container after stopping+removing it.

    The above might be achieved by shortening your compose file a bit:

    frontend:
      build: ./app/frontend
      volumes:
        - ./app/frontend:/usr/src/app
      ports:
        - 3000:3000
      environment:
        NODE_ENV: ${ENV}
      command: npm start
    
    1. Avoid overlapping volume data with Dockerfile instructions when not necessary.

    That means you might need two Dockerfiles - one for local development and one for deploying a fat image with all the application dist files layered inside.

    That said, consider a development Dockerfile:

    FROM node:10
    RUN mkdir -p /usr/src/app
    WORKDIR /usr/src/app
    RUN npm install
    

    The above makes the application create a full node_modules installation and map it to your host location, while the docker-compose specified command would start your application off.

提交回复
热议问题