Docker Basic Commands
Today, we focused on mastering essential Docker housekeeping commands to manage containers, images, volumes, and perform clean-up tasks effectively.
π§ Concepts Covered
- Listing running and stopped containers
- Removing stopped containers and unused images
- Managing Docker volumes
- Executing commands inside running containers
- Understanding how Docker resources accumulate over time and how to keep your environment clean
π§ Docker Commands Practiced
CONTAINER
π List Containers
docker ps # Lists only running containers
docker ps -a # Lists all containers (including stopped ones)
π§Ή Remove Containers
docker rm <container_id> # Removes a specific container by ID or name
π‘ Tip: Use docker ps -a to find stopped containers you no longer need.
Create & Run a New Container
docker run <image_name>
If the image is not available locally, itβll be downloaded from Docker Hub.
Run Container in Background
docker run -d <image_name>
Run Container with Custom Name
docker run --name <container_name> <image_name>
IMAGES
π¦ List Docker Images
docker images
π§½ Remove Docker Images
docker rmi <image_id> # Removes an image by its ID
π‘ Useful for deleting old or unused images to free up disk space.
Remove Unused Images
docker image prune
Build an Image from a Dockerfile
docker build -t <image_name>:<version> . # Version is optional
docker build -t <image_name>:<version> . --no-cache # Build without cache
Port Binding in Container
docker run -p<host_port>:<container_port> <image_name>
Set Environment Variables in a Container
docker run -e <var_name>=<var_value> <container_name> # Or <container_id>
Start or Stop an Existing Container
docker start|stop <container_name> # Or <container_id>
Inspect a Running Container
docker inspect <container_name> # Or <container_id>
TROUBLESHOOT
Fetch Logs of a Container
docker logs <container_name> # Or <container_id>
Open Shell Inside Running Container
docker exec -it <container_name> /bin/bash
docker exec -it <container_name> sh
DOCKER HUB
Pull an Image from Docker Hub
docker pull <image_name>
Publish an Image to Docker Hub
docker push <username>/<image_name>
Login to Docker Hub
docker login -u <image_name>
# Or
docker login
Use
docker logoutto remove credentials.
Search for an Image on Docker Hub
docker search <image_name>
VOLUME
π List All Volumes
docker volume ls
π Create a New Named Volume
docker volume create <volume_name>
β Delete a Named Volume
docker volume rm <volume_name>
π Mounting Volumes
π Mount a Named Volume to a Running Container
docker run --volume <volume_name>:<mount_path>
# Or using --mount
docker run --mount type=volume,src=<volume_name>,dst=<mount_path>
β Mount an Anonymous Volume with Running Container
docker run --volume <mount_path>
π Create a Bind Mount
docker run --volume <host_path>:<container_path>
# Or using --mount
docker run --mount type=bind,src=<host_path>,dst=<container_path>
π§Ή Remove Unused Local Volumes
docker volume prune
π Note: Use this to remove anonymous volumes that are no longer used.
π Docker Networks β Quick Reference
π List All Networks
docker network ls
π οΈ Create a Network
docker network create <network_name>
β Remove a Network
docker network rm <network_name>
Remove All Unused Networks
docker network prune
β οΈ Warning: This will remove all unused networks. Ensure no containers depend on them.
Top comments (0)