cover-img

Deploying Docker Containers on Amazon EC2: A Step-by-Step Guide

13 July, 2023

2

2

0

Amazon Elastic Compute Cloud (EC2) provides a flexible and scalable infrastructure to deploy and manage your applications. By combining the power of EC2 with Docker, you can easily containerize your applications and leverage the benefits of portability, scalability, and easy deployment. In this blog post, we will walk you through the process of deploying Docker containers on Amazon EC2.

Prerequisites:


Before we begin, make sure you have the following:

  1. An AWS account: If you don't have one, sign up for an AWS account at https://aws.amazon.com/.
  2. Basic knowledge of Docker: Familiarize yourself with Docker and containerization concepts.

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console and navigate to the EC2 service.
  2. Click on "Launch Instances" to start the instance creation process.
  3. Select the desired Amazon Machine Image (AMI) based on your application requirements.
  4. Choose the instance type, configure networking and security groups, and review the settings.
  5. Click "Launch" to create the EC2 instance.

Step 2: Connect to the EC2 Instance

  1. Once the instance is running, select it from the EC2 dashboard and click "Connect."
  2. Follow the instructions to establish a secure shell (SSH) connection to the instance using the provided key pair.

Step 3: Install Docker on the EC2 Instance

  1. Connect to the EC2 instance via SSH.
  2. Update the instance by running the command: sudo yum update -y.
  3. Install Docker by running the following commands:
sqlCopy code
sudo amazon-linux-extras install docker
sudo service docker start
sudo usermod -a -G docker ec2-user
  1. Verify the Docker installation by running: docker --version.

Step 4: Build and Push your Docker Image (Optional)

  1. If you haven't already built your Docker image, navigate to your application's directory.
  2. Create a Dockerfile to define the image's specifications, dependencies, and commands.
  3. Build the Docker image using the command: docker build -t your-image-name ..
  4. Tag the image with the appropriate repository information: docker tag your-image-name repository-url/your-image-name.
  5. Push the image to the repository using the command: docker push repository-url/your-image-name.

Step 5: Run a Docker Container on the EC2 Instance

  1. Connect to the EC2 instance via SSH.
  2. Run the Docker container using the command: docker run -d -p host-port:container-port your-image-name.
    Replace host-port with the desired port on the EC2 instance and container-port with the port exposed by your application inside the Docker container.
  3. Verify the container is running by running: docker ps.

Step 6: Access your Application

  1. Determine the public IP address or DNS name of your EC2 instance from the EC2 dashboard.
  2. Open a web browser and navigate to http://public-ip-or-dns:host-port to access your application running inside the Docker container.

Conclusion


Deploying Docker containers on Amazon EC2 allows you to take advantage of both containerization and the scalable infrastructure provided by AWS. By following the steps outlined in this guide, you can easily set up an EC2 instance, install Docker, build and push your Docker image, and run your containerized application. Embrace the flexibility and portability of Docker containers while harnessing the power of Amazon EC2 for your application deployment needs.

2

2

0

Karm Patel
Open Source Enthusiast | DevOps | Learning in Public

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.