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

0 votes
Oct 22 in Docker by Shashi
• 320 points
87 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 by MD
• 77,580 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 17,875 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 19, 2018 in Docker by Tyrion anex
• 8,560 points
129 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
985 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,620 points
877 views
+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
19,415 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
• 51,970 points

edited Jan 16 by Kalgi 18,731 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,100 points
287 views
0 votes
2 answers

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

Instead of using docker run you could ...READ MORE

answered Aug 13, 2019 in Docker by Sirajul
• 58,210 points
21,437 views
0 votes
1 answer

How to install packages in a Docker container?

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

answered Apr 15 in Docker by MD
• 77,580 points
11,338 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 in Docker by MD
• 77,580 points
219 views