Configuring Docker Container by Ansible


  • Docker
  • Docker SDK
  • Python3
  1. Pull Centos image using command
$docker pull centos:latest
$docker run -dit --name os1 centos:latest
$docker attach os1
$yum install openssh-server -y
$yum install passwd -y
$passwd root
$docker commit os1 centos_ssh:v1
- name: start a docker container
name: “os1”
image: centos_ssh:v1
state: started
— “81:22”
— “8080:80”
interactive: yes
tty: yes
restart: yes
register: x
- debug:
msg: this is the IP of docker {{ x.ansible_facts.docker_container.NetworkSettings.IPAddress }}
- blockinfile:
path: /etc/ansible/myhosts.txt
block: |
{{ x.ansible_facts.docker_container.NetworkSettings.IPAddress }} ansible_ssh_user=root ansible_ssh_pass=redhat

- hosts: docker #here docker contains the ip of contianer
— name: install httpd
name: httpd
state: present
— copy:
src: /root/index.html
dest: /var/www/html/index.html
— command: /usr/sbin/httpd #to start service



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store