Copy multiple directories with one command

后端 未结 2 1611
猫巷女王i
猫巷女王i 2020-12-18 17:35

Is there any way to copy multiple directories in one command, to reduce the number of layers? E.g., instead of:

COPY dirone ./dirone
COPY dirtwo ./dirtwo
COP         


        
相关标签:
2条回答
  • 2020-12-18 18:16

    That's the documented behavior of the copy command:

    If <src> is a directory, the entire contents of the directory are copied, including filesystem metadata.

    Note: The directory itself is not copied, just its contents.

    Best workaround I can suggest is to change your directory layout in your build folder, move the three folders under one parent folder and add the parent.

    0 讨论(0)
  • 2020-12-18 18:17

    As BMitch answered, that is expected COPY behaviour.

    An alternative would be to ADD the contents of a tarball.

    Create the initial tarball

    tar -cvf dirs.tar dirone/ dirtwo/ dirthree/
    

    Add it to the build

    FROM busybox
    ADD dirs.tar /
    CMD find /dirone /dirtwo /dirthree
    

    The tarball is automatically extracted

    ○ →docker run c28f96eadd58
    /dirone
    /dirone/one
    /dirtwo
    /dirtwo/two
    /dirthree
    /dirthree/three
    

    Note that every time you update the tar file you are invalidating the Docker build cache for that step. If you are dealing with a lot of files you might want to be smart about when you do the tar -c. You could also use tar -u if you can deal with files not being automatically deleted from the tarball.

    [ -f dirs.tar ] && tar -uf dirs.tar something || tar -cf dirs.tar something
    
    0 讨论(0)
提交回复
热议问题