How To Build and Deploy Applications Using Docker

In this article, We will explore how we can Build and deploy applications using docker on an Amazon EC2 instance.

What is Docker?

Docker is an open-source platform for developing, testing, shipping, and deploying applications within containers. Docker provides a way to package and distribute applications with all their dependencies, ensuring consistency across different environments, such as development, testing, and production.

What is a Docker Container?

A container is a runnable instance that is both lightweight and portable. Containers are self-sufficient units that encapsulate the application code, runtime, system tools, libraries, and configurations needed to run the application.

What is a Docker Image?

A Docker image is a lightweight, standalone, executable package that contains everything required to execute a piece of software. These docker images are created from docker files. Docker files are text files that contain the instructions required to create a docker image.

What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) is a core component of Amazon Web Services (AWS) which provides computing resources such as CPU, memory, storage, and networking capabilities.EC2 instances enable developers to quickly provision virtual servers customised to their project requirements.

Prerequisite:

To build and deploy an application or server using Docker we require an AWS account.

How To Configure EC2 Instance?

  1. Login to AWS Account.
  2. Go to EC2 Dashboard.
  3. Click on Launch Instance.
  4. Give a name to your Instance.
  5. Select Amazon Machine Image(Ubuntu for this project).
  6. Select Instance type.
Configure EC2 Instance

7. Generate key-pair –> Give Key-pair name –> Select RSA key-pair type –> Choose .pem as file format –>Click Create key-pair.

Key-pair

8. Select the Newly created key from the dropdown.

9. Click on Edit network settings then click Add Security Group Rule. Configure network settings and security group rules to control inbound and outbound traffic, improving your instance’s security.

What is the Security Group Rule?

A security group rule is a set of instructions that controls the inbound and outbound traffic for instances in an Amazon Web Services (AWS) EC2 security group. Security groups act as virtual firewalls, allowing you to specify which traffic is allowed to reach your instances.

10. Add the port number you want to expose for example 8000 and add the CIDR block. Exposing ports allows remote access to the EC2 instances which is essential for enabling network communication and allowing access to the services hosted on the instances.

Custom security Group

11. Click on the Launch Instance button. Our EC2 instance is configured successfully and ready to use.

How to Connect to your EC2 instance?

Once our instance is configured, we can connect to it via SSH using either the AWS Management Console or a terminal window. Ensure the correct permissions are set for your key pair (.pem file) and execute the SSH command with your instance’s public IP address.

  1. Open your terminal and go to the location where .pem while downloaded (Check step 7, while generating key-pair).
  2. Change the permission.
chmod 400 "your pem file"

3. Execute below command.

ssh -i "your pem file" ubuntu@publicIPV4dns address.

Alternatively, these commands can be found from –>Click on Connect –>Select SSH client Tab –>Follow the instructions.

Installing Docker on EC2

Open the terminal window and update the repository with the below command.

sudo apt update

Now execute the install command to install the docker.

sudo apt install docker.io

Verify the installation by checking the Docker version.

docker --version

To start the docker service use this command.

sudo systemctl start docker

To check the status of Docker, use the below-mentioned command.

sudo systemctl status docker

To stop the docker, execute the following command.

sudo systemctl stop docker

How to Build a Docker Image?

To build a docker image, we need to create a docker file that will contain a series of instructions for Docker to follow. This docker file specifies the image’s configuration, dependencies, and setup instructions.

Sample Docker File Syntax:

#Using offical maven image as a parent image
FROM maven:3.5-jdk-8-alpine

#Setting the working directory to /app
WORKDIR /app

#Copy the current directory contents into the container at current directory
COPY . .

#Install the mvn command for maven 
RUN mvn install

A docker file contains a base image, set-up environments, COPY instruction to add files from our local directory into the Docker image, Expose ports (if we want to specifically expose a port for our application), startup commands etc.

Go to the EC2 terminal window and clone this repo. Navigate inside this repo, this repo contains a docker file. We need to build this docker file to create the image from it. Execute the below command with your desired image name.

docker build -t yourimagename

Once this command is executed successfully then check whether an image is created or not using the below command.

docker images

You will see an image created a few seconds ago with a random name and unique ID.

How to run the docker container?

Once the Docker image is successfully built, initiate a container from it using the ‘docker run’ command, specifying port mappings and any additional runtime parameters.

docker run -d -p 8080:8080 <image_name>

This command will run a container from the specified image in detached mode (-d) and map port 8080 of the host machine to port 8080 of the container (-p 8080:8080). Replace <image_name> with the name of the Docker image you built.

Access your application by navigating to your EC2 instance’s public IP address followed by the designated port in your web browser.

Deploy Applications Using Docker

To read more articles, please visit this section.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading