πDay 20: Docker Installation and Essential Commands
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! πβ¨
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.