Docker Command Cheat Sheet: The Ultimate Guide for Container Enthusiasts

Docker Command Cheat Sheet: The Ultimate Guide for Container Enthusiasts

Explore the World of Docker: Pull, Push, and List Images Like a Pro

·

3 min read

Table of contents

No heading

No headings in the article.

Here's a list of common Docker commands with explanations:

  1. docker run

    • Description: Run a command in a new container.

    • Parameters:

      • -d or --detach: Run the container in the background.

      • -it: Allocate a pseudo-TTY and keep stdin open (interactive mode).

      • --rm: Automatically remove the container when it exits.

      • -p or --publish: Publish a container's port(s) to the host.

      • -v or --volume: Bind mount a volume.

      • -e or --env: Set environment variables.

      • --name: Assign a name to the container.

    • Example: docker run -d -p 80:80 --name my-web-server nginx

  2. docker build

    • Description: Build an image from a Dockerfile.

    • Parameters:

      • -t or --tag: Name and optionally a tag in the name:tag format.

      • --file or -f: Name of the Dockerfile (default is 'Dockerfile').

      • .: Path to the build context (usually the current directory).

    • Example: docker build -t my-image .

  3. docker pull

    • Description: Pull an image or a repository from a registry.

    • Parameters:

      • --all-tags: Download all tagged images in the repository.
    • Example: docker pull ubuntu:latest

  4. docker push

    • Description: Push an image or a repository to a registry.

    • Parameters:

      • --all-tags: Push all tagged images into the repository.
    • Example: docker push my-image:latest

  5. docker images

    • Description: List images.

    • Parameters:

      • -a or --all: Show all images (default hides intermediate images).

      • --no-trunc: Don't truncate output.

    • Example: docker images

  6. docker ps

    • Description: List containers.

    • Parameters:

      • -a or --all: Show all containers (default shows only running).

      • -q or --quiet: Only display container IDs.

    • Example: docker ps

  7. docker stop

    • Description: Stop one or more running containers.

    • Parameters:

      • container: ID or name of the container to stop.
    • Example: docker stop my-web-server

  8. docker start

    • Description: Start one or more stopped containers.

    • Parameters:

      • container: ID or name of the container to start.
    • Example: docker start my-web-server

  9. docker rm

    • Description: Remove one or more containers.

    • Parameters:

      • -f or --force: Force the removal of a running container.

      • container: ID or name of the container to remove.

    • Example: docker rm my-web-server

  10. docker rmi

    • Description: Remove one or more images.

    • Parameters:

      • -f or --force: Force removal of the image.

      • image: ID or name of the image to remove.

    • Example: docker rmi my-image:latest

To generalize and combine commands, you can use shell scripting, command substitution, or pipes. For example, you can stop and remove all running containers using a single command:

docker stop $(docker ps -q) && docker rm $(docker ps -a -q)

In this example, we use command substitution $(...) to get the list of running container IDs from docker ps -q and pass them to docker stop. Then, we use the && operator to chain the commands, so after stopping the containers, we use docker ps -a -q to get the list of all container IDs and pass them to docker rm.

Another example is removing all dangling images:

docker rmi $(docker images -f "dangling=true" -q)

Here, we use docker images -f "dangling=true" -q to get the list of dangling image IDs and pass them to docker rmi using command substitution.

Remember to be cautious when using commands that delete containers or images, especially if you are combining them with other commands. It's a good practice to check the output of the commands you're chaining together before executing them to prevent unintentional deletions.

As you become more familiar with Docker commands and their parameters, you can create more complex sequences and use them in your daily work to automate tasks and increase efficiency.