10

in my dockerfile I have these two lines:

ADD /ansible/inventory /etc/ansible/hosts
ADD /ansible/. /ansiblerepo

The first line works, as I can run the container and see my hosts file has been populated with all the ips from my inventory file.

The second line doesn't appear to be working though. I'm just trying to copy all the files/subdirectories of ansible and copy them over to the ansiblerepo directory inside the new container.

There are no errors while building the image, but again ansiblerepo is just an empty directory and nothing has copied over to it. I assume I'm just missing a back slash or something.

stephan
  • 2,293
  • 4
  • 31
  • 55

3 Answers3

8

Docker ADD and COPY commands work relative to the build directly, and only for files in that directory that weren't excluded with a .dockerignore file. The reason for this is that builds actually run on the docker host, which may be a remote machine. The first step of a docker build . is to package up all the files in the directory (in this case .) and send them to the host to run your build. Any absolute paths you provide are interpreted as relative to the build directory and anything you reference that wasn't sent to the server will be interpreted as a missing file.

The solution is to copy /ansible to your build directory (typically the same folder as your Dockerfile).

BMitch
  • 231,797
  • 42
  • 475
  • 450
4

Make sure that in your ".dockerignore" file, it does not excluded everything. usually, dockerignore file has these lines

*
!obj\Docker\publish\*
!obj\Docker\empty\

this means that everything is ignored except publish and empty folders.

VJPPaz
  • 917
  • 7
  • 21
3

Removing trailing /. from source directory should fix the ADD command.

On a related note, Docker Best Practices suggest using COPY over ADD if you don't need the URL download feature.

Roman
  • 19,581
  • 6
  • 68
  • 84