undefined
undefined

Reputation: 3632

How to speed up the COPY from one image to other in Dockerfile

I am creating an image of my application which involves the packaging of different applications.

After doing the tests/ npm/ bower install etc I am trying to copy the content from the previous image to a fresh image. But that COPY seems very very slow and takes more than 3-4 minutes.

COPY --from=0 /data /data

(Size of /data folder is around 800MB and thousands of files)

Can anyone please suggest a better alternative or some idea to optimize this:

Here is my dockerfile:

FROM node:10-alpine
RUN apk add python git \
  && npm install -g bower

ENV CLIENT_DIR /data/current/client
ENV SERVER_DIR /data/current/server
ENV EXTRA_DIR /data/current/extra

ADD src/client $CLIENT_DIR
ADD src/server $SERVER_DIR

WORKDIR $SERVER_DIR
RUN npm install
RUN npm install --only=dev
RUN npm run build

WORKDIR $CLIENT_DIR
RUN bower --allow-root install

FROM node:10-alpine 
COPY --from=0 /data /data # This step is very very slow.
EXPOSE 80
WORKDIR /data/current/server/src
CMD ["npm","run","start:staging"]

Or if anyone can help me cleaning up the first phase (to reduce the image size), so that it doesn't require using the next image that will be useful too.

Upvotes: 8

Views: 10421

Answers (1)

SushilG
SushilG

Reputation: 731

It is taking time because the number of files are large . If you can compress the data folder as tar and then copy and extract will be helpful in your situation.

Otherwise If you can take this step to running containers it will be very fast. As per your requirement you need to copy an image of your application that is created already in another image. You can use volume sharing functionality that will share a volume in between 2 or more docker containers.

Create 1st container:

docker run -ti --name=Container -v datavolume:/datavolume ubuntu

2nd container:

docker run -ti --name=Container2 --volumes-from Container ubuntu

Or you can use -v option , so with v option create your 1st and 2nd container as:

docker run -v docker-volume:/data-volume --name centos-latest -it centos

docker run -v docker-volume:/data-volume --name centos-latest1 -it centos

This will create and share same volume folder that is data-volume in both the containers. docker-volume is the volume name and data-volume is folder name in that container that will be pointing to docker-volume volume Same way you can share a volume with more than 2 containers.

Upvotes: 4

Related Questions