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.

  • unable to connect to dockerized mysql container locally
  • Docker: Monitor logical units of linked containers
  • limit logs amount for docker-compose
  • Can a variable be used in docker FROM?
  • Dockerfile production/build/debug/test environment
  • Connecting to a Remote EJB Module running in Docker
  • 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?

  • Attaching a Docker Container automatically to an IP Address/DNS
  • RoR Processing messages off an Message Queue
  • Docker: fail linking with mysql DB
  • Docker RUN Command
  • Big size of python image in Docker
  • How can I link a container spawned by one compose file to a container which is spawned by another compose file?
  • 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.