HubertNNN
HubertNNN

Reputation: 2101

How to copy subfolders in docker

I have following folder structure:

nova-components
    component1
        dist
        ...
    component2
        dist
        ...
    component3
        dist
        ...
    ...

Is there any way to copy only dist folders in docker. I am thinking about something like:

COPY --from=assets /nova-components/*/dist /var/www/nova-components/*/dist

The end goal is to include generated dist folders in the final image and keep the directory tree structure.

Upvotes: 1

Views: 5445

Answers (2)

atline
atline

Reputation: 31564

Currently multi-stage docker build do not respect .dockerignore, see this discussion, so you had to do it yourself, one way is to clean things in the first stage, like follows:

Dockerfile:

FROM ubuntu:16.04 AS assets

RUN mkdir -p /nova-components/component1/dist && \
  mkdir -p /nova-components/component1/others && \
  mkdir -p /nova-components/component2/dist && \
  mkdir -p /nova-components/component2/others

RUN find /nova-components/*/* ! -name "dist" -maxdepth 0 | xargs rm -fr

FROM ubuntu:16.04

COPY --from=assets /nova-components /var/www/nova-components/

RUN ls -alh /var/www/nova-components
RUN ls -alh /var/www/nova-components/*

test:

# docker build --no-cache -t try:1 .
Sending build context to Docker daemon  2.048kB
Step 1/7 : FROM ubuntu:16.04 AS assets
 ---> b9e15a5d1e1a
Step 2/7 : RUN mkdir -p /nova-components/component1/dist &&   mkdir -p /nova-components/component1/others &&   mkdir -p /nova-components/component2/dist &&   mkdir -p /nova-components/component2/others
 ---> Running in d4c9c422d53a
Removing intermediate container d4c9c422d53a
 ---> d316032dd59d
Step 3/7 : RUN find /nova-components/*/* ! -name "dist" -maxdepth 0 | xargs rm -fr
 ---> Running in b6168b027f4c
Removing intermediate container b6168b027f4c
 ---> 9deb57cb5153
Step 4/7 : FROM ubuntu:16.04
 ---> b9e15a5d1e1a
Step 5/7 : COPY --from=assets /nova-components /var/www/nova-components/
 ---> 49301f701db2
Step 6/7 : RUN ls -alh /var/www/nova-components
 ---> Running in 9ed0cafff2fb
total 16K
drwxr-xr-x 4 root root 4.0K Nov  6 02:13 .
drwxr-xr-x 3 root root 4.0K Nov  6 02:13 ..
drwxr-xr-x 3 root root 4.0K Nov  6 02:13 component1
drwxr-xr-x 3 root root 4.0K Nov  6 02:13 component2
Removing intermediate container 9ed0cafff2fb
 ---> f1ee82cff972
Step 7/7 : RUN ls -alh /var/www/nova-components/*
 ---> Running in 23a27e5ce853
/var/www/nova-components/component1:
total 12K
drwxr-xr-x 3 root root 4.0K Nov  6 02:13 .
drwxr-xr-x 4 root root 4.0K Nov  6 02:13 ..
drwxr-xr-x 2 root root 4.0K Nov  6 02:13 dist

/var/www/nova-components/component2:
total 12K
drwxr-xr-x 3 root root 4.0K Nov  6 02:13 .
drwxr-xr-x 4 root root 4.0K Nov  6 02:13 ..
drwxr-xr-x 2 root root 4.0K Nov  6 02:13 dist
Removing intermediate container 23a27e5ce853
 ---> b9d5ab8f5157
Successfully built b9d5ab8f5157
Successfully tagged try:1

With the clean in first stage using RUN find /nova-components/*/* ! -name "dist" -maxdepth 0 | xargs rm -fr, then you can make it, let's wait for possible official feature support.

Upvotes: 2

Siyu
Siyu

Reputation: 12079

Add a .dockerignore file with your Dockerfile

nova-components/*/*
!nova-components/*/dist

And copy like

COPY nova-components/ /var/www/nova-components

EDIT

So with multi stage build, this is currently not working. A new solution is to run, on the last stage,

rsync -avz --include='dist/' nova-components/ nova-components-dist/.

then at the final stage,

COPY --from=assets /nova-components-dist/ /var/www/nova-components

Upvotes: 0

Related Questions