Docker CLI
Container management from the command line
🐳 What is Docker CLI?
Docker CLI is the command-line interface for Docker that lets you build, run, and manage containers. Containers package applications with their dependencies, making them portable and consistent across different environments.
# Check Docker version
docker --version
# View Docker info
docker info
Output:
Docker version 24.0.5, build ced0996 Server Version: 24.0.5
Core Docker Commands
Run
Start a new container
docker run nginx
List
Show running containers
docker ps
Stop
Stop a running container
docker stop <id>
Build
Create image from Dockerfile
docker build -t app .
🔹 Running Containers
Create and start Docker containers from images using the docker run command with extensive configuration options for deployment. Basic execution docker run nginx starts a container, while docker run -d -p 8080:80 --name webserver nginx runs detached with port mapping and naming. Include environment variables with -e KEY=value, mount volumes with -v /host/path:/container/path, and set resource limits with --memory and --cpus flags. These run options provide complete control over container initialization and runtime behavior.
# Run container in background
docker run -d nginx
# Run with port mapping
docker run -p 8080:80 nginx
# Run with name
docker run --name my-nginx nginx
# Run with environment variables
docker run -e "ENV=production" node
# Run interactively
docker run -it ubuntu bash
Output:
a3f5c8d9e2b1 Container my-nginx started on port 8080
🔹 Managing Containers
Monitor and control Docker container lifecycles using management commands for status checking, modification, and cleanup operations. View all containers with docker ps -a, check specific container details using docker inspect containername, and stop running instances with docker stop containername. Restart containers via docker restart, remove unwanted containers with docker rm, and prune all stopped containers using docker container prune. These management commands ensure efficient container orchestration and resource utilization across development and production environments.
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container
docker stop container_name
# Start a stopped container
docker start container_name
# Restart a container
docker restart container_name
# Remove a container
docker rm container_name
# Remove all stopped containers
docker container prune
Output:
CONTAINER ID IMAGE STATUS PORTS a3f5c8d9e2b1 nginx Up 2 minutes 80/tcp
🔹 Working with Images
Manage Docker image lifecycle including acquisition, inspection, tagging, and cleanup for efficient container deployment preparation. Pull images from registries using docker pull nginx:latest, list local images with docker images, and build custom images from Dockerfiles via docker build -t myapp:v1 .. Tag images for version control with docker tag myapp:latest myapp:v1.2, and remove unused images using docker rmi imagename. Proper image management ensures consistent deployments and optimal storage utilization across container environments.
# Pull an image from Docker Hub
docker pull ubuntu:22.04
# List all images
docker images
# Build image from Dockerfile
docker build -t myapp:1.0 .
# Tag an image
docker tag myapp:1.0 myapp:latest
# Remove an image
docker rmi image_name
# Remove unused images
docker image prune
Output:
REPOSITORY TAG IMAGE ID SIZE ubuntu 22.04 3b418d7b466a 77.8MB myapp 1.0 7f2e4c8a9d1b 125MB
🔹 Container Logs and Inspection
Access container operational data through logging and inspection commands for monitoring, debugging, and performance analysis. View logs in real-time with docker logs -f containername, examine detailed configuration via docker inspect containername, and check resource usage statistics with docker stats. Execute commands inside running containers using docker exec -it containername /bin/bash for interactive troubleshooting. These diagnostic tools provide comprehensive visibility into container behavior and facilitate rapid issue resolution in production environments.
# View container logs
docker logs container_name
# Follow logs in real-time
docker logs -f container_name
# View last 100 lines
docker logs --tail 100 container_name
# Inspect container details
docker inspect container_name
# View container stats
docker stats
# Execute command in running container
docker exec -it container_name bash
Output:
2024-01-15 10:30:45 Server started on port 80 2024-01-15 10:31:12 GET / 200 OK CONTAINER ID CPU % MEM USAGE a3f5c8d9e2b1 0.5% 50MB / 2GB
🔹 Docker Volumes
Implement persistent data storage using Docker volumes to maintain information across container lifecycles and ensure data durability. Create named volumes with docker volume create myvolume, mount them to containers using -v myvolume:/container/path, and list existing volumes via docker volume ls. Inspect volume details with docker volume inspect myvolume and remove unused volumes through docker volume rm myvolume. Volume management enables stateful applications, database persistence, and shared storage solutions in containerized environments.
# Create a volume
docker volume create my-volume
# Run container with volume
docker run -v my-volume:/data nginx
# Mount host directory
docker run -v /host/path:/container/path nginx
# List volumes
docker volume ls
# Inspect volume
docker volume inspect my-volume
# Remove volume
docker volume rm my-volume
Output:
my-volume DRIVER VOLUME NAME local my-volume
🔹 Docker Networks
Establish container communication channels using Docker networks for isolated, secure, and organized multi-service applications. Create custom networks with docker network create mynetwork, connect containers using docker network connect mynetwork containername, and list available networks via docker network ls. Inspect network configurations with docker network inspect mynetwork and disconnect containers when needed. Network management enables microservices architecture, load balancing configurations, and secure service isolation within container ecosystems.
# List networks
docker network ls
# Create a network
docker network create my-network
# Run container on network
docker run --network my-network nginx
# Connect container to network
docker network connect my-network container_name
# Disconnect from network
docker network disconnect my-network container_name
# Inspect network
docker network inspect my-network
Output:
NETWORK ID NAME DRIVER a1b2c3d4e5f6 bridge bridge 7g8h9i0j1k2l my-network bridge
🔹 Docker Compose Basics
Orchestrate multi-container applications using Docker Compose YAML configuration files for simplified deployment and management. Start all services with docker-compose up -d, stop them gracefully using docker-compose down, and view consolidated logs via docker-compose logs -f. Scale specific services with docker-compose up --scale service=3 and validate configuration files using docker-compose config. Compose simplifies complex application stacks, enabling development environment consistency and production deployment standardization across teams and environments.
# Start services from docker-compose.yml
docker-compose up
# Start in background
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs
# List running services
docker-compose ps
# Rebuild and start
docker-compose up --build
Output:
Creating network "app_default" Creating app_web_1 ... done Creating app_db_1 ... done