This project involves deploying a Ghost Blog with a MySQL database using Docker, Docker Compose and Terraform on an AWS EC2 instance running Ubuntu.
The infrastructure is provisioned through Terraform, which automates the setup of the EC2 instance and SSH key configuration. Docker and Docker Compose are used to containerize and orchestrate the Ghost and MySQL services, enabling efficient application management. The AWS CLI is utilized for secure interaction with AWS services, while tools like VS Code and SCP assist in code transfer and editing.
During implementation, several issues were encountered and resolved, including AWS CLI configuration problems, SSH permission errors, Git initialization issues, and Docker service failures. Troubleshooting involved checking file permissions, correcting SSH key paths, installing necessary packages, and validating Docker Compose syntax. Successful deployment was verified through Terraform commands and by accessing the blog via the EC2 public IP. Key Docker commands were used to manage containers, ensuring the blog and database were running as expected.
The document outlines the steps to deploy a Ghost Blog and MySQL using Docker, Docker Compose and Terraform on an AWS EC2 instance. Key tools include:
- Terraform: For infrastructure provisioning.
- Docker/Docker Compose: For application containerization.
- AWS CLI: For interaction with AWS services.
- vsCode: Code Editor
- Ubuntu Linux environment:
- AWS account with access keys:
1.AWS CLI Configuration Problems: If aws configure fails, verify that your AWS Access Key and Secret Key are correct.
-
Run aws configure and input your credentials
-
Confirm setup with aws sts get-caller-identity
2.Permission Denied During SSH: Ensure you're using the correct private key and the correct username (ec2-user).And ensure you're using the correct private key with the -i option.
Example: ssh -i /path/to/your-key.pem ec2-user@98.80.171.157
ssh -i /root/.ssh/docker ec2-user@98.80.171.157Issue: Git init shows .git: Permission denied
Fix Permission
-
sudo chown -R administrator:administrator/home/administrator/Terraform-Docker
-
sudo chmod u+rwx /home/administrator/Terraform-Docker
4.Terraform Plan Errors: Check if the paths to private/public keys are correctly defined in the variables.tf file (under modules -compute - variables.tf)
Issue: Invalid SSH key path error like "C:\\Users\\wessa\\.ssh\\id_rsa.pub"
Fix: Update variables.tf to point to a valid Unix-style path, e.g. /root/.ssh/docker.pub
-
sudo systemctl status docker
-
sudo systemctl start docker
-
sudo systemctl enable docker
-
docker run docker.io/hello-world: Run the hello-world container image to verify installation
Issue: Blog not loading in browser
Fix:
Check running containers: docker ps
Check not running containers: docker ps -a
Restart container: docker start ghost-blog
View logs for errors: docker logs ghost-blog
Restart services: docker-compose up -d
Fix:
Run docker compose config to validate
Check for YAML syntax issues like incorrect indentation, colons, or missing values
8.Access denied to files After transferring the files from windows to linux you need to give following permission so that we can save and edit files
sudo chown -R administrator:administrator /home/administrator/Terraform-Docker
sudo chmod -R u+rwX /home/administrator/Terraform-DockerUse scp to transfer your Terraform and docker files from your local machine to your Ubuntu instance:
Note: Transfer the terraform and docker code from local machine to Linux by running following command on cmd
scp -r -v "C:\Users\Gurpreet\OneDrive\Desktop\York Univ\Assignments\Assignment 4 - Docker\Terraform-Docker" administrator@10.0.0.83:/home/administratorEnter your password of ubuntu user
It shows the following screen after transfer
Make git repo:
administrator@Cloudinfotech-Server:~/Terraform-Docker$ git init
/home/administrator/Terraform-Docker/. git: Permission denied
Fix Permission
sudo chown -R administrator:administrator /home/administrator/Terraform-Docker
sudo chmod u+rwx /home/administrator/Terraform-Dockersudo apt update && sudo apt install -y gnupg software-properties-common curlcurl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpgecho "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.listsudo apt update
sudo apt install terraform -yterraform -vAfter running the terraform plan if you see the above error than you need to configure aws account as it found no aws credentials
To install the AWS CLI, run the following command
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/installRun the following command to check if AWS CLI is installed correctly:
aws –versionYou see the following output
After Creating
Click on account name - Select Security Credentials
Click Create access key.
Note: Download the key file or copy the Access Key ID & Secret Access Key (Secret Key is shown only once!).
After install and creating AWS account configure the AWS
Configure AWS CLI with the New Access Key:
aws configureIt will prompt you for:
1.AWS Access Key ID: Your access key from AWS IAM.
2.AWS Secret Access Key: Your secret key from AWS IAM.
3.Default region name: (e.g., us-east-1, us-west-2).
4.Default output format: (json, table, text — default is json).
Enter access key and secret key which you will get from aws account
Check credentials added to aws configure correctly
aws sts get-caller-identityIf your AWS CLI is properly configured, you'll see a response like this:
Run Terraform plan
If it shows the following error
Error: Invalid function argument
│ on modules/compute/main.tf line 17, in resource "aws_key_pair" "aws-key":
│ 17: public_key = file(var.ssh_key_public)
│ ├────────────────
│ │ while calling file(path)
│ │ var.ssh_key_public is "C:\\Users\\wessa\\.ssh\\id_rsa.pub"Then in this case you must update the location of public and private keys under modules -compute - variables.tf
As shown bellow in image
Provision terraform managed infrastructure. You must confirm by trying yes if you would like to continue and perform the actions described to provision your infrastructure resources
If everything works fine at end you will see the public Ip
ssh ec2-user@ 98.80.171.157
Ensure you're using the correct private key and the correct username (ec2-user) and ensure you're using the correct private key with the -i option.
Example
ssh -i /path/to/your-key.pem ec2-user@98.80.171.157
ssh -i /root/.ssh/docker ec2-user@98.80.171.157you will see ec2 instance connected remotely with your ubuntu instance
yum install docker -ysystemctl status dockersystemctl start dockersystemctl enable dockerdocker run docker.io/hello-worldRun the hello-world container image to verify installation:
Install docker composesudo yum install -y py-pip, python3-dev, libffi-dev, openssl-dev, gcc, libc-dev, rust, cargo, makesudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-composeCheck docker compose installdocker compose version:
Create Docker Compose File:
vi docker-compose.ymlcopy and paste yml file
for check it save properly
run
cat docker-compose.ymlIf you're working with Docker Compose, you can validate your docker-compose.yml file by running:
docker compose configif it shows some error as follows than there is syntax error in your yml file
Correct the syntax and correct error of your yml file again run
Docker compose configIf it shows the full code than your yml file works fine
Start up the Docker Compose service:
Run
docker compose up -dExecute a compose up and use the detached flag to run the container in the background:
Verify that your app is up and running using the public IP address of your EC2 instance
Some important command of docker:
docker info**docker ps:** shows only running containers**Docker ps -a**It shows all the containers
To stop the container
**Docker rm <container_name>:**To delete the container
Docker start <container_name>:
to start the container
**Docker search <image_name>**Ex: docker search ubuntu: it search all the images regarding ubuntu present on ubuntu hub
**Docker exec -it <container_name>**To go inside the running docker






















