5
votes

I want to execute shell commands, for eg: a "wget" command inside a running docker container using Ansible. This is the playbook I am trying to execute

---

- name: Enter into a running container and run a command
  docker_container:
    name: centos_conatainer
    state: started
    image: centos
    command: wget https://downloadlink.com

This stops the container and also it is not downloading the file. Is this the right way to execute shell commands using docker_container module, or is there any other way to do this using Ansible?

3
This is bad practice and not a good way to use Docker. You should probably rethink how you use Docker or use a VM. That said, Michael answers your question.MillerGeek
Re: duplicate, this answer from the other question describes another approach to this same problem using docker connection introduced in Ansible 2.0 and the raw module that isn't covered hereJasmine Hegman

3 Answers

5
votes

You are looking for the Ansible equivalent of the docker exec command-line.

Command in ansible docker_container is the equivalent of the command option in the docker run command-line.

It doesn't appear that this new Ansible module has support for this. You'll just have to use the generic Ansible command.

Example:

- name: Enter into a running container and run a command
  command: docker exec centos_container wget https://downloadlink.com
3
votes

AFAIK there is no way you can do it with docker_container module – it is used to start a new container with the specified command.

I use this code to execute commands inside containers:

- name: Execute command inside a container
  shell: "docker exec {{ containerName }} {{ commandToRun }}"