paint-brush
How to Deploy a Docker Image From AWS ECR to Aptible in Three Stepsby@aahil
275 reads

How to Deploy a Docker Image From AWS ECR to Aptible in Three Steps

by AahilDecember 4th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Embrace Aptible, the platform that streamlines application deployment. With a focus on code and application development, Aptible manages infrastructure tools, including storage, servers, and security. Save resources with a pay-as-you-go model and enjoy a free trial. Our step-by-step guide demonstrates deploying a Docker image from AWS ECR to Aptible in three easy steps: build your Docker image, authenticate AWS ECR with Aptible, and deploy seamlessly. Aptible ensures efficient deployment, allowing you to focus on your application's success.
featured image - How to Deploy a Docker Image From AWS ECR to Aptible in Three Steps
Aahil HackerNoon profile picture

Why spend a ridiculous amount of time and resources trying to set up & manage your infrastructure when you can rely on Aptible to do it for you? Use these three steps to deploy a Docker image from your AWS ECR private repository to Aptible.

What is Aptible?

Aptible is a platform as a service that provides a scalable environment for deploying applications. In this environment, Aptible delivers development teams infrastructure tools such as storage, servers, load balancers, security & compliance, and more. These tools allow development teams to focus on writing code and deploying applications rather than managing infrastructure.


Companies can save money and all the resources needed to set up and manage their infrastructure by relying on Aptible. Aptible offers a pay-as-you-go pricing model, allowing companies to only pay for the resources they use. It also gives a free one-month trial to development teams testing the platform for the first time.


Using Aptible, you can deploy any application via the user-friendly Aptible dashboard. It also allows other deployment options, including Infrastructure as Code via Terraform and the Aptible CLI. All these platforms ensure that developers can deploy applications in the most comfortable manner.


How to deploy a Docker image from AWS ECR to Aptible

Project Overview

For this project, we'll use a JavaScript word counter application that counts the number of words in a sentence. This application will be packaged into a Docker image and deployed to a private AWS ECR repository. We'll then deploy this Docker image to Aptible using the Aptible CLI.


From this overview, we can see that there are three main steps to complete this project:


Step 1: Build a Docker image and push it to AWS ECR Step 2: Authenticate AWS ECR with Aptible Step 3: Deploy to Aptible



Figure 1: Project overview

Prerequisites

Before you begin this project, you'll need to have the following:

  • An AWS account. If you don't have one, you can create one on the official AWS website.
  • The AWS CLI is installed and configured with your AWS account. Make sure the AWS CLI has appropriate permissions to access AWS ECR.
  • An Aptible account. If you don't have one, sign up on the Aptible website.
  • The Aptible CLI. You can install it using the Aptible CLI documentation.
  • Docker. You can install it here.


Once you have created the following accounts and installed the necessary tools, you can begin with step one of this project.


Step 1: Build a Docker image and push it to AWS ECR

We'll package a JavaScript word counter application into a Docker image for this step. After that, we'll push this Docker image to a private AWS ECR repository.


To build the Docker image, follow these steps:


  • Clone the word counter application:

    $ git clone https://github.com/Aahil13/Word-counter.git
    


  • Navigate to the word counter application directory:

    $ cd Word-counter
    


    You'll find all the files and folders for this application in this directory. Part of these files is a Dockerfile containing instructions for building the image.


  • From the Dockerfile, build the Docker image:

    $ docker build -t word-counter .
    


  • Verify that the Docker image was built:

    $ docker images --filter reference=word-counter
    


  • You should see the following output:

    REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
    word-counter   latest    c3d724e0a19e   12 seconds ago   60MB
    


  • You can view the application locally using the following command:

    $ docker run -p 80:80 word-counter
    


Navigate to your browser and point to the URL: http://localhost:80.


On your browser, you see the word counter application:


Figure 2: Word counter application


  • To stop the application, press CTRL+C.


With this, we have built the Docker image. Now, you'll push this Docker image to a private AWS ECR repository.


To push the Docker image to AWS ECR, follow these steps:


  • Using the AWS CLI, log in to AWS ECR:

    $ aws ecr get-login-password --region <REGION> | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com
    


  • Replace <AWS_ACCOUNT_ID> with your AWS account ID and <REGION> with the AWS region you want to use.


  • Create the private AWS ECR repository:

    aws ecr create-repository \
      --repository-name word-counter \
      --image-scanning-configuration scanOnPush=true \
      --region region
    


  • Tag the Docker image:

    $ docker tag word-counter:latest <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/word-counter
    


  • Push the Docker image to AWS ECR:

    $ docker push <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/word-counter
    


    You should see the following output:

    Using default tag: latest
    The push refers to repository [<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/word-counter]
    0342869436ee: Pushed
    c1f569e0c435: Pushed
    29c9ccae1ae2: Pushed
    53bd9605a443: Pushed
    c140f535da0a: Pushed
    aba51e9dc8b1: Pushed
    cc2447e1835a: Pushed
    


With this, you have pushed the Docker image to AWS ECR. Now, we'll move on to step two of this project.


Step 2: Authenticate AWS ECR with Aptible

In this step, we'll authenticate the AWS ECR repository with Aptible. This authentication will give Aptible access to pull the Docker image from the AWS ECR repository.


To authenticate the AWS ECR repository with Aptible, follow these steps:


  • Login to Aptible from the CLI:

    $ aptible login
    


    You should see the following output:

    Email: <EMAIL>
    Password:
    
    Logged in as <EMAIL>
    


  • Authenticate the AWS ECR repository with Aptible.


Note: In this step, we'll run the same command in Step 1 to log in to AWS ECR. However, the ECR Docker login password will be stored in a variable. This variable will then authenticate the AWS ECR repository with Aptible.


$ ECR_PASSWORD=$(aws ecr get-login-password --region <REGION>)


Replace <REGION> with the AWS region you want to use. This command will store the ECR Docker login password in the ECR_PASSWORD variable.


$ echo -n "$ECR_PASSWORD" | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com


Replace <AWS_ACCOUNT_ID> with your AWS account ID and <REGION> with the AWS region you want to use. This command will authenticate the AWS ECR repository with Aptible.


Note: We had to re-authenticate the AWS ECR repository with Aptible because the ECR Docker login password is necessary to deploy an application to Aptible. In Step 1, the password was passed using the --password-stdin flag. This flag ensures that the ECR Docker login password is not stored in the shell history. However, this flag is not supported by Aptible. Therefore, we had to re-authenticate the AWS ECR repository with Aptible, saving the ECR Docker login password in a variable.


With this, we have authenticated the AWS ECR repository with Aptible. Now, we'll move on to step three of this project.


Step 3: Deploy to Aptible

This project's final step is deploying the Docker image to Aptible. To do this, we'll be using a single Aptible CLI command.


Before executing the command, navigate to the Aptible Dashboard and create an app. You can do this by following these steps:


  • Navigate to the Aptible Dashboard.


  • In the Environments section, select the environment you want to use.

    Figure 3: Environments section


  • Navigate to the Apps tab and click on the New App button.

    Figure 4: New app button


  • Enter the app name and click on the Create App button.

    Figure 5: Create app button


After creating the app, you can deploy the Docker image to Aptible.


To deploy the Docker image to Aptible, execute the command below:

$ aptible deploy --app "<APP_NAME>" --docker-image "<AWS_ACCOUNT_ID>.dkr.ecr.<REGION>.amazonaws.com/word-counter" --private-registry-username "AWS" --private-registry-password "$ECR_PASSWORD"


Replace <AWS_ACCOUNT_ID> with your AWS account ID, <REGION> with the AWS region you want to use, and <APP_NAME> with the name of the app you created in the Aptible Dashboard.


You should see the following output:


Figure 6: Deploy command output

Hurrah! You have successfully deployed the Docker image to Aptible. You can now view the application on Aptible.


Navigate to the Aptible Dashboard and select the app you created. You should see the following output:


Figure 7: Aptible dashboard output


Although we have finished the deployment, we can't access the application yet. To access the application on your browser, you must create an endpoint.


To create an endpoint, follow these steps:

  • On the Apps section, select the word-count app and switch to the Endpoints tab below.


  • Click on the New Endpoint button.

    Figure 8: New endpoint button


  • Select the configurations as shown below:

    Figure 9: Endpoint configurations


  • Click on the Save Endpoint button.


It will take a few minutes for the endpoint to be provisioned. Once the endpoint is created, you can access the application using the endpoint URL.


Figure 10: Endpoint output


From the output, you can see that Aptible provisions a secure endpoint for the application. This endpoint is secured using a TLS certificate from Let's Encrypt. Aptible also provisions a load balancer for the application. This load balancer ensures that the application is highly available.


Note: At the time of this writing, Aptible supports the creation of only one endpoint for a free tier account. If you want to create more endpoints, you must upgrade your account.


Conclusion

As displayed throughout this project, Aptible is an excellent platform for deploying applications. Aptible provided all the infrastructure tools needed for deploying the word counter application. Eliminating the need to set up and manage infrastructure.


This hands-on project taught you how to deploy a Docker image from AWS ECR to Aptible. You can now deploy any application from AWS ECR to Aptible using the steps outlined in this project.


To learn more about Aptible’s services, you can check out the Aptible documentation.


More resources: