How to copy folders to docker image from Dockerfile?

后端 未结 10 647
走了就别回头了
走了就别回头了 2021-01-31 01:41

I tried the following command in my Dockerfile: COPY * / and got mighty surprised at the result. Seems the naive docker code traverses the directories from the glob

相关标签:
10条回答
  • 2021-01-31 02:12

    use ADD instead of COPY. Suppose you want to copy everything in directory src from host to directory dst from container:

    ADD src dst
    

    Note: directory dst will be automatically created in container.

    0 讨论(0)
  • 2021-01-31 02:14

    Use ADD (docs)

    The ADD command can accept as a <src> parameter:

    1. A folder within the build folder (the same folder as your Dockerfile). You would then add a line in your Dockerfile like this:
    ADD folder /path/inside/your/container
    

    or

    1. A single-file archive anywhere in your host filesystem. To create an archive use the command:
    tar -cvzf newArchive.tar.gz /path/to/your/folder
    

    You would then add a line to your Dockerfile like this:

    ADD /path/to/archive/newArchive.tar.gz  /path/inside/your/container
    

    Notes:

    • ADD will automatically extract your archive.
    • presence/absence of trailing slashes is important, see the linked docs
    0 讨论(0)
  • 2021-01-31 02:15

    FROM openjdk:8-jdk-alpine RUN apk update && apk add wget openssl lsof procps curl RUN apk update RUN mkdir -p /apps/agent RUN mkdir -p /apps/lib ADD ./app/agent /apps/agent ADD ./app/lib /apps/lib ADD ./app/* /apps/app/ RUN ls -lrt /apps/app/ CMD sh /apps/app/launch.sh

    by using DockerFile, I'm copying agent and lib directories to /apps/agent,/apps/lib directories and bunch of files to target.

    0 讨论(0)
  • 2021-01-31 02:18

    I don't completely understand the case of the original poster but I can proof that it's possible to copy directory structure using COPY in Dockerfile.

    Suppose you have this folder structure:

    folder1
      file1.html
      file2.html
    folder2
      file3.html
      file4.html
      subfolder
        file5.html
        file6.html
    

    To copy it to the destination image you can use such a Dockerfile content:

    FROM nginx
    
    COPY ./folder1/ /usr/share/nginx/html/folder1/
    COPY ./folder2/ /usr/share/nginx/html/folder2/
    
    RUN ls -laR /usr/share/nginx/html/*
    

    The output of docker build . as follows:

    $ docker build --no-cache .
    Sending build context to Docker daemon  9.728kB
    Step 1/4 : FROM nginx
     ---> 7042885a156a
    Step 2/4 : COPY ./folder1/ /usr/share/nginx/html/folder1/
     ---> 6388fd58798b
    Step 3/4 : COPY ./folder2/ /usr/share/nginx/html/folder2/
     ---> fb6c6eacf41e
    Step 4/4 : RUN ls -laR /usr/share/nginx/html/*
     ---> Running in face3cbc0031
    -rw-r--r-- 1 root root  494 Dec 25 09:56 /usr/share/nginx/html/50x.html
    -rw-r--r-- 1 root root  612 Dec 25 09:56 /usr/share/nginx/html/index.html
    
    /usr/share/nginx/html/folder1:
    total 16
    drwxr-xr-x 2 root root 4096 Jan 16 10:43 .
    drwxr-xr-x 1 root root 4096 Jan 16 10:43 ..
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file1.html
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file2.html
    
    /usr/share/nginx/html/folder2:
    total 20
    drwxr-xr-x 3 root root 4096 Jan 16 10:43 .
    drwxr-xr-x 1 root root 4096 Jan 16 10:43 ..
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file3.html
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file4.html
    drwxr-xr-x 2 root root 4096 Jan 16 10:33 subfolder
    
    /usr/share/nginx/html/folder2/subfolder:
    total 16
    drwxr-xr-x 2 root root 4096 Jan 16 10:33 .
    drwxr-xr-x 3 root root 4096 Jan 16 10:43 ..
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file5.html
    -rwxr-xr-x 1 root root    7 Jan 16 10:32 file6.html
    Removing intermediate container face3cbc0031
     ---> 0e0062afab76
    Successfully built 0e0062afab76
    
    0 讨论(0)
  • 2021-01-31 02:22

    As mentioned in your ticket:

    You have COPY files/* /test/ which expands to COPY files/dir files/file1 files/file2 files/file /test/.
    If you split this up into individual COPY commands (e.g. COPY files/dir /test/) you'll see that (for better or worse) COPY will copy the contents of each arg dir into the destination directory. Not the arg dir itself, but the contents.

    I'm not thrilled with that fact that COPY doesn't preserve the top-level dir but its been that way for a while now.

    so in the name of preserving a backward compatibility, it is not possible to COPY/ADD a directory structure.

    The only workaround would be a series of RUN mkdir -p /x/y/z to build the target directory structure, followed by a series of docker ADD (one for each folder to fill).
    (ADD, not COPY, as per comments)

    0 讨论(0)
  • 2021-01-31 02:24

    the simplest way:

    sudo docker cp path/on/your/machine adam_ubuntu:/root/path_in_container
    

    Note putting into the root path if you are copying something that needs to be picked up by the root using ~.

    0 讨论(0)
提交回复
热议问题