This is beginner's dairy of getting started with deploying a Docker Container, serving a simple web page, deployment being done with Ansible. Know more about What is Docker? and What is Ansible?
This project consists of three components:
- A static web page that is to be displayed
- A Docker container serving this webpage, using Ngnix on the server
- Ansible script that deploys the Docker container on to a server machine and gets it up and running
Ansible script will be run from a local machine/another server which will connect, install required packages on the actual server, download the docker container image, deploy and launch the container.
Ansible to be installed. If not installed, install with:
sudo apt update && sudo apt install ansible
Although any remote server can be used, we will be using AWS EC2 Ubuntu Linux 18.04 Server for this project. You can get access to AWS EC2 with Amazon Free Tier plan.
The EC2 instance needs to be launched with the following configuration/settings:
- Ubuntu 18.04 Server Image for the VM
- Default Free Tier CPU & Storage Specs
- Security Groups with incoming traffic open to SSH on port 22 and HTTP on port 8888, as shown below:
Type | Protocol | Port Range | Source |
---|---|---|---|
Custom TCP Rule | TCP | 8888 | 0.0.0.0/0 |
SSH | TCP | 22 | 0.0.0.0/0 |
- SSH access to EC2 instance with a private-public key set up. Add your private key file to SSH with
ssh-add /path-to-private-key/AWSkey.pem
-
Clone this repository onto a folder on local machine/control server
$ git clone https://github.com/yakshaG/Ansible-Docker-AWS.git
-
Grab your EC2 instance IP. Navigate your terminal to the cloned directory, edit the hosts file with your favourite text editor and replace the server IP with your EC2 instance IP.
$ nano hosts
-
Run the Ansible Playbook MainScript.yml in the cloned directory:
$ ansible-playbook MainScript.yml -i hosts
Each step might take a few minutes depending on the bandwidth and server resources. -
Finally you would get a message displaying PLAY RECAP and successful run of the Playbook.
-
Access the site with your EC2 instance IP on port 8888, from any web browser, like this:
12.13.14.15:8888
-
There we get the site, running on a single docker container with the following message!
Hi from Docker Container! This page is being served from a docker container running Nginx, hosted on AWS EC2 Instance.
Take a look at the web page being served from the container on: 18.224.138.221:8888
(Above server will remain active till 29 Feb 23:59 IST)
- Check if your server is accessible over SSH with
ssh ubuntu@PublicDNS
. If not check inbound traffic rules in the EC2 security groups or issues with the private key file permissions.
- Check if the target server IP is correctly mentioned in the hosts file
- Check SSH access as above
- Run
$ ansible-playbook AnsiblePing.yml -i hosts
to check if the connection attempt returns OK message. If not, SSH access needs fixing. - Default user of the target server. If the user to be used is other than
ubuntu
, change theremote_user
value inMainScript.yml
file.
- Remove cloned directory on the EC2 Server home folder, by logging in via SSH