Day 19: Mastering Docker Commands and Concepts
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.
Feature | Description |
Immutable | Images cannot be changed once built; modifications require creating a new image layer |
Layered Architecture | Images consist of multiple layers, where each layer represents a change or addition |
Reusable | The same image can be used to run multiple containers |
Portable | Docker images can run on any system with Docker installed |
Efficient Storage | Only 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:
- Docker Hub
- Google Artifact Registry (GAR)
- Amazon Elastic Container Registry (ECR)
- Azure Container Registry (ACR)
In-House or Local Registries:
- Harbor
- Jfrog Artifactory
- DTR (Docker trusted Registry)
Task: Docker Nginx
In this task, we’ll run an Nginx container using Docker.
- Pull the Nginx image:
docker pull nginx
- Check the downloaded image:
docker images
- Pull the Nginx image with specific version
docker pull nginx:mainline-alpine-perl
- 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
.
- List running containers
docker ps
- 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
Command | Purpose |
docker images | Lists all downloaded Docker images on your system. |
docker run | Creates and starts a container from an image. |
docker ps | Lists all currently running containers. |
docker ps -a | Lists all containers, including stopped ones. |
docker exec | Executes a command inside a running container. |
docker start / stop / restart | Starts, stops, or restarts a container. |
docker rm | Removes a stopped container. |
docker rmi | Removes an image from your system. |
docker inspect | Shows 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:
docker run
- The
docker run
command both creates and starts a container in a single step. - If the container does not already exist, Docker will create one from the specified image, start it, and optionally execute a specified command.
- This command is ideal for initializing and running a new container.
- The
docker run --name <container-name> <image-name>
docker start
- The
docker start
command starts an existing container that was previously stopped or created usingdocker create
. - It does not create a new container; it resumes the operation of an existing one.
- This is useful for reusing containers in recurring workflows.
- The
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! 🎆🎉❤️
Day 19: Mastering Docker Commands and Concepts
Master Docker with commands, lifecycle stages, and registries to streamline container management and boost DevOps efficiency.
For the passion of automated cloud solutions.
Subscribe to get the latest posts. I mostly write about Backend (Python/Bash), DevOps and Linux.