我有以下文件夹结构:

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

有什么办法只能在docker中复制dist文件夹。
我在想类似的东西:
COPY --from=assets /nova-components/*/dist /var/www/nova-components/*/dist

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

最佳答案

当前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进行第一阶段的清理后,您就可以做到了,让我们等待可能的官方功能支持。

关于docker - 如何在Docker中复制子文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53159516/

10-16 03:58