Docker for DevOps Engineers

Docker for DevOps Engineers

Docker

Docker is an open-source platform that enables developers to automate the deployment and management of applications within software containers. It provides an efficient way to package an application and all its dependencies into a standardized unit called a container. These containers are lightweight and isolated, allowing the application to run consistently across different environments.

Docker Container

Imagine you have a special kind of container, like a shipping container, but for software. You can put everything your software needs to run inside this container, such as the code, libraries, and dependencies. This container is called a Docker container.

Now, let's say you want to run your software on different computers. Each computer might have a different operating system or configuration. With Docker, you don't need to worry about these differences. You can simply take your Docker container and run it on any computer that has Docker installed.

Docker makes it easy to package and distribute software. It's like a standardized, self-contained package that includes everything needed for the software to run. This makes it easier to deploy and manage applications, especially in complex environments with multiple software components.

Docker also allows you to run multiple containers on the same computer without conflicts. Each container is isolated from the others, like separate compartments. This isolation ensures that one container's software doesn't interfere with another container's software.

Docker Image

A Docker image is a lightweight, standalone, and executable software package that contains all the necessary files, libraries, dependencies, and configurations required to run an application. It serves as a blueprint for creating Docker containers.

Docker images are built based on instructions specified in a text file called a Dockerfile. The Dockerfile defines the base image, adds any necessary files or dependencies, sets up environment variables, and specifies the commands to run when the container is created.

Images are organized in layers, where each layer represents a specific instruction in the Dockerfile. Docker uses a layered file system and a copy-on-write mechanism, allowing images to share common layers and minimizing storage requirements.

Docker Installation

To install Docker on an Ubuntu instance, you can follow these steps

Step 1: Update the package database First, update the package database on your Ubuntu instance to ensure you have the latest versions of software packages. Open a terminal and run the command:

sudo apt-get update

Step 2: Install Docker Next, install Docker on your Ubuntu instance using the below command. This will fetch the necessary files and set up Docker on your system.

sudo apt-get install docker.io

Important: Granting user access to Docker By default, Docker creates a group named "docker," and to allow your user to access and use Docker, you'll need to add your user to that group. Follow these steps to add your user to the "docker" group:

  1. Run the below command to add your user to the "docker" group.

     sudo usermod -aG docker $USER
    
  2. Reboot your system for the changes to take effect. Simply run

     sudo reboot
    

That's it! You've successfully installed Docker on your Ubuntu instance. You can now leverage the power of Docker to build and manage containers on your system.

Tasks

Task 1:Use the docker run command to start a new container and interact with it through the command line

docker run hello-world

It fetches a lightweight "hello-world" image, creates a container from it, and displays a message.

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

The docker inspect command provides a comprehensive view of the specified container or image, allowing you to gather detailed information about its configuration and other relevant details.

docker inspect container/image_id

Task 3: Use the docker port command to list the port mappings for a container.

  1. Identify the name or ID of the container for which you want to list the port mappings. You can use the docker ps command to list the running containers and their names or IDs.

  2. Run the following command, replacing CONTAINER_ID with the actual name or ID of the container you want to inspect:

     docker port CONTAINER_ID
    

  3. Docker will output a list of the container's port mappings, showing the mapping between the container's internal ports and the corresponding ports on the host system.

  4. The output will typically include two columns: the container's internal port number and the corresponding host system's port number to which it is mapped.

Task 4: Use the docker stats command to view resource usage statistics for one or more containers

The docker stats will show a live stream of data with columns such as the container's name or ID, CPU percentage, memory usage, network transmit/receive, and block I/O.

docker stats

Task 5: Use the docker top command to view the processes running inside a container

The output will show columns such as PID (Process ID), TTY (Terminal), TIME (CPU time used), and CMD (Command).

This information allows you to get a glimpse of the processes running inside the container and their resource usage.

docker top CONTAINER_ID

Task 6: Use the docker save command to save an image to a tar archive

Docker will save the specified image to a tar archive file. The -o flag is used to specify the output file name.

After the command completes, you will find the tar archive file (output_file.tar or mysql.tar in the examples) in the current directory.

docker save -o output_file.tar IMAGE_NAME/IMAGE_ID

The docker save command allows you to export Docker images to a tar archive file. This can be useful for backup purposes, transferring images to another system, or sharing images with others.

Task 7: Use the docker load command to load an image from a tar archive

Docker will start loading the image from the tar archive file. The loading process may take some time depending on the size of the image.

Once the image is successfully loaded, you will see an output indicating the image ID and other details.

docker load -i archive_file.tar

After loading the image, you can use it to run containers or perform other Docker operations as needed.

The docker load command enables you to import Docker images from tar archive files, allowing you to restore images that were previously saved or shared.

Thank you for reading.