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

+1 vote
Oct 22, 2020 in Docker by Shashi
• 370 points
5,884 views

1 answer to this question.

0 votes

Hi@Shashi,

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,440 points
I am aware of these steps. Can you please provide me a script which is written in yaml

Hi@Shashi,

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
  tasks:
  - name: Install docker.
    package:
      name: docker-ce
      state: present
  - name: Start docker service
    service:
      name: docker
      state: started
  - name: Create a redis container
    docker_container:
      name: data1
      image: centos
      state: started
      recreate: yes
      exposed_ports:
        - 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 32,324 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,700 points
551 views
+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
3,034 views