Docker install node modules and copy back to host

chi*_*nds 7 node.js npm docker

I am moving an application to a new build pipeline. On CI I am not able to install node to complete the NPM install step.

My idea to is to move the npm install step to a Docker image that uses Node, install the node modules and them copy the node modules back to the host so another process can package up the application.

This is my Dockerfile:

FROM node:9

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY ./dashboard-interface/package.json /usr/src/app/
RUN npm install --silent --production

# Bundle app src
COPY node_modules ./dashboard-interface/node_modules #I thought this would copy the new node_modules back to the host
Run Code Online (Sandbox Code Playgroud)

This runs fine and install the node modules, but when I try and copy the node_modules directory back to the host I see an error saying:

COPY node_modules ./dashboard-interface/node_modules
COPY failed: stat /var/lib/docker/tmp/docker-builder718557240/node_modules: no such file or directory
Run Code Online (Sandbox Code Playgroud)

So it's clear that the copy process cannot find the node_modules directory that it has just installed the node modules too.

Abh*_*nda 2

根据COPY指令的文档,COPY指令将文件从主机复制到容器。

如果您希望容器中的文件在容器外部可用,则可以使用Volumes。卷将帮助您为容器提供独立于容器本身的存储,因此您将来可以将其用于其他容器。