This project uses Ansible to automate the installation of Docker on a group of servers and deploy a simple Docker container.
-
Ansible Installed: Ensure Ansible is installed on your control node (the machine from which you'll run Ansible).
To Install Ansible on a Red Hat-based system:
sudo yum install epel-release -y sudo yum install ansible -y
-
SSH Access: Ensure you have SSH access to all target servers where Docker will be installed. Setting up SSH keys for passwordless access is recommended.
-
Verify Ansible Installation:
ansible --version
-
Set Up SSH Access:
- Make sure you have SSH access to the target servers. You might need to configure SSH keys for passwordless login.
- Create an Inventory File:
- Create a file named
hostsin your project directory with the following content:
[docker_hosts] server1.example.com server2.example.com- Replace
server1.example.comandserver2.example.comwith the actual hostnames or IP addresses of your target servers.
- Create a file named
-
Create a Playbook File:
- Create a file named
setup_docker.ymlin your project directory with the following content:
--- - name: Install Docker and run a container hosts: docker_hosts become: yes tasks: - name: Install required system packages yum: name: "{{ item }}" state: present loop: - yum-utils - device-mapper-persistent-data - lvm2 - name: Add Docker repository command: > yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo args: creates: /etc/yum.repos.d/docker-ce.repo - name: Install Docker yum: name: docker-ce state: present enablerepo: docker-ce-stable - name: Start and enable Docker service: name: docker state: started enabled: yes - name: Run a simple Docker container (Hello World) docker_container: name: hello-world image: hello-world state: started
Explanation of Playbook:
- Install Required Packages: Installs necessary system packages for Docker.
- Add Docker Repository: Adds the official Docker repository to the system so that Docker can be installed.
- Install Docker: Installs Docker from the added repository.
- Start and Enable Docker: Starts Docker service and ensures it starts on boot.
- Run a Docker Container: Pulls the
hello-worldDocker image and runs it to test Docker installation.
- Create a file named
-
Execute the Playbook:
- Run the playbook using the following command:
ansible-playbook -i hosts setup_docker.yml
- This command tells Ansible to use the
hostsinventory file and run the tasks defined in thesetup_docker.ymlplaybook on the servers listed under the[docker_hosts]group.
-
Verify Docker Installation and Container Execution:
- SSH into one of your servers and check if Docker is running:
systemctl status docker
- To verify that the
hello-worldcontainer ran successfully, use:
docker ps -a
- Deploy Different Docker Containers: Modify the playbook to deploy different Docker containers or services as needed.
- Create Docker Networks: Use Ansible to create Docker networks if your containers need to communicate with each other.
- Use Docker Compose: Extend the playbook to install Docker Compose and deploy multi-container applications using Compose files.
This project demonstrates how to automate the installation of Docker and the deployment of a simple Docker container using Ansible. Feel free to extend this project to suit your needs for managing Docker across multiple servers.
