Essential Docker Commands for Efficient Application Management
Docker has emerged as a formidable platform that enables developers to build, deploy, and manage applications using containerization technology. Utilizing Docker’s command-line interface is fundamental for executing tasks such as creating images, orchestrating containers, and configuring networks and volumes. A strong grasp of these commands is essential for optimizing your workflow with containerized applications.
This guide comprehensively outlines the core Docker commands you need to know. Each command is accompanied by its syntax, practical examples, and relevant use cases to facilitate understanding and application in real-world scenarios.
| Command | Description | Basic Syntax |
|---|---|---|
| docker ps | Displays currently running containers | docker ps [OPTIONS] |
| docker run | Initiates a new container from a specified image | docker run [OPTIONS] IMAGE |
| docker stop | Halts a running container | docker stop CONTAINER |
| docker start | Resumes a previously halted container | docker start CONTAINER |
| docker restart | Restarts a container to implement changes | docker restart CONTAINER |
| docker exec | Executes a command within a running container | docker exec [OPTIONS] CONTAINER COMMAND |
| docker logs | Retrieves logs from a container | docker logs [OPTIONS] CONTAINER |
| docker build | Creates a custom Docker image from a Dockerfile | docker build [OPTIONS] PATH |
| docker images | Lists local Docker images | docker images |
| Docker RMI | Removes a specified Docker image | docker rmi IMAGE |
| docker pull | Fetches an image from a Docker registry | docker pull IMAGE |
| docker push | Uploads an image to a Docker registry | docker push IMAGE |
| docker stats | Displays real-time resource metrics | docker stats [CONTAINER] |
| docker system prune | Cleans up unused containers, images, and networks | docker system prune [OPTIONS] |
| docker-compose up | Launches multi-container applications as defined in a compose file | docker-compose up [OPTIONS] |
| docker-compose down | Stops and removes multi-container applications | docker-compose down |
How to List Docker Containers
The command docker ps is essential for monitoring active containers. It functions similarly to a task manager, presenting details such as container ID, names, status, and port mappings.
docker ps

For an overview of both running and stopped containers, append the -a option.
docker ps -a

Creating and Starting a New Container
Initiating a new container typically starts with the docker run command. This command launches a container based on a specified image, making it the first step in deploying an application.
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Different options can be used to tailor the startup process. For instance, the -d option runs the container in detached mode, allowing the terminal to remain usable. The -p option maps desired ports between the host and the container.
docker run -d -p 8080:80 nginx

This command launches an NGINX container in the background, making it accessible on port 8080 of your host. Enter http://localhost:8080 in your browser, and you should see the default NGINX welcome page.

Viewing Container Logs
To access the log output of a container, whether it’s running or stopped, the docker logs command is indispensable. It’s an excellent tool for diagnosing application issues, monitoring events, or finding errors within the application.
For instance, to check the logs of a container named openwebui, execute:
docker logs openwebui

Downloading an Image from a Registry
The docker pull command is used when the necessary image is not present locally, allowing you to conveniently retrieve pre-built images instead of crafting them from scratch.
Additionally, specific version tags can be utilized to ensure you obtain the precise image needed. For example, to fetch the latest Ubuntu image, you would run:
docker pull ubuntu:latest

Inspecting a Container or Image
The docker inspect command provides comprehensive details about a container or image, including network configurations, mounted volumes, and environment variables, which are invaluable for debugging issues.
docker inspect openwebui
This command is particularly useful when diagnosing configuration challenges or understanding internal container structures.

Monitoring Resource Usage
The docker stats command serves to monitor real-time information about the CPU, memory, network, and disk utilization across active containers.
docker stats

You can also monitor specific containers by adding their name or ID:
docker stats openwebui
This functionality acts as a live performance dashboard for your Docker environment.

Starting Multi-Container Applications
For projects that require multiple services, such as a backend API, frontend application, and a database, Docker Compose simplifies management. You can start all components in a single command using:
docker compose up -d
The -d flag ensures that all services run in the background, making it straightforward to manage complex applications.

Cleaning Up Unused Docker Resources
As your Docker environment evolves, it may accumulate obsolete resources, such as old images, inactive containers, and unused networks. Use the following command to tidy up:
docker system prune
Confirm the operation by typing y to remove stopped containers, unused networks, dangling images, and other unused data.

If you need a more thorough cleanup, including all unused images (not just dangling ones) and volumes, use:
docker system prune -a --volumes

This comprehensive cleaning process helps maintain optimal storage levels.
Viewing All Available Docker Commands
If you need a quick reference for Docker commands, the following command brings up the help menu:
docker --help

For detailed information about specific commands, such as docker ps, use:
docker ps --help

This overview encapsulates fundamental Docker commands that developers utilize daily to ensure effective application management.
Leave a Reply