最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dockerfile - After running docker, a new “node_modules” folder is created locally - Stack Overflow

programmeradmin3浏览0评论
root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# cat Dockerfile 
FROM node:14

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 80

CMD ["npm", "start"]


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# docker build -t feedbackimg .
[+] Building 0.3s (10/10) FINISHED                                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                                              0.0s
 => => transferring dockerfile: 145B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/node:14                                                                        0.0s
 => [internal] load .dockerignore                                                                                                 0.0s
 => => transferring context: 2B                                                                                                   0.0s
 => [1/5] FROM docker.io/library/node:14                                                                                          0.0s
 => [internal] load build context                                                                                                 0.0s
 => => transferring context: 369B                                                                                                 0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                     0.0s
 => CACHED [3/5] COPY package.json .                                                                                              0.0s
 => CACHED [4/5] RUN npm install                                                                                                  0.0s
 => [5/5] COPY . .                                                                                                                0.0s
 => exporting to image                                                                                                            0.1s
 => => exporting layers                                                                                                           0.0s
 => => writing image sha256:c6c67ae9a59c9d34ce2d59216eae2e6df2fe0c67366319d4e3095bd60bfe66b9                                      0.0s
 => => naming to docker.io/library/feedbackimg                                                                                    0.0s


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  node_modules  package.json  pages  public  server.js  temp


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# rm -r node_modules/


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  package.json  pages  public  server.js  temp

root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# docker run -p 8000:80 -d --rm --name feedback-app -v feedback:/app/feedback -v "/root/docker_learning/volumes_learning/data-volumes-02-added-dockerfile:/app" -v /app/node_m
odules feedbackimg
d0f9829868ad5e39a780afd13dc00f8e89848a8323a396fe19fa9d1b14f25ef0


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  node_modules  package.json  pages  public  server.js  temp

I am a beginner learning docker, and I encountered some problems when creating volumes. After I run the following command, a new empty folder node_modules is created in the local directory, which is far beyond my expectations.

Here is my understanding:

  • Volumes are managed in priority order according to "path length".
  • The volume with the shortest path is bind mounts, which points to /app. The other two volumes are /app/feedback and /app/node_modules. Therefore, bind mounts have the lowest priority.
  • The node_modules added to the container through Dockerfile will not be mapped to the local directory through bind mounts.

The purpose of doing this is:

  1. The feedback directory is empty, I want feedback to be saved in the volume, and bind mounts will not overwrite the content in feedback.
  2. There is no node_modules folder in the local directory, and the node_modules in the container is generated by npm install.

I hope I have explained my problem clearly and I hope you can help me. Your help is very important to me and I am very grateful.

root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# cat Dockerfile 
FROM node:14

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 80

CMD ["npm", "start"]


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# docker build -t feedbackimg .
[+] Building 0.3s (10/10) FINISHED                                                                                      docker:default
 => [internal] load build definition from Dockerfile                                                                              0.0s
 => => transferring dockerfile: 145B                                                                                              0.0s
 => [internal] load metadata for docker.io/library/node:14                                                                        0.0s
 => [internal] load .dockerignore                                                                                                 0.0s
 => => transferring context: 2B                                                                                                   0.0s
 => [1/5] FROM docker.io/library/node:14                                                                                          0.0s
 => [internal] load build context                                                                                                 0.0s
 => => transferring context: 369B                                                                                                 0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                     0.0s
 => CACHED [3/5] COPY package.json .                                                                                              0.0s
 => CACHED [4/5] RUN npm install                                                                                                  0.0s
 => [5/5] COPY . .                                                                                                                0.0s
 => exporting to image                                                                                                            0.1s
 => => exporting layers                                                                                                           0.0s
 => => writing image sha256:c6c67ae9a59c9d34ce2d59216eae2e6df2fe0c67366319d4e3095bd60bfe66b9                                      0.0s
 => => naming to docker.io/library/feedbackimg                                                                                    0.0s


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  node_modules  package.json  pages  public  server.js  temp


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# rm -r node_modules/


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  package.json  pages  public  server.js  temp

root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# docker run -p 8000:80 -d --rm --name feedback-app -v feedback:/app/feedback -v "/root/docker_learning/volumes_learning/data-volumes-02-added-dockerfile:/app" -v /app/node_m
odules feedbackimg
d0f9829868ad5e39a780afd13dc00f8e89848a8323a396fe19fa9d1b14f25ef0


root@iZ2zee46sbqa371vzyq1jpZ:~/docker_learning/volumes_learning/data-volumes-02-added-dockerfile# ls
Dockerfile  feedback  node_modules  package.json  pages  public  server.js  temp

I am a beginner learning docker, and I encountered some problems when creating volumes. After I run the following command, a new empty folder node_modules is created in the local directory, which is far beyond my expectations.

Here is my understanding:

  • Volumes are managed in priority order according to "path length".
  • The volume with the shortest path is bind mounts, which points to /app. The other two volumes are /app/feedback and /app/node_modules. Therefore, bind mounts have the lowest priority.
  • The node_modules added to the container through Dockerfile will not be mapped to the local directory through bind mounts.

The purpose of doing this is:

  1. The feedback directory is empty, I want feedback to be saved in the volume, and bind mounts will not overwrite the content in feedback.
  2. There is no node_modules folder in the local directory, and the node_modules in the container is generated by npm install.

I hope I have explained my problem clearly and I hope you can help me. Your help is very important to me and I am very grateful.

Share Improve this question edited Mar 31 at 9:40 B.Griffin 3555 silver badges14 bronze badges asked Mar 31 at 9:23 夏浅沫夏浅沫 32 bronze badges 3
  • For the Dockerfile you show, you shouldn't need any volumes at all; everything is already contained in the image, and you should be able to just run it. If you're looking for a development environment, just installing Node is usually a one-liner with your host's package manager and that will be far easier to use than simulating a local environment with Docker. – David Maze Commented Mar 31 at 10:31
  • 1 This question sounds a lot like Why does docker create empty node_modules and how to avoid it? Does that question have the technical answer you're looking for? If not, can you edit the question to include a minimal reproducible example, in particular showing the volume mounts you're using and the behavior you're getting? – David Maze Commented Mar 31 at 10:34
  • @davidmaze Thank you very much, this is my question, I haven't found any similar answers to my question. I will try my best to find relevant answers on the Internet in the future. – 夏浅沫 Commented Apr 1 at 3:01
Add a comment  | 

1 Answer 1

Reset to default 0

Linux needs a directory to act as an anchor point for any mounts.

As you have mounted the folder that contains the node_modules anchor point, it will appear locally.

However, the node_modules directory won't reflect the containers contents to the host or visa versa.

In the case that this behaviour is unwanted, docker compose watch is a superior way to sync code changes into a running container without using any kind of host to container bind mount.

发布评论

评论列表(0)

  1. 暂无评论