Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Commonly Used Docker Commands

Notes 3

Basic Docker Commands

  • docker version: Retrieve installed Docker version details
  • docker info: View full Docker system overview, including total images, containers, and storage driver info
  • docker --help: Access built-in Docker help documentation for command references

Image Management Commands

  • docker images: List all locally stored Docker images
  • docker search <image-name>: Search for official and community images on Docker Hub
  • docker pull <image-name>:<tag>: Download a specified image from Docker Hub; omitting the tag defaults to the latest version
  • docker rmi <image-id>: Delete a local image by its ID
  • docker image prune: Remove all unused local images not referenced by any container

Container Management Commands

  • docker run [OPTIONS] IMAGE [COMMAND] [ARG...]: Create and launch a new cotnainer from a specified image
  • docker ps: List currently running containers; add the -a flag to show all containers including stopped ones
  • docker stop <container-id/name>: Gracefully stop a running container
  • docker start <container-id/name>: Launch a previously stopped container
  • docker restart <container-id/name>: Restart a running or stopped container
  • docker rm <container-id/name>: Delete a stopped container; use the -f flag to force remove a running container
  • docker exec -it <container-id/name> /bin/sh: Attach an interactive shell to a running container

Core Container Operation Examples

  1. Pull the official Nginx image:
docker pull nginx:latest
  1. List all local images:
docker images
  1. Create and run a detached Nginx container named web-demo mapped to host port 8080:
docker run --name web-demo -p 8080:80 -d nginx:latest
  1. View all containers (running and stopped):
docker ps -a
  1. Stop the web-demo container:
docker stop web-demo
  1. Reactivate the stopped web-demo container:
docker start web-demo
  1. Delete the stopped web-demo container:
docker rm web-demo
  1. Attach an interactive shell to the running web-demo container:
docker exec -it web-demo /bin/bash

Network Management Commands

  • docker network ls: List all existing Docker networks
  • docker network create <network-name>: Create a custom Docker bridge network
  • docker network rm <network-name>: Delete an unused Docker network
  • docker network connect <network-name> <container-name>: Attach a container to a specified network
  • docker network disconnect <network-name> <container-name>: Remove a container from a specified network

Volume Management Commands

  • docker volume ls: List all locally created Docker volumes
  • docker volume create <volume-name>: Create a named Docker volume for persistent storage
  • docker volume inspect <volume-name>: View detailed configuration and path info for a volume
  • docker volume rm <volume-name>: Delete an unused Docker volume

Docker Compose Commands

Docker Compose is a tool for defining and running multi-container Docker applications using YAML configuration files.

  • docker-compose up -d: Launch all services defined in docker-compose.yml in detached mode
  • docker-compose down --volumes: Stop and remove all services, along with associated volumes
  • docker-compose ps: List active Compose-managed services
  • docker-compose logs <service-name>: View log output for a specified service
  • docker-compose exec <service-name> <command>: Run a command inside a running Compose service

Docker Swarm Cluster Commands

Docker Swarm provides native container orchestration for deploying and managing clustered container applications.

  • docker swarm init --advertise-addr <node-ip>: Initialize a new Swarm cluster on the current node
  • docker swarm join --token <join-token> <manager-ip:port>: Add a worker node to an existing Swarm cluster
  • docker node ls: List all nodes in the active Swarm cluster
  • docker service create --replicas 3 -p 80:80 --name web-service nginx:latest: Deploy a replicated service on the Swarm
  • docker service ls: List all services running in the Swarm cluster
  • docker service scale web-service=5: Adjust the number of replicas for a Swarm service
  • docker service rm web-service: Remove a specifeid Swarm service

Advanced Container and Image Operations

  1. Create a custom image from a modified container: First list all containers to get the target ID:
docker ps -a

Then commit the changes to a new image:

docker commit <container-id> custom-nginx:v1
  1. Export a container to a tar archive:
docker export <container-id> > demo-container.tar
  1. Import an exported tar archive as a new image:
cat demo-container.tar | docker import - imported-image:v1
  1. Save a local image to a compressed tar file:
docker save -o nginx-backup.tar nginx:latest
  1. Load an image from a tar archive:
docker load -i nginx-backup.tar
  1. Build an image from a Dockerfile in the current directory:
docker build -t my-app:v1 .
  1. View real-time resource usage for a running container:
docker stats web-demo
  1. Clean up unused Docker resources:
    • Remove all stopped containers: docker container prune
    • Remove all unused volumes: docker volume prune
    • Remove all unused images, networks, and volumes: docker system prune -a

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.