Beginner’s Docker Tutorial: How to Deploy on AWS EC2

Beginner’s Docker Tutorial: How to Deploy on AWS EC2

What is Docker?

Docker is a powerful software platform that enables developers to package applications into standardized units called containers. A container includes everything an application needs to run—code, runtime, libraries, and system tools—ensuring that your application runs seamlessly in any environment. With Docker, you can:

  • Quickly Deploy and Scale: Run applications efficiently across multiple platforms.

  • Ensure Reliability: Guarantee that your code works identically in development, testing, and production environments.


Here's a quick command to get started:

bashCopy codedocker run hello-world

This command starts a new container and lets you interact with it through the command line, showcasing the simplicity of Docker.


Installing Docker on an AWS EC2 Instance

Installing Docker on an AWS EC2 instance is straightforward. Follow these steps to get started:

Step 1: Create an EC2 Instance

  • Launch an EC2 instance using AWS Management Console.

  • Choose an Ubuntu-based AMI for simplicity.

Step 2: Connect to the EC2 Instance

  • Use SSH to connect to your EC2 instance:
bashCopy codessh -i your-key.pem ubuntu@your-ec2-instance-ip

Step 3: Install Docker

Run the following commands to install Docker:

bashCopy codesudo apt-get update
sudo apt install docker.io -y

Step 4: Grant Docker Daemon Permissions

Add your user to the Docker group to enable non-root access:

bashCopy codesudo usermod -aG docker ubuntu

Step 5: Restart the Instance

Reboot the instance to apply permission changes:

bashCopy codesudo reboot

Step 6: Verify Docker Installation

After rebooting, check if Docker is installed and running:

bashCopy codedocker run hello-world


Key Docker Commands for Managing Containers and Images

Mastering Docker involves understanding its versatile command-line tools. Here are some essential commands to help you manage containers and images:

1. Inspect Containers and Images

Use the docker inspect command to view detailed information about a container or image:

docker inspect container_name_or_id

2. List Port Mappings

To check the port mappings for a container, use:

docker port container_name_or_id

For example:

docker run -p 8181:82 container_name
docker port container_name

3. Monitor Resource Usage

Track real-time resource usage of running containers:

docker stats

4. View Running Processes

To see the processes inside a specific container:

docker top my_container2

5. Save and Load Images

  • Save an image to a tar archive:
docker save -o my_image.tar nginx

  • Load an image from a tar archive:
docker load -i my_image.tar


Why Learn Docker?

Docker has become a cornerstone of modern application development and deployment. Its portability, scalability, and ease of use make it a favorite for developers and DevOps professionals alike. Whether you're deploying applications to the cloud or optimizing local development workflows, Docker empowers you to deliver reliable and efficient solutions.


Conclusion

Docker is more than just a tool—it's a gateway to modernizing application development and deployment. By installing Docker on an AWS EC2 instance and mastering essential commands, you take the first steps toward becoming proficient in containerization and cloud-based deployments. Start your Docker journey today, and unlock a world of possibilities in software engineering!

Ready to dive deeper? Share your experience with Docker or ask questions in the comments below. Let’s build a vibrant community of learners and innovators!