Essential Docker Commands: A Complete Reference Guide

Essential Docker Commands: A Complete Reference Guide
Photo by Bernd đź“· Dittrich / Unsplash

Docker has revolutionized how developers build, ship, and run applications by providing lightweight containerization. Whether you're just starting with Docker or need a quick reference, mastering these essential commands will significantly improve your development workflow.

Image Management Commands

Pulling Images

The foundation of working with Docker starts with obtaining images from registries like Docker Hub.

docker pull [IMAGE_NAME]

Pull the latest version of an image:

docker pull nginx

Pull a specific version:

docker pull nginx:1.21

Listing Images

View all locally stored images:

docker images

For a more compact view:

docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

Building Images

Create custom images from a Dockerfile:

docker build -t [IMAGE_NAME] .

Build with a specific tag:

docker build -t myapp:v1.0 .

Build from a different directory:

docker build -t myapp -f /path/to/Dockerfile /path/to/context

Removing Images

Remove a specific image:

docker rmi [IMAGE_ID]

Remove all unused images:

docker image prune

Force remove an image (even if containers are using it):

docker rmi -f [IMAGE_ID]

Container Lifecycle Commands

Running Containers

Start a new container from an image:

docker run [OPTIONS] [IMAGE] [COMMAND]

Common run options:

# Run in detached mode (background)
docker run -d nginx

# Run with port mapping
docker run -p 8080:80 nginx

# Run with environment variables
docker run -e NODE_ENV=production myapp

# Run with volume mounting
docker run -v /host/path:/container/path nginx

# Run interactively with a terminal
docker run -it ubuntu bash

Listing Containers

View running containers:

docker ps

View all containers (including stopped):

docker ps -a

View container IDs only:

docker ps -q

Starting and Stopping Containers

Start a stopped container:

docker start [CONTAINER_ID]

Stop a running container:

docker stop [CONTAINER_ID]

Restart a container:

docker restart [CONTAINER_ID]

Force kill a container:

docker kill [CONTAINER_ID]

Removing Containers

Remove a stopped container:

docker rm [CONTAINER_ID]

Remove a running container (force):

docker rm -f [CONTAINER_ID]

Remove all stopped containers:

docker container prune

Container Interaction Commands

Executing Commands in Containers

Run a command in a running container:

docker exec [CONTAINER_ID] [COMMAND]

Open an interactive shell:

docker exec -it [CONTAINER_ID] bash

Run a command as a specific user:

docker exec -u root [CONTAINER_ID] whoami

Viewing Container Output

View container logs:

docker logs [CONTAINER_ID]

Follow log output in real-time:

docker logs -f [CONTAINER_ID]

View last 100 lines of logs:

docker logs --tail 100 [CONTAINER_ID]

Inspecting Containers

Get detailed information about a container:

docker inspect [CONTAINER_ID]

View container resource usage:

docker stats [CONTAINER_ID]

View processes running in a container:

docker top [CONTAINER_ID]

File Operations

Copying Files

Copy files from container to host:

docker cp [CONTAINER_ID]:/path/in/container /host/path

Copy files from host to container:

docker cp /host/path [CONTAINER_ID]:/path/in/container

Network Commands

Listing Networks

View all Docker networks:

docker network ls

Creating Networks

Create a custom network:

docker network create [NETWORK_NAME]

Create a network with specific driver:

docker network create --driver bridge mynetwork

Connecting Containers to Networks

Connect a container to a network:

docker network connect [NETWORK_NAME] [CONTAINER_ID]

Run a container on a specific network:

docker run --network [NETWORK_NAME] nginx

Volume Management

Listing Volumes

View all Docker volumes:

docker volume ls

Creating Volumes

Create a named volume:

docker volume create [VOLUME_NAME]

Using Volumes

Mount a volume when running a container:

docker run -v [VOLUME_NAME]:/path/in/container nginx

Removing Volumes

Remove a specific volume:

docker volume rm [VOLUME_NAME]

Remove all unused volumes:

docker volume prune

System Maintenance Commands

System Information

View Docker system information:

docker info

Check Docker version:

docker version

Cleaning Up Resources

Remove all unused containers, networks, and images:

docker system prune

Remove everything including volumes:

docker system prune -a --volumes

View disk usage:

docker system df

Docker Compose Commands

When working with multi-container applications, Docker Compose provides additional commands:

Starting Services

Start all services defined in docker-compose.yml:

docker-compose up

Start in detached mode:

docker-compose up -d

Stopping Services

Stop all services:

docker-compose down

Stop and remove volumes:

docker-compose down -v

Viewing Service Status

List running services:

docker-compose ps

View service logs:

docker-compose logs [SERVICE_NAME]

Best Practices and Tips

Using Tags Effectively

Always specify image tags in production environments rather than relying on latest. This ensures consistency and prevents unexpected updates.

Container Naming

Use meaningful names for containers to make management easier:

docker run --name web-server nginx

Resource Management

Monitor resource usage regularly and clean up unused containers, images, and volumes to free up disk space.

Security Considerations

Avoid running containers as root when possible, and use specific image tags rather than generic ones to reduce security risks.

Conclusion

These Docker commands form the foundation of container management and will handle most day-to-day Docker operations. As you become more comfortable with these basics, you can explore advanced features like multi-stage builds, custom networks, and orchestration tools. Regular practice with these commands will make container management second nature and significantly improve your development workflow.

Remember that Docker commands often have many additional options and flags. Use docker [COMMAND] --help to explore all available options for any specific command.