# Run Ansible Playbook inside Docker container?

**URL:** <https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639>\
**Category:** Ansible Project\
**Created:** [June 29, 2015, 11:10pm UTC](https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639 "2015-06-29T23:10:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alejandro](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alejandro](https://forum.ansible.com/u/Alejandro)\
**Post date:** [June 29, 2015, 11:10pm UTC](https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639/1 "2015-06-29T23:10:12Z")

</div>

Hi All,

I like to know if someone is using Ansible to provisioning inside a Docker container… and how use this?

I has all my infrastructure in Playbooks, now l like to use this to create Dockers for each of my microservices… but I can’t found some alternatives…

As we know Ansible use SSH but de best practice of Docker is NOT use SSH to login into container… for this reason my doubt…

Some idea or advices about how use this?

Thanks  
Alejandro

---

<div class="post-metadata">

**Author:** ![Erik\_Anderson](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@Erik\_Anderson](https://forum.ansible.com/u/Erik_Anderson)\
**Post date:** [June 30, 2015, 5:25am UTC](https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639/2 "2015-06-30T05:25:38Z")

</div>

Sure, the general pattern I follow is thus:

1. I use a base docker image that already has ansible installed.  
2. In your Dockerfile, use the COPY command to copy your ansible files  
(playbook yaml, files, etc.) into the container. Something like:

COPY ansible/ /tmp/ansible/

3. After the copy, in your Dockerfile, run ansible-playbook on the  
files you copied into the container in the previous step:

RUN ansible-playbook /tmp/ansible/foo.yml -i /tmp/ansible/hosts

In this case, /tmp/ansible/hosts just contains:

[local]  
127.0.0.1

And in the playbook, I specify:

- hosts: local  
&nbsp;&nbsp;connection: local

4. After the playbook completes, remove the ansible files you copied  
in during step 2.

---

<div class="post-metadata">

**Author:** ![Michael\_Merritt](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Merritt](https://forum.ansible.com/u/Michael_Merritt)\
**Post date:** [May 19, 2016, 6:27am UTC](https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639/3 "2016-05-19T06:27:52Z")

</div>

Thank you! This got me up and going fast.

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [May 19, 2016, 7:27pm UTC](https://forum.ansible.com/t/run-ansible-playbook-inside-docker-container/18639/4 "2016-05-19T19:27:20Z")

</div>

I hadn’t thought of using Ansible to create Docker images before, but this is an interesting way to do this, instead of using a complicated Dockerfile to manage each variety of service.

I might want to figure out a way to be able to build an image for each group in your hosts file.
