πŸš€Day 20: Docker Installation and Essential Commands

Tanmaya AroraTanmaya Arora
5 min read

Docker is a powerful tool for developing and running applications in a containerized environment. In this blog, we will walk through the process of setting up Docker on an Ubuntu EC2 instance hosted on AWS. By the end of this guide, you'll have Docker installed and ready to use on your EC2 instance.

1. Setting Up an AWS EC2 Instance πŸ–₯️

Step 1: Launching an EC2 Instance

  • Log in to your AWS Management Console.

  • Navigate to the EC2 Dashboard and click Launch Instance.

  • Choose an Amazon Machine Image (AMI) – select Ubuntu (Ubuntu Server 22.04 LTS is a good choice).

  • Choose an Instance Type – select t2.micro (eligible for the free tier).

  • Configure instance details, add storage, and add tags if needed.

  • 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 always a good practice to update the package repository. Run the following commands:

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

Step 2: Install Docker Dependencies πŸ“¦

Install the necessary packages that allow you to add new repositories over HTTPS:

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y

Step 3: Add Docker’s GPG Key πŸ”‘

Now, add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Step 4: Add Docker Repository to APT Sources πŸ—‚οΈ

Add the Docker repository to your APT sources:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Install Docker 🐳

Update your package database again to reflect the new Docker repository:

sudo apt-get update

Now, install Docker:

sudo apt-get install docker-ce -y

Step 6: 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

Step 7: Verify Docker Installation βœ…

Check if Docker is installed and running correctly by running the following command:

sudo docker --version

You can also verify Docker’s installation by running a test container:

sudo docker run hello-world

If you see a message saying "Hello from Docker!", congratulations πŸŽ‰β€”Docker is successfully installed on your EC2 instance!

3. Explanation of Key Docker Commands πŸ§‘β€πŸ’»

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

1. docker --version πŸ”

This command shows the currently installed version of Docker.

sudo docker --version

Example output:

Docker version 20.10.17, build 100c701

2. docker run πŸš€

The docker run command is used to create and start a container from a Docker image.

sudo docker run hello-world

This command runs the hello-world image, which prints a message to confirm that Docker is working.

3. docker ps πŸ“

The docker ps command lists all running containers.

sudo docker ps

To list both running and stopped containers, use:

sudo docker ps -a

4. docker images πŸ“‚

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

sudo docker images

5. docker stop β›”

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

sudo docker stop <container_id>

Example:

sudo docker stop 1a2b3c4d5e6f

6. docker rm πŸ—‘οΈ

The docker rm command removes a stopped container.

sudo docker rm <container_id>

Example:

sudo docker rm 1a2b3c4d5e6f

7. docker rmi πŸ–ΌοΈ

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

sudo docker rmi <image_id>

Example:

sudo docker rmi 7e8b9c0d1e2f

8. docker exec πŸ› οΈ

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

sudo docker exec -it <container_id> /bin/bash

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

9. 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

10. docker top πŸ“

The docker top command displays the running processes inside a Docker container. It's similar to the top command in Linux but specifically for viewing the 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

11. 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

12. 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 πŸ“

In this expanded guide, we explored installation of Docker and some major docker commands used in day to day life. These commands are essential for inspecting containers and images, managing processes inside containers, and saving/loading Docker images for backups or transfers.

With these commands in your toolkit, you're ready to dive deeper into Docker’s powerful features! πŸ‹βœ¨


0
Subscribe to my newsletter

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

Written by

Tanmaya Arora
Tanmaya Arora

Hey there! πŸ‘‹ 🌟 Welcome to my DevOps journey! 🌟 I'm Tanmaya Arora, an enthusiastic DevOps Engineer. Currently, on a learning adventure, I'm here to share my journey and Blogs about DevOps. I believe in fostering a culture of resilience, transparency, and shared responsibility. Embracing agility and flexibility, in this adventure let's grow together in this vibrant DevOps space. Join me in transforming software delivery through collaboration, innovation, and excellence! πŸš€πŸ”§πŸ’‘ 🌐 Connect with me for friendly chats, group discussions, shared experiences and learning moments.