Docker for DevOps Engineers (Day14)

Araiz bin Saqib
5 min readJul 7, 2024

--

Welcome to Day-14 of your DevOps journey! Continuing our DevOps series, today we will be diving into Docker for DevOps Engineers.

In our previous discussion, we covered Python Libraries for DevOps. If you haven’t read it yet, you can check it out [here].

What is Docker?

Docker is a software platform designed to simplify the process of building, testing, and deploying applications. It achieves this by packaging software into standardized units called containers. These containers encapsulate all the necessary components, such as libraries, system tools, code, and runtime, ensuring that the software runs consistently across different environments. Docker enables developers to deploy and scale applications rapidly, knowing that their code will function reliably regardless of the underlying infrastructure.

Why do we use Docker?

Docker offers several advantages, including:

  • Faster software shipping: By using containers, developers can build, test, and release software more quickly and reliably.
  • Standardizing operations: Containers provide a consistent environment for development, testing, and production, reducing discrepancies and configuration issues.
  • Seamless migrations: Docker containers can be easily moved across different environments, facilitating smooth transitions and migrations.
  • Cost-effective: Docker helps optimize resource usage and reduces the overhead associated with managing virtual machines.

When do we use Docker?

Docker is beneficial in various scenarios, including:

  • Handling microservices: Docker allows the deployment of microservices in isolated containers, simplifying the management and scaling of individual services.
  • Continuous Integration and Delivery (CI/CD): Docker streamlines the CI/CD pipeline by providing consistent environments for building, testing, and deploying applications.
  • Data processing: Docker containers can be used to package and execute data processing tasks, ensuring consistency and repeatability.
  • Container as a Service (CaaS): Docker enables the use of containers as a service, providing a flexible and scalable solution for deploying and managing applications.

We will install it in the AWS EC2 machine.

First, we will start an EC2 machine. Head over to your EC2 Instance Dashboard and click on ‘Launch Instance.’ Give it a meaningful name, then select Ubuntu as the operating system and the free-tier eligible service.

After selecting the storage type that is eligible for the free tier and your key pair (you can create one as well) to connect via SSH

After configuring the network security settings under the Security Group section, you can create a new security group or select from an existing one. Then, in the User Data field under the Advanced tab, write the following commands.

#!/bin/bash
# Update package list
sudo apt update
# Install OpenSSH server package
sudo apt install -y openssh-server
# Start SSH service
sudo systemctl start ssh
# Enable SSH service to start on boot
sudo systemctl enable ssh

This will install an SSH server when launching the instance. After that, press Launch Instance. Congratulations, you have launched an EC2 instance.

After some time, the instance will be launched. To connect to it using SSH, open the command prompt in the location where you have stored your key pair.

Once the instance is in the running state, you can connect to it. You can use SSH to connect or connect directly through the browser. It takes some time to install and configure the SSH server.

Copy the SSH command from the Connect section and paste it into the command prompt in the directory where the key pair is stored.

As now I am in my AWS Ubuntu Matchine. I will run the following commands to install Docker.

#Updating the already present packages
sudo apt update
#Installing docker
sudo apt install docker.io
#Checking status of Docker
sudo systemctl status docker
#If docker does not start automatically
sudo systemctl start docker

This is how we can install Docker.

Now we will see some of the important commands in docker.

I will simply run the following command to run docker.

Now we have verified the docker installation as well. The hello-world in docker is always stored as a docker image.

To list all the docker images

docker images

Now to list the contents of a docker image we use the following command

docker inspect [image-name]
#ordocker inspect [image-id]

Both these commands list all the contents of the docker image

To list the port mappings for a container, we use the following command

docker port [container-id/container-name]

Now, to check the stats and determine how many resources any of the running containers are using, we use the following command:

docker stats

This lists the container and the usage.

To display the running processes inside the container, use the following Docker command:

docker top

If we want to compress a specific image into a .tar format, we use the following docker command:

docker save -o filename.tar image_name:version
#we can give multiple images as well
docker save -o filename.tar image_name:version image2_name:version
#or we can use the following command
docker save image_name:version > filename.tar

This created a zip of Nginx’s latest image

Now to extract an image from a zip file we use the following command

docker load -i filename.tar

This loads the zipped image

So these were some basic docker commands everyone must know.

This was all from today, we will see how to make containers and run them later on.

Thank you for giving it a read.

--

--