Asked
Active
Viewed 928 times
1
I want to copy a file to a docker container, as one of my Ansible playbook steps. I create the file with jinja2 "template". I can copy the file in /tmp/ and the run a command to copy it to the docker container, such as:
`docker cp /tmp/config.json my_image:/app/config/path/`
But I'm looking for the better way not to use "/tmp" or like that.

Ali
- 337
- 1
- 5
- 15
1 Answers
1
Ansible has a docker
connection plugin that you can use to interact with existing containers in your playbook. For example, if I have a container named mycontainer
:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07899303ac55 alpine "sleep inf" 7 seconds ago Up 2 seconds mycontainer
I can create an Ansible inventory like this that sets the ansible_connection
variable to community.general.docker
:
all:
hosts:
mycontainer:
ansible_connection: community.docker.docker
Now I can target the container in a play like this:
- hosts: mycontainer
gather_facts: false
become: true
tasks:
- name: create target directory in container
file:
path: /target
state: directory
- name: copy a file into the container
copy:
src: example.file
dest: /target/example.file

larsks
- 277,717
- 41
- 399
- 399