Automated Docker Image Build and Deployment with Jenkins in ec2 aws instance

Automated Docker Image Build and Deployment with Jenkins in ec2 aws instance

The blog will be divided into two parts

1 : Create a docker file & web pages push it into Git hub.

2 : Now create the custom image with the help of Jenkins & push the image to Docker Hub .

3 : Now pull the image & launch the container & expose the container.

Lets get started with creating files and pushing it into github

Take a powerfull ec2 instance otherwiseit will take too long.

Step 1: Install git and docker using yum command.

yum install docker

yum install git

Step 2: clone your repository using

git clonehttps://github.com/your-username/your-repository.git

Step 3: open your repo using cd repo_name

and check its status using git status

Step 4: Now we will create html file using vim index.html and write

<html> <head> <title>Hello, from Jenkins!</title> </head> </html>

Step 5: Now we will create dockerfile using vim Dockerfile and write

# Use the CentOS base image

FROM centos:7

# Install Apache HTTP Server

RUN yum install httpd -y

# Install net-tools

RUN yum install net-tools -y

# Copy the HTML file to the Apache document root

COPY index.html /var/www/html/index.html

# Expose port 80 to the outside world

EXPOSE 80

# Start the Apache HTTP Server

CMD ["httpd", "-D", "FOREGROUND"]

Step 6: now we will commit the index file and dockerfile in the repo using

git commit -m "file name"

Step 7: Configure the github account by giving user name and password.

Step 8: Run git push

Your files will be added to repository

Now we will create the custom image with the help of Jenkins & push the image to Docker Hub

Step 1: Create a freestyle job and give it a name.

Step 2: Click on git option to connect wit github repo then give address of the repo

https://github.com/user-name/jenkins.git

Step 3: Choose the execute shell in build steps and write these commands.

# Build the Docker image with tag v1(v1 is version)

docker build -t your_dockerhub_user_name/my-image:v1 .

# Login to Docker Hub

echo "password" | docker login -u your_dockerhub_user_name --password-stdin

# Push the Docker image to Docker Hub with tag v1

docker push your_dockerhub_user_name/my-image:v1

# Pull the Docker image from Docker Hub with tag v1

docker pull your_dockerhub_user_name/my-image:v1

# Run the Docker container with the pulled image

docker run -d -p 8081:80 your_dockerhub_user_name/my-image:v1

Step 4:click on build now to start building the project.

I have done this in t2.micro that's why it is taking too long if you want to do this fast then use c5.2xlarge types of powerfull instance.

Your image will be pushed in dockerhub.