How to Deploy a Docker Container to AWS Elastic Beanstalk

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

If you are building a website, you will often find yourself dealing with web servers and databases. While the basics for deploying these services aren’t overly complicated, there is some complexity when it comes to setting them up on Amazon Web Services (AWS). In this post we’ll walk through how to deploy your first Docker container onto an AWS Elastic Beanstalk environment.,

How to Deploy a Docker Container to AWS Elastic Beanstalk

The “deploy docker image from ecr to elastic beanstalk” is a process that takes time. It will take some time for the new container to be created, but it is worth it.

Do you need to install Docker containers automatically? Have you heard of Amazon Web Services’ Elastic Beanstalk? Another fantastic solution for delivering and scaling web apps and services is AWS Elastic Beanstalk.

In this article, you’ll learn how to use the EB CLI and GitHub Actions to deploy an application to AWS Elastic Beanstalk.

Ready? Continue reading to learn how to scale your apps.

Prerequisites

This will be an interactive presentation. If you want to follow along, make sure you have these items:

Creating a Static Web Host in Docker

You’ll need an app to deploy before you can demonstrate how to deploy a Docker container. You’ll need an HTML file for this demonstration.

1. To clone an HTML file from a GitHub repository, use the git clone command.

2. Next, in the root of the repository you cloned (step one), build a Dockerfile with your favourite text editor and fill it with the code below.

The HTML code output is seen in the code below (index.html).

# Obtain the NGINX base image from nginx ADD index.html /usr/share/nginx/html/ to the nginx index file # Allow elastic beanstalk to connect to the Docker container by exposing a port. EXPOSE 80

Getting Started with NGINX on Docker is related.

3. In the working directory, run the docker build command to create a Docker container of your HTML file named html.

4. Now, launch the Docker container (html) you created before using the docker run command (step three).

The —name parameter instructs Docker to refer to the container by its name (justhtml) in the network. While the -p option sets your localhost’s port (8080) to the Docker container’s port (80).

—name justhtml -p 8080:80 html docker run

5. Finally, go to http://localhost:8080/ using your web browser. to make sure you can get inside the Docker container (HTML).

If everything goes properly, you’ll see the HTML page below.

The HTML page is being run.The HTML page is being run.

Using the AWS CLI to deploy an application to Elastic Beanstalk

It’s time to deploy your application to AWS Elastic Beanstalk now that you’ve confirmed your Docker container is available on your host. To deploy your Docker container (html) to AWS Elastic Beanstalk, you’ll utilize the EB CLI.

You don’t need to do anything to set up the EB CLI if you already have the AWS CLI set up. Why? The credentials for EB CLI are the same as for AWS CLI.

1. To create an Elastic Beanstalk environment for your project, use the eb init command. Elastic Beanstalk uses the environment to figure out what sort of application you wish to deploy.

You will be prompted to fill in the information after executing the program. For this example, the following settings were utilized.

  • You seem to be using Docker. Is this accurate? Y
  • 1 Choose a Platform Branch
  • Do you want to keep using CodeCommit? N
  • Do you wish to use SSH to connect to your instances? N

Getting Started with an Elastic Beanstalk EnvironmentGetting Started with an Elastic Beanstalk Environment

2. Next, use the eb create command to build an Elastic Beanstalk environment for your application. Simply said, you deploy your application and Elastic Beanstalk takes care of the rest, such as load balancing.

If you’ve set up Git in your project, make sure you commit all of the changes before creating an Elastic Beanstalk application. Why? The EB CLI utilizes the most current commit’s Git archive.

Because you’re merely working on an HTML app, press Enter to utilize the defaults for an Environment Name, DNS CNAME prefix, and load balancer type.

However, when requested to allow Spot Fleet requests, enter n, since this option gives optional On-Demand Instances, which are not required in this case, and then click Enter.

Creating the Application's EnvironmentCreating the Application’s Environment

When you originally executed the eb create command, an auto-scaling group was created for you.

Over a five-minute period, the default triggers scale up when the average outgoing network traffic from each instance exceeds 6 MB and scales down when the outbound network traffic exceeds 2 MB.

Application scaling is unlikely to occur since your application is merely HTML.

3. Finally, use the eb open command to launch your application straight from Elastic Beanstalk in your preferred web browser.

The HTML application is launched.The HTML application is launched.

AWS Elastic Beanstalk Application Validation

After you’ve deployed your app, you’ll be able to view all of the useful features you’ve included. The first step is to determine where Elastic Beanstalk saved the application file.

To get access to the application file, follow these steps:

1. Log in to the AWS console using the user that was specified in the AWS CLI setup.

2. Select S3 from the All Services drop-down menu, then click the Storage tab.

Using the S3 DashboardUsing the S3 Dashboard

3. Go to the S3 dashboard and click on your project to view the details of your application.

Information about the applicationInformation about the application

Your program appears in the Objects list, as seen below.

 S3 Application Information Viewing S3 Application Information Viewing

4. Finally, pick EC2 from the All Services drop-down, then click the Compute tab, then select EC2.

Finding the EC2 dashboardFinding the EC2 dashboard

You can view the Resources on EC2 that are executing your application on the EC2 dashboard, which looks like the one below. But keep in mind that the information below may vary from yours.

Resources on EC2Resources on EC2

You may also use the eb status command to get additional information about your application.

Viewing additional information about the application Viewing additional information about the application

AWS Elastic Beanstalk Application Update

When you’ve made modifications to your application, use the eb deploy command to generate and deploy a new version.

Open your HTML file (index.html) and replace what you have in the <style> tag with the one below. The code below changes the background color of your application .

<style> body{ background-color: rgb(0, 255, 55); } </style>

To deploy the modifications and launch your application in your web browser, use the instructions below.

When the deployment is finished, your application will open in your browser with the modifications you made.

Verifying the Application's ChangesVerifying the Application’s Changes

Using GitHub Actions to deploy an application to Elastic Beanstalk

GitHub Actions, in addition to the EB CLI, is another handy method to publish your Docker container to Elastic Beanstalk. You may use GitHub Actions to automate tedious tasks while developing. You create the instructions and publish them to GitHub using GitHub Actions.

How to Manage GitHub Actions Secrets and Environment Variables

When you make modifications right now, you have to perform the deployment command yourself. However, in this demonstration, you’ll use GitHub Actions to automate the deployment process.

If you’re building a pipeline that runs tests and conducts additional checks, this configuration is ideal.

1. In the root directory of your project, create a directory called.github/workflows. Your workflow file will be saved in this directory.

2. In the.github/workflows directory, create a file with your selected name. However, in this case, the file is called main.yml.

Copy and paste the following code into the main.yml file to do the following:

  • Creates a task that downloads the most recent push to your repository and zips it (Elastic Beanstalk requires you to zip your project folder).
  • The timestamp is formatted, and the prepared timestamps are used as the version label.
  • Elastic Beanstalk should be used to deploy the repository.

name: Github To AWS Deployment on: When a push is made on the main branch, the workflow is activated: branches: – primary responsibilities: deploy: executes # the operating system your task will execute on -on: ubuntu-latest – Checkout’s name Uses: actions/[email protected] to check out your repository inside the GitHub workspace so that your workflow may access it. – name: Deployment Package Generator # except, zip all files. zip -r deploy.zip * -x ‘*.git*’ git run Uses: gerred/actions/[email protected] – name: Get timestamp current-time id – name: Replace string with frabert/[email protected] # substitutes ‘[:.]+’ in the timestamp with ‘-‘ pattern: id: format-time with: replace-with: ‘-‘ flags: ‘g’ string: “$ steps.current-time.outputs.time ” – name: einaregilsson/[email protected] for EB purposes with: # aws access key: AKIATI3RC11111YQ3TAU aws secret key: Urr46HfaaaaabbbbbcccaC/+YpWFtJFbRQN27xF aws secret key: Urr46HfaaaaabbbbbcccaC/+YpWFtJFbRQN27xF aws secret_ region: us-west-2 deployment package: deploy.zip application name: html environment name: html-dev3 version label: “$ steps.format-time.outputs.replaced”

In a live project, you should typically save passwords in GitHub Secrets.

3. To commit and publish the code to your GitHub repository, run the following commands on the root of your project.

# Adds changes to the staging area using git add. # commits your modifications using git commit -m “update” git push # GitHub push

4. Now open your web browser and go to your GitHub project. Go to the Actions tab and choose your current commit.

You may ensure that your application is deployed by looking at the picture below.

In GitHub, verifying the deployed applicationIn GitHub, verifying the deployed application

5. Finally, run your program using the command below.

The GitHub Actions successfully deployed the modifications, as seen below.

GitHub Actions Verification Changes were implemented.GitHub Actions Verification Changes were implemented.

Conclusion

You’ve learnt how to use EB CLI and GitHub Actions to deploy an application to Elastic Beanstalk through the console in this article.

You should know how to deploy your application to Elastic Beanstalk by now, so you may choose the method that best meets your requirements.

How do you intend to use your acquired knowledge? Deploying a full-fledged application, perhaps?

The “elastic beanstalk docker tutorial” is a guide to deploying a Docker container on AWS Elastic Beanstalk.

Related Tags

  • aws elastic beanstalk docker
  • docker-compose elastic beanstalk
  • elastic beanstalk docker-compose example
  • elastic beanstalk docker image
  • elastic beanstalk update docker image