Skip to content

Guide: How to Compile and Run a Local Docker Image on an Amazon EC2 Instance

Published: at 12:27 PM

Guide: How to Compile and Run a Local Docker Image on an Amazon EC2 Instance

Part 1: Setting Up Your Local Environment

  1. SQLite Database Integration:

    • Ensure that your application is set up to use SQLite as its database.
    • Test your application locally to confirm that it runs without any issues.
    • Resource:

    Add SQLite DB to ASP.NET Core using EF Core code-first - Mobiletonster’s Blog


Part 2: Setting Up an AWS EC2 Instance

  1. Creating an EC2 Instance:
    • Log in to your AWS Management Console.
    • Navigate to the EC2 dashboard and click on “Launch Instance.”
    • Follow the prompts to create a new EC2 instance.
  2. Installing Docker on EC2:
    • Once your EC2 instance is up and running, SSH into it.
    • Follow the official Docker documentation to install Docker on your EC2 instance.

Creating a container image for use on Amazon ECS - Amazon Elastic Container Service


Part 3: Compiling a Docker Image Locally

  1. Building the Image:
    • Navigate to the directory containing your Dockerfile.
    • Run the command docker build -t your-image-name . to compile your application into a Docker image.
  2. Testing the Image Locally:
    • Run the command docker run -p local_port:Container_port your-image-name to start a container from your image.
    • Ensure that your application is accessible and functioning correctly.

Part 4: Pushing the Docker Image to Docker Hub

  1. Logging in to Docker Hub:
    • Run the command docker login and provide your Docker Hub credentials.
  2. Pushing the Image:
    • Tag your image for Docker Hub using docker tag your-image-name:latest your-dockerhub-username/your-image-name:latest.
    • Push the image using docker push your-dockerhub-username/your-image-name:latest.

Part 5: Running the Docker Image on EC2

  1. Pulling the Image on EC2:
    • SSH into your EC2 instance.
    • Run the command docker pull your-dockerhub-username/your-image-name:latest to download your Docker image.
  2. Running the Image on EC2:
    • Start a container from your image using docker run -p 80:Container_port your-dockerhub-username/your-image-name:latest.(Using port 80 helps you avoid modifying the firewall of your EC2 instance)
    • Ensure that your application is accessible and functioning correctly on the EC2 instance.