Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy node_modules folder out of my docker container onto the build machine?

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

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

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

like image 571
chinds Avatar asked Jan 14 '18 14:01

chinds


People also ask

Can I copy node_modules folder?

You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required. In short: it won't hurt.

Can I copy node_modules folder to another project?

Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)


1 Answers

According to the documentation of the COPY instruction, the COPY instruction copies a file from the host to the container.

If you want the files from the container to be available outside your container, you can use Volumes. Volumes will help you have a storage for your container that is independent of the container itself, and thus you can use it for other containers in the future.

like image 186
Abhinav Manchanda Avatar answered Oct 09 '22 06:10

Abhinav Manchanda