Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Top tips to manage docker containers from command line
Docker has become the standard for containerization, with organizations rapidly adopting container-based architectures. Managing multiple containers and images through the command line interface (CLI) can seem overwhelming, but with the right set of commands and techniques, it becomes straightforward and efficient.
This article covers essential tips and commands for managing Docker containers and images from the command line, helping you streamline your container operations and save valuable time.
Listing Images and Containers
Keeping track of your Docker resources is crucial for effective management. Use these commands to view your current images and containers.
List All Images
sudo docker images
This command displays all images with their repository names, tags, image IDs, creation dates, and sizes.
List Running Containers
sudo docker ps
Shows currently running containers with their IDs, names, images, creation time, status, and port mappings.
List All Containers
sudo docker ps -a
Displays all containers, including stopped ones, providing complete visibility into your container ecosystem.
Using Aliases to Save Time
Docker commands can be lengthy and repetitive. Create custom aliases to streamline your workflow by adding them to your ~/.bashrc file.
alias dps='docker ps' alias dpsa='docker ps -a' alias di='docker images' alias drm='docker rm' alias dri='docker rmi' alias dl='docker ps -l -q'
After adding these aliases, reload your shell with source ~/.bashrc to start using them immediately.
Container Management
Stopping and Removing Containers
Before removing a running container, you must stop it first:
sudo docker stop CONTAINER_ID sudo docker rm CONTAINER_ID
To forcefully stop and remove a container in one step:
sudo docker rm -f CONTAINER_ID
Running Interactive Containers
To run a container with an interactive shell, use the -it flags:
sudo docker run -it ubuntu bash
This creates an interactive terminal session inside the Ubuntu container.
Image Management
Removing Images
To remove an image, first ensure no containers are using it:
sudo docker rmi IMAGE_ID
Cleaning Up Dangling Images
Remove unused images that are taking up space:
sudo docker image prune
Volume Management
Dangling volumes consume disk space unnecessarily. List them with:
sudo docker volume ls -f dangling=true
To prevent dangling volumes, always use the -v flag when removing containers:
sudo docker rm -v CONTAINER_ID
Essential Commands Reference
| Operation | Command | Description |
|---|---|---|
| List containers | docker container ls |
Show running containers |
| Kill container | docker container kill CONTAINER_ID |
Forcefully stop container |
| Inspect container | docker inspect CONTAINER_ID |
Get detailed container information |
| List images | docker image ls |
Show all local images |
| Clean images | docker image prune |
Remove dangling images |
Conclusion
Effective Docker container management from the command line requires familiarity with core commands for listing, stopping, removing, and inspecting containers and images. Using aliases and cleanup commands helps maintain an organized and efficient containerized environment, saving time and system resources.
