How to write ansible play book to install docker and start docker container with docker image on multiple host machines

0 votes
Oct 22, 2020 in Docker by Shashi
• 360 points

1 answer to this question.

0 votes


You can do this task easily with Ansible. First, update the Ansible inventory file in your controller node. After that, you need to create an Ansible playbook to configure the docker container. You can follow the below steps.

  • Install the Docker container with the package module.

  • Start the Docker service with the service module.

  • Launch the container.

answered Oct 22, 2020 by MD
• 95,300 points
I am aware of these steps. Can you please provide me a script which is written in yaml


According to your requirement, you can use the below playbook. You may need to modify the software name depending on your O.S.

- hosts: localhost
  - name: Install docker.
      name: docker-ce
      state: present
  - name: Start docker service
      name: docker
      state: started
  - name: Create a redis container
      name: data1
      image: centos
      state: started
      recreate: yes
        - 8080

Related Questions In Docker

+4 votes
4 answers

How To Access a Service on Host From a Docker Container?

Adding to kalgi's answer, You can also ...READ MORE

answered Oct 16, 2018 in Docker by lina
• 8,220 points

edited Oct 16, 2018 by lina 24,179 views
0 votes
1 answer

How to run an image with volume on docker for windows?

Try : declaring the volume of container mounting the ...READ MORE

answered Sep 20, 2018 in Docker by Tyrion anex
• 8,690 points
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
0 votes
1 answer

How to store data in external drive with Docker Postgres:9.3 image?

Apparently, the problem would be in your ...READ MORE

answered Jul 12, 2018 in Docker by Kalgi
• 2,680 points
+3 votes
4 answers

How to install packages inside a docker Ubuntu image?

Execute the update and the install command ...READ MORE

answered Apr 24, 2019 in Docker by Vismaya
+1 vote
1 answer

How to expose docker container's ip and port to outside docker host without port mapping?

you can accomplish this with IP aliasing ...READ MORE

answered Aug 2, 2018 in Docker by Kalgi
• 52,370 points

edited Jan 16, 2020 by Kalgi 25,310 views
0 votes
1 answer

How to base a docker volume with docker container?

Run the following command: docker run -d --volumes-from ...READ MORE

answered Aug 5, 2018 in Docker by Sophie may
• 10,550 points
+1 vote
2 answers

How do I copy a file from docker container to host?

Here is a way which you can ...READ MORE

answered Aug 28, 2018 in Docker by Damon Salvatore
• 5,980 points
+1 vote
1 answer

How to install packages in a Docker container?

Hi@akhtar, To install packages in a docker container, ...READ MORE

answered Apr 15, 2020 in Docker by MD
• 95,300 points
0 votes
1 answer

How to remove a docker container using Ansible playbook?

Hi@akhtar, You can set the state=absent in your ...READ MORE

answered Sep 20, 2020 in Docker by MD
• 95,300 points