Docker Installation On AWS and Essential Commands

Deepesh GuptaDeepesh Gupta
5 min read

Docker makes it easy to develop and run applications in isolated environments. In this blog, we'll show you how to install Docker on an Ubuntu EC2 instance in AWS. By the end, you'll have Docker up and running on your EC2 instance, ready to use.

1. Setting Up an AWS EC2 Instance 🔗

Step 1: Launching an EC2 Instance

  • Log in to your AWS Management Console.

  • Go to the EC2 Dashboard and click "Launch Instance."

  • Pick an Amazon Machine Image (AMI) – Ubuntu Server 22.04 LTS is a great option.

  • Select an Instance Type – choose t2.micro, which is free tier eligible.

  • Set up your instance details, add storage, and include tags if you like.

  • Configure Security Group: Allow SSH (port 22) to access your instance.

  • Launch the instance and download the key pair (.pem file) for SSH access.

Step 2: Connect to Your EC2 Instance

  • Open your terminal and navigate to the directory where your .pem key file is saved.

  • Run the following command to connect to your EC2 instance:

ssh -i "your-key-file.pem" ubuntu@your-ec2-public-ip

Replace your-key-file.pem with the name of your key file and your-ec2-public-ip with the public IP address of your instance.

Once connected, you are ready to install Docker.

2. Installing Docker on Ubuntu 🐋

Step 1: Update Your Ubuntu Package Repository

Before installing Docker, it’s essential to refresh the package index to ensure you’re working with the latest available versions. Execute the following commands:

sudo apt-get update && sudo apt-get upgrade -y

Step 2: Install Docker

To install Docker, simply execute the following command in your terminal:

sudo apt-get install docker.io

Step 3: Start and Enable Docker Service

To ensure Docker starts on boot, enable the Docker service and start it:

sudo systemctl start docker
sudo systemctl enable docker

3. Docker Commands 🖥️

Now that Docker is installed, let's explore some essential Docker commands that you’ll use regularly:

3.1 docker ps

The docker ps command displays all running containers. If you encounter a "permission denied" error when using this command, you can resolve it by running the command shown in the screenshot below.

docker ps

If you have not idea about the user the use command

whoami

In my case, since the user is ubuntu, you can directly use ubuntu in your command and run it. Alternatively, you can use $USER to automatically adjust the ownership permissions.

chown $USER /var/run/docker.sock

To list both running and stopped containers, use:

docker ps -a

3.2 docker images

The docker images command lists all the images downloaded on your system.

docker images

3.3 docker stop

The docker stop command stops a running container. You need to specify the container ID or name.

docker stop <container_id>

Example:

docker stop 2a68bbc66528

3.4 docker rm

The docker rm command removes a stopped container.

docker rm <container_id>

Example:

docker rm 2a68bbc66528

3.5 docker rmi

The docker rmi command removes a Docker image from your system.

docker rmi <image_id>

Example:

docker rmi 7e8b9c0d1e2f

3.6 docker inspect

The docker inspect command is used to view detailed information about a Docker container or image. It provides a JSON output that includes configuration settings, network settings, volumes, mounts, and more.

sudo docker inspect <container_id_or_image_id>

Example:

If you want to inspect a running container with ID 1a2b3c4d5e6f, you would run:

sudo docker inspect 1a2b3c4d5e6f

3.7 docker exec

The docker exec command allows you to run commands inside a running container.

docker exec -it <container_id> /bin/bash

This opens a bash shell inside the container, allowing you to interact with it.

3.8 docker top

The docker top command shows the processes running inside a Docker container. It functions similarly to the top command in Linux but is specifically designed to display processes within a container.

sudo docker top <container_id>

Example:

If you want to see the running processes in a container with ID 1a2b3c4d5e6f, you would run:

sudo docker top 1a2b3c4d5e6f

3.9 docker save

The docker save command is used to save one or more Docker images to a tar archive file. This is useful for backing up images or transferring them between systems.

sudo docker save -o <path_to_tar_file> <image_name_or_id>

Example:

If you want to save an image called my_app:latest to a tar file called my_app_backup.tar, you would run:

sudo docker save -o my_app_backup.tar my_app:latest

3.10 docker load

The docker load command is used to load Docker images from a tar archive file. It’s commonly used to restore images that were previously saved using docker save.

sudo docker load -i <path_to_tar_file>

Example:

If you want to load a Docker image from a tar file called my_app_backup.tar, you would run:

sudo docker load -i my_app_backup.tar

Conclusion 📝

As I dive deeper into AWS and DevOps, I’m excited to share what I’ve learned with you. In this expanded guide, we covered the installation of Docker and explored key commands used daily. These commands are vital for inspecting containers and images, managing processes within containers, and saving or transferring Docker images for backups.

Ready to explore Docker’s features further? 🐋✨

I’d love to hear your feedback or insights. Connect with me on LinkedIn at Deepesh Gupta if you’re on a similar journey or want to collaborate. Stay tuned for more updates on my DevOps adventures!


0
Subscribe to my newsletter

Read articles from Deepesh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Deepesh Gupta
Deepesh Gupta

DevOps & Cloud Enthusiast | Open Source Contributor | Driving Technological Excellence in DevOps and Cloud Solutions