Python Flask App deploy, push image to ECR
Steps: [Note: output success, complete blog]
video link: https://youtu.be/Q5sTM5LuhuI?si=CiZJW-pqg9nkG60V
Jenkins server to be up & running
Docker Installed on Jenkins server and Docker plug-ins installed on Jenkins dashboard
IAM Roles and aws CLI install, aws configure, ECR Repo create
create Jenkins pipeline and build it
github link: https://github.com/Narian318/nn-ECR-Jenkins.git
IAM Roles: AmazonEC2ContainerRegistryFullAccess, AmazonEC2FullAccess ---> attach this role to jenkins EC2 server
In Jenkins:
docker-cred: add dockerhub username, password or access token
aws-cred: add aws IAM user access key, secret access key in jenkins credentials
Load Balancer DNS Link or EC2 PublicIP:8096 for accessing python-flask-webapp
sudo yum update -y
#jenkins Install
sudo wget <link from jenkins.io downloads page for linux>
sudo yum upgrade -y
sudo amazon-linux-extras install java-openjdk11 -y
sudo yum install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
# ec2publicIP:8080, next run below command in ec2 terminal
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
# copy & paste above password in jenkins dashboard to login
# docker install
amazon-linux-extras install docker -y
service docker start
systemctl enable docker
sudo usermod -aG docker ec2-user
sudo usermod -aG docker jenkins
sudo service jenkins restart
sudo systemctl daemon-reload
sudo service docker stop
sudo service docker start
# Dockerfile
FROM alpine:3.5
RUN apk add --update py2-pip
COPY requirements.txt /usr/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
EXPOSE 5000
CMD ["python", "/usr/src/app/app.py"]
# Pipeline [Note: xxxx.dkr.ecr.....it is ecr repo uri link]
pipeline {
agent any
environment {
registry = "xxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/nn-ecr-jenkins"
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/Narian318/nn-ECR-Jenkins.git'
}
}
stage('Building image') {
steps{
script {
dockerImage = docker.build registry
}
}
}
stage('Pushing to ECR') {
steps{
script {
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', credentialsId: 'aws_cred', accessKeyVariable: 'AWS_ACCESS_KEY_ID', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
sh 'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin xxxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com'
sh 'docker push 167613117387.dkr.ecr.us-east-1.amazonaws.com/nn-ecr-jenkins:latest'
}
}
}
}
stage('stop previous containers') {
steps {
sh 'docker ps -f name=mypythonContainer -q | xargs --no-run-if-empty docker container stop'
sh 'docker container ls -a -fname=mypythonContainer -q | xargs -r docker container rm'
}
}
stage('Docker Run') {
steps{
script {
sh 'docker run -d -p 8096:5000 --rm --name mypythonContainer xxxxxxx.dkr.ecr.us-east-1.amazonaws.com/nn-ecr-jenkins:latest'
}
}
}
}
}
Comments
Join the discussionNo comments yet. Be the first to comment.