How to copy folders to docker image from Dockerfile?

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 and then dumps the each file in the target directory while respectfully ignoring my directory structure.

At least that is how I understand this ticket and it certainly corresponds to the result I got.

  • Passing variables as arguments to docker container
  • Not able to connect to AS in Docker container
  • Docker container not able to access port 5432
  • Docker volumes qustion
  • Can't access OpenShift console on http://ip:8443
  • Docker letsencrypt does not appear to be creating webroot files
  • I guess the only reason this behavior can still exist must be that there is some other way this should be done. But it is not so easy for a bear of very little brain to understand how, does anyone know?

  • To what extent can a Docker cluster reduce infrastructure costs? [closed]
  • I need to run twice docker-compose to not have errors
  • Docker Swarm container reachable although port is not open?
  • How to debug an application running in Docker with IntelliJ?
  • How can I run my docker container with installed Nginx?
  • Difference between Docker ENTRYPOINT and Kubernetes container spec COMMAND?
  • 2 Solutions collect form web for “How to copy folders to docker image from Dockerfile?”

    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 COPY (one for each folder to fill).

    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: b will be automatically created in container.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.