Day 19: Mastering Docker Commands and Concepts

Adrian Rubico

|

Dec 31, 2024

09:25 PM GMT+8

This blog post aims to introduce Docker's most fundamental commands and concepts. DockerHub, Docker images, registries, and Docker container lifecycle will all be covered. Upon completion, readers will be equipped to effectively utilize Docker and perform practical tasks such as running an Nginx container.

What is DockerHub?

DockerHub is a cloud-based registry where Docker images are stored, shared, and distributed. It serves as a centralized platform for developers to find and use images for common applications, as well as to publish custom images.

Docker Images

A Docker Image is a lightweight, immutable file that contains all the dependencies, libraries, and configurations needed to run an application. Think of it as a stopped container, similar to a VM image.

FeatureDescription
ImmutableImages cannot be changed once built; modifications require creating a new image layer
Layered ArchitectureImages consist of multiple layers, where each layer represents a change or addition
ReusableThe same image can be used to run multiple containers
PortableDocker images can run on any system with Docker installed
Efficient StorageOnly changes are saved as new layers, reducing duplication

Docker Registries

A Docker Registry is a repository for storing and managing Docker images. Registries can be cloud-based or self-hosted.

Cloud-Based Registries:

In-House or Local Registries:

Task: Docker Nginx

In this task, we’ll run an Nginx container using Docker.

  1. Pull the Nginx image:
docker pull nginx

  1. Check the downloaded image:
docker images

  1. Pull the Nginx image with specific version
docker pull nginx:mainline-alpine-perl

  1. Run the Nginx container
docker run -d -p 8080:80 --name my-nginx nginx

This command:

  • Runs the container in detached mode (-d).
  • Maps port 8080 on your host to port 80 in the container (-p).
  • Names the container my-nginx.
  1. List running containers
docker ps

  1. Stop and remove the container
docker stop <container-id or container-name>
docker rm <container-id or container-name>

Docker Commands

The following table lists essential Docker commands and their purposes

CommandPurpose
docker imagesLists all downloaded Docker images on your system.
docker runCreates and starts a container from an image.
docker psLists all currently running containers.
docker ps -aLists all containers, including stopped ones.
docker execExecutes a command inside a running container.
docker start / stop / restartStarts, stops, or restarts a container.
docker rmRemoves a stopped container.
docker rmiRemoves an image from your system.
docker inspectShows detailed information about a container or image.

Docker Container Lifecycle

Docker containers go through a series of stages during their lifecycle. Understanding these stages, along with the actions and corresponding commands, is key to effectively managing containers.

Container Creation

A container is created from a Docker image but not yet running.

To create a container, use docker create command:

docker create --name <container-name> <image-name>

Container Running

A container is actively running and executing its assigned tasks.

Two Ways to Run a Container:

  1. docker run
    1. The docker run command both creates and starts a container in a single step.
    2. If the container does not already exist, Docker will create one from the specified image, start it, and optionally execute a specified command.
    3. This command is ideal for initializing and running a new container.
docker run --name <container-name> <image-name>

  1. docker start
    1. The docker start command starts an existing container that was previously stopped or created using docker create.
    2. It does not create a new container; it resumes the operation of an existing one.
    3. This is useful for reusing containers in recurring workflows.
docker start <container-name>

Container Paused

A running container is temporarily paused, halting all processes without stopping it entirely.

To pause a container, use docker pause command:

docker pause <container-name>

To unpause a container, use docker unpause command:

docker unpause <container-name>

Container Stopped

The container has been stopped, meaning all processes have terminated, but the container still exists.

To stop a container, use docker pause command:

docker stop <container-name>

Container Deleted

The container has been removed from the system, including its metadata and file system changes. This frees up Docker's storage and container resources. In order to stop the container, it must be stopped.

To remove a container, use docker rm command:

docker rm <container-name>

Wrapping Up

Docker commands and concepts form the backbone of container management. Achieving mastery of the lifecycle, registries, and essential commands is a major accomplishment.

In the next blog, we’ll explore the Docker logs command, diving into how to monitor and troubleshoot container activities effectively.

Happy New Year, fellow DevOps engineers! 🎆🎉❤️

Discussion