We will learn how to automate Docker builds using Jenkins. We will use
Python based application. I have already created a repo with source code
+ Dockerfile. We will see how to create Docker image and upload into AWS ECR successfully.
Pre-requisites:
1. Jenkins is up and running
2. Docker installed on Jenkins instance
3. Docker and Docker pipelines plug-in are installed and Amazon ECR plug-in installed
4. Repo created in ECR, Click here to know how to do that.
5. port 8096 is opened up in firewall rules.
6. Access keys + secret keys from AWS account
Step # 1 - Add ECR Plug-in
Go to Jenkins, Manage Jenkins, Add Amazon ECR plug-in
Step #2 - Create Credentials for AWS ECR
Go to your Jenkins where you have installed Docker as well.
Go to credentials -->
Click on Global credentials
Click on Add Credentials
Choose AWS credentials
Add your AWS access keys and secret keys and save it
Note down the ID after saving.
Step # 3 - Create a scripted pipeline in Jenkins, name can be anything
Step # 4 - Copy the pipeline code from below
Make sure you change red highlighted values below:
Your account_d should be updated and repo should be updated.
your registry credentials ID from Jenkins from step # 1 should be copied
pipeline { agent any environment { registry = "account_id.dkr.ecr.us-east-2.amazonaws.com/myphpapp" //- update your credentials ID after creating credentials for connecting to Docker Hub registryCredential = 'Copy_ID_from_step_no_1_above' dockerImage = '' }
Amazon Elastic Container Registry (ECR) is a fully managed container registry service provided by Amazon Web Services (AWS). It allows users to store, manage, and deploy Docker container images. Amazon ECR uses Amazon S3 for storage to make your container images highly available and accessible, allowing you to reliably deploy new containers for your applications. We will learn how to create ECR in AWS, will create a docker image and upload docker image into ECR.
You can watch the steps on YouTube as well:
What are we going to do in this lab?
1. Create a Repository in AWS ECR
2. Create an IAM role with ContainerRegistryFullAccess
Where your_acct_id is from AWS ECR in the above picture.
You must get a message says Login succeeded. Now let's build a docker image, I have already created a public repo in Bitbucket. All you need to do is perform the below command to clone my repo:
Setup SSH keys and upload public key into Ubuntu EC2 instance where LAMP stack will be installed.
Modify hosts/inventory file to add target EC2 instance ip address/dns name.
port 80 is opened in firewall rule for Apache
Steps to setup SSH keys:
1. Login to Ansible management server/machine. Create SSH keys in Ansible host machine by executing the below command: (if you already have keys created, please skip this step)
ssh-keygen
enter three times..now you will see keys successfully created. 2. Execute the below command on Ansible management node and copy the public key content: sudo cat ~/.ssh/id_rsa.pub
copy the above output. 3. Now login into target node where you want to install LAMP stack, execute the below command to open the file sudo vi /home/ubuntu/.ssh/authorized_keys type shift A and then enter now and paste the key in the above file. please do not delete any existing values in this file.
4. Now go back to Ansible mgmt node, do changes in /etc/ansible/hosts file to include the node you will be installing software. Make sure you add public or private IP address of target node as highlighted below in red color: sudo vi /etc/ansible/hosts [My_Group] xx.xx.xx.xx ansible_ssh_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_python_interpreter=/usr/bin/python3
Ansible playbook for installing LAMP(Linux Apache MySQL PHP) stack on Ubuntu