Stackvora.tech

Common Docker Commands

Effectively managing containers is crucial for modern application deployment and infrastructure. This reference provides an in-depth overview of…

Effectively managing containers is crucial for modern application deployment and infrastructure. This reference provides an in-depth overview of fundamental Docker commands, focusing on practical usage patterns for network engineers, sysadmins, cloud consultants, and developers. We will cover container lifecycle management, image operations, volume handling, network inspection, and resource monitoring, complete with command syntax and real-world considerations.

Container Lifecycle Management

Understanding how to start, stop, pause, and remove containers is foundational.

docker run: Create and Start Containers

The docker run command creates a new container and executes a command within it. It's often the first command used to bring an application online.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • -d, --detach: Run container in background (detached mode).
  • -p :: Publish a container's port(s) to the host. E.g., -p 8080:80.
  • --name : Assign a specific name to the container for easier reference.
  • -v :: Bind mount a volume. E.g., -v /opt/appdata:/var/www/html.
  • --restart : Restart policy (e.g., always, on-failure, no).
  • --env : Set environment variables inside the container.
  • --network : Connect the container to a specific Docker network.

Example: Run an Nginx container, exposing port 8080 on the host, named my-web-server, and restarting always.

docker run -d -p 8080:80 --name my-web-server --restart always nginx:latest

docker ps: List Containers

This command lists running containers and their essential details.

docker ps [OPTIONS]
  • -a, --all: Show all containers (default shows only running).
  • -s, --size: Display total file sizes.
  • -f , --filter : Filter output based on conditions (e.g., status=exited, name=my-app).
  • --format : Pretty-print containers using Go template.

Example: List all containers, including exited ones, showing their size.

docker ps -a -s

docker stop, docker start, docker restart: Manage Container State

These commands allow graceful termination, initiation, and re-initiation of containers.

docker stop my-web-server      # Sends SIGTERM, then SIGKILL after a timeout (default 10s)
docker start my-web-server     # Starts an exited container
docker restart my-web-server   # Stops and then starts a container

docker rm: Remove Containers

Use this to delete one or more containers. A container must be stopped before it can be removed unless -f is used.

docker rm [OPTIONS] CONTAINER [CONTAINER...]
  • -f, --force: Force removal of a running container (sends SIGKILL).
  • -v, --volumes: Remove anonymous volumes associated with the container.

Example: Remove an exited container named my-old-app and its associated anonymous volumes.

docker rm -v my-old-app

Container Interaction and Inspection

Accessing container logs, executing commands, and inspecting configurations are vital for debugging and monitoring.

docker logs: View Container Logs

Retrieves logs from a container, useful for troubleshooting applications.

docker logs [OPTIONS] CONTAINER
  • -f, --follow: Follow log output in real-time.
  • --tail : Output the specified number of lines from the end of the logs.
  • --since : Show logs since a specific timestamp (e.g., 2023-01-01T00:00:00Z or 10m for 10 minutes ago).
  • --timestamps: Show timestamps in the logs.

Example: Tail the logs of my-web-server, showing timestamps.

docker logs -f --timestamps my-web-server

docker exec: Execute Commands in a Running Container

This command allows you to run a command inside a running container, essential for debugging or administrative tasks.

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  • -i, --interactive: Keep STDIN open even if not attached.
  • -t, --tty: Allocate a pseudo-TTY. Often used with -i for interactive shell sessions.
  • -u , --user : Username or UID (and optionally groupname or GID) to use.

Example: Get an interactive bash shell in the my-web-server container.

docker exec -it my-web-server bash

If bash is not available, try sh: docker exec -it my-web-server sh.

docker inspect: Get Detailed Information

Provides a wealth of low-level information about Docker objects (containers, images, volumes, networks) in JSON format.

docker inspect [OPTIONS] NAME|ID [NAME|ID...]
  • --format