6

The docker client offers the cp sub-command as explained here, which is very handy when one needs to copy a file into a container (note: this is somewhat analogous to Dockerfile ADD instruction in image building). In Docker 1.8 the cp command has been even expanded a bit.

However, reading the Ansible docker module documentation, it appears that this is missing? Here are my 2 questions:

  1. Did I misunderstand the Ansible documentation?
  2. if Ansible is missing the cp thing, has anyone found a workaround? I can think of something like using Ansible copy module to transport the files to the remote machine first, and then run there the native docker client with cp, but ideally Ansible's docker module would have done this in a single shot as part of the docker module?

Thanks in advance.

rok
  • 9,403
  • 17
  • 70
  • 126
Hristo Stoyanov
  • 1,508
  • 3
  • 15
  • 24

3 Answers3

4

You can also use the synchronize command ~ examples provided in this link:

http://opensolitude.com/2015/05/26/building-docker-images-with-ansible.html

dshenoy
  • 753
  • 1
  • 5
  • 11
  • Thanks for pointing me to "synchronize". That's sounds better than using " copy" as my workaround suggested. I filed request with ansible to extend the docket module, but for now this is the best we have... – Hristo Stoyanov Aug 17 '15 at 18:13
2

Using ansible shell module helped:

  - name: copy db dump to localhost
    ansible.builtin.shell: docker cp container:/tmp/dump.sql /tmp/dump.sql
rok
  • 9,403
  • 17
  • 70
  • 126
1
  1. You didn't. Ansible's docker module does not support copying files or folders from a container.
  2. There is no simple way to do so. I mean you need a hack. Off the top of my head you can play with '-' argument for docker cp option.

However from my point of view if you wish to copy something into a container you're probably doing something wrong. Containers should be ephemeral.

hostmaster
  • 1,822
  • 16
  • 17
  • 5
    There are lots of valid reasons to copy files into and out of containers, which is why docker itself has the functionality and is expanding it. – mpeters Aug 17 '15 at 13:50
  • 1
    Probably, however from my experiance such cases mean a bad design – hostmaster Aug 17 '15 at 16:23
  • 2
    Agreed on the bad design but the most useful related case is using ansible to copy a file into a docker named volume. – DMCoding Dec 01 '16 at 15:57
  • I would disagree on that it categorically means a bad design. For example I use NiFi in container and need to copy DB drivers (SAP ASE) into there. I could do mount, but it feels even more ugly. – Rao Oct 26 '20 at 15:34