Skip to main content

Helpful Commands for Docker

Helpful Commands for Docker

  • sudo: Runs the command with superuser privileges. Docker commands often require such privileges to interact with the Docker daemon, which controls Docker containers and services.

View running containers

  • docker ps: This is the Docker command itself, composed of:
    • docker: The base command for the Docker CLI (Command Line Interface), which allows you to manage Docker containers and images.
    • ps: Short for "process status," this option is borrowed from the Unix command with the same name, which lists processes running on a system. In the context of Docker, it lists running containers.

When you run sudo docker ps, the command outputs a table with several columns of information about each container, including:

  • CONTAINER ID: A unique identifier for each container.
  • IMAGE: The base image used to create the container.
  • COMMAND: The command that was executed in the container.
  • CREATED: When the container was created.
  • STATUS: The current status of the container, such as "Up" followed by the duration.
  • PORTS: Any network ports that are mapped from the container to the host.
  • NAMES: A unique name assigned to the container.

This command is fundamental for Docker management tasks. It's used to quickly see which containers are running, monitor their status, and gather basic information about each container's configuration and behavior. This is especially useful in environments with multiple containers to ensure they are all running as expected or to identify any containers that may need attention.

sudo docker ps

View container logs

  • docker logs: This Docker command fetches the logs of a container.

    • docker: The base command for interacting with Docker from the command line.
    • logs: Specifies that you want to view the output logs of a container.
  • <container name>: Placeholder for the actual name or ID of the Docker container whose logs you want to view. You should replace <container name> with the specific name or ID of the container of interest.

Accessing container logs is a fundamental task in Docker container management. It allows administrators and developers to:

  • Diagnose and troubleshoot issues within a running container by providing insight into the application's runtime behavior and errors.
  • Monitor application behavior and performance in real-time or over a period.
  • Collect information for audit purposes or to analyze the behavior of an application in a production environment.

The information gained from docker logs is invaluable for maintaining the health and performance of containerized applications.

sudo docker logs <container name>

Monitor events Real-Time

  • docker events: This specific Docker command requests a live stream of system-level events from the Docker daemon.
    • docker: The base command for the Docker Command-Line Interface (CLI), which allows users to manage Docker containers and images.
    • events: Specifies that you are requesting information about real-time events generated by the Docker daemon.

The sudo docker events command is particularly useful for system administrators, developers, and DevOps professionals who need to:

  • Monitor Docker activities in real-time for debugging, auditing, or operational purposes.
  • Trigger automations or alerts based on specific Docker events.
  • Gain insights into the behavior and lifecycle of containers and other Docker entities in a dynamic, live environment.

This command can help with understanding the operational aspects of a Docker environment and with diagnosing issues by providing immediate feedback on what actions are taking place within the Docker daemon.

sudo docker events

Remove an image

  • docker image rm: This is a Docker command specifically for removing Docker images.

    • docker: The base command for interacting with Docker from the command line.
    • image: Specifies that the operation will be performed on Docker images.
    • rm: Short for "remove", this command deletes the specified image from the Docker host.
  • <image>: Placeholder for the actual name or ID of the Docker image you want to remove. You should replace <image> with the specific name or ID of the image. Docker images can be referred to either by their repository name and tag (e.g., ubuntu:latest) or by their unique image ID.

The sudo docker image rm <image> command is useful in various scenarios, such as:

  • Cleaning up disk space by removing unused Docker images.
  • Managing Docker images carefully in environments with limited resources.
  • Preparing environments for updated versions of Docker images by removing older ones.
  • Maintaining a clean and organized Docker environment by removing images that are no longer needed.

Removing Docker images is a basic maintenance task for Docker users, helping to control the Docker images present on a system and manage the host’s storage effectively.

sudo docker image rm <image> <image2> <image3>

Stop a stack with Docker Compose

Syntax: sudo docker compose down

Purpose: This command stops and removes all the containers, networks, and other resources defined in the docker-compose.yml file for the current project. It is a critical command for cleaning up after running your Docker Compose project, ensuring that no unnecessary resources are left running or consuming system resources.

Detailed Explanation:

  • When executed, docker compose down will first stop all the containers associated with the services defined in the Compose file. This is done gracefully, allowing containers to shut down properly.
  • After stopping the containers, it removes them from the system, along with any networks that were created specifically for this Compose project.
  • Optionally, you can remove volumes associated with the project by using the --volumes (or -v) flag, and remove images used by the project with --rmi flag.
  • It’s especially useful in development and testing environments for ensuring a clean state before redeploying or when no longer working on the project.
sudo docker compose down

Start a stack with Docker Compose

Syntax: sudo docker compose up -d

Purpose: This command deploys your Docker Compose application by creating and starting containers for all services defined in your docker-compose.yml file. The -d flag stands for "detached" mode, which means containers run in the background.

Detailed Explanation:

  • docker compose up processes the Compose file and starts services in dependency order, establishing networks and volume mounts as specified.
  • The -d (detached) flag is an optional flag used to run containers in the background. Without this flag, Docker Compose logs will be displayed in the terminal, and stopping the logs will stop the containers.
  • On the first execution, it builds any Docker images that are not found locally and are needed by the services, unless the images are being pulled from a registry.
  • This command ensures your entire stack is running as described in your Compose file, making it essential for both development and production environments.
  • If the services are already running, docker compose up -d will recreate containers if the configuration has changed, ensuring the running services match your current Compose file's specifications.
sudo docker compose up -d

Pull updated Images for a stack with Docker Compose

Syntax: sudo docker compose pull

Purpose: Used to download the Docker images used in the services defined in the Docker Compose file from a Docker registry (like Docker Hub) without starting the containers.

Detailed Explanation:

  • It iterates over all services that define an image property and attempts to pull the latest version of these images from their respective Docker registries.
  • This command is useful for ensuring that your local environment is updated with the latest versions of required images before starting services, especially in shared or team environments where image updates are frequent.
  • Running docker compose pull is particularly recommended in production environments before deploying updates to ensure all services are using their specified image versions.
sudo docker compose pull

Cleanup the Docker System

Syntax: sudo docker system prune

Purpose: This command removes unused Docker objects including containers, networks, images (both dangling and unreferenced), and optionally, volumes. It's a housekeeping command to free up space and remove clutter from your system.

Detailed Explanation:

  • By default, docker system prune will ask for confirmation to remove unused objects, which does not include volumes unless the --volumes flag is specified.
  • Dangling images (images not tagged and not referenced by any containers) and stopped containers are the primary targets for cleanup.
  • This command is particularly useful for system maintenance. It can significantly free up disk space by removing unnecessary Docker objects that are not in use.
  • Use with caution in production environments. Ensure that no critical data is stored in containers or volumes that might inadvertently be pruned.
  • It’s recommended to periodically run this command in development environments to maintain a clean Docker setup.
sudo docker system prune


Hi, how can I help you?