Guide: How to Compile and Run a Local Docker Image on an Amazon EC2 Instance
Part 1: Setting Up Your Local Environment
-
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
- 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.
- 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.
- Resource:
Creating a container image for use on Amazon ECS - Amazon Elastic Container Service
Part 3: Compiling a Docker Image Locally
- 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.
- 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.
- Run the command
Part 4: Pushing the Docker Image to Docker Hub
- Logging in to Docker Hub:
- Run the command
docker login
and provide your Docker Hub credentials.
- Run the command
- 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
.
- Tag your image for Docker Hub using
Part 5: Running the Docker Image on EC2
- 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.
- 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.
- Start a container from your image using