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
6,764 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,460 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 34,246 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
899 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,611 views
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
2,649 views
+4 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
47,354 views
+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,350 points

edited Jan 16, 2020 by Kalgi 31,669 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,620 points
1,153 views
+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
28,608 views
+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,460 points
147,274 views
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,460 points
7,021 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP