# Getting Ansible to run commands inside a running Docker container

**URL:** <https://forum.ansible.com/t/getting-ansible-to-run-commands-inside-a-running-docker-container/20467>\
**Category:** Ansible Project\
**Created:** [February 5, 2016, 5:59pm UTC](https://forum.ansible.com/t/getting-ansible-to-run-commands-inside-a-running-docker-container/20467 "2016-02-05T17:59:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andre\_Castelan\_Prado](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@Andre\_Castelan\_Prado](https://forum.ansible.com/u/Andre_Castelan_Prado)\
**Post date:** [February 5, 2016, 5:59pm UTC](https://forum.ansible.com/t/getting-ansible-to-run-commands-inside-a-running-docker-container/20467/1 "2016-02-05T17:59:30Z")

</div>

Hello guys, I am using Ansible + Vagrant and having problem to run Django migrations inside a container that’s running my django app.

Basically I have a postgres container and a django container

If I vagrant ssh and then run

sudo docker exec -ti django /bin/sh -c ‘cd mumopus;python manage.py migrate’

Everything is good and life is beautiful

However if I try to run as an ansible task having

- name: Run migrate on Django.  
become: yes  
command: docker exec -ti django /bin/sh -c ‘cd mumopus;python manage.py migrate’

I get

“django.db.utils.OperationalError: could not connect to server: Connection refused”, “\tIs the server running on host "172.17.0.2" and accepting”, “\tTCP/IP connections on port 5432?”]

I’ve run docker ps just prior to that task and all the containers are there and running, I’ve also tried to use ansible shell instead of command but no joy. I’ve made a thread in reddit with the codes, if anyone can help I would be very thankful

[https://www.reddit.com/r/ansible/comments/44ao3t/ansible\_command\_module\_in\_playbook\_in\_role\_not/](https://www.reddit.com/r/ansible/comments/44ao3t/ansible_command_module_in_playbook_in_role_not/)

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [February 7, 2016, 8:42pm UTC](https://forum.ansible.com/t/getting-ansible-to-run-commands-inside-a-running-docker-container/20467/2 "2016-02-07T20:42:34Z")

</div>

That's manage.py complaining that postgres isn't running where it  
expects it to be, my guess would  
be you're missing an environment variable to point to the correct  
ip/port combo (if you're sure a straight  
ssh session works) or you haven't started postgres in the container.
