如何在Docker中复制子文件夹

时间:2018-11-05 17:43:28

标签: docker dockerfile

我具有以下文件夹结构:

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

有什么方法可以只复制docker中的dist文件夹。 我在想类似的东西:

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

最终目标是在最终映像中包括生成的dist文件夹,并保留目录树结构。

2 个答案:

答案 0 :(得分:2)

当前multi-stage docker build不尊重.dockerignore,请参阅此discussion,所以您必须自己做,一种方法是在第一阶段清洁事物,如下所示:

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/*

测试:

# 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

在第一阶段使用RUN find /nova-components/*/* ! -name "dist" -maxdepth 0 | xargs rm -fr进行清理后,您就可以做到了,让我们等待可能的官方功能支持。

答案 1 :(得分:0)

.dockerignore中添加一个Dockerfile文件

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

并复制

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

编辑

因此,对于多阶段构建,当前无法使用。一种新的解决方案将在最后一个阶段运行

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

然后在最后阶段

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