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.

  • Version error when installing docker toolbox on OSX
  • trouble getting docker image up and runnig
  • How to make an Azure VM & configure containers to use Azure File Storage via docker CLI / quickstart terminal?
  • Error 500 when running exec start with Docker Remote API
  • cannot connect mysql and php
  • bluemix “docker images” results in “json: cannot unmarshal string into Go value of type int”
  • 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?

  • SSH into staging machine from docker instance using Bitbucket Pipelines
  • How to send files as arguments of docker commands?
  • Rich editors in a Docker development environment
  • Where are binaries of a program running inside a Docker container?
  • docker + rails + redis - rescue workers are not running
  • Files created inside docker are write protected on host
  • 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.