Back to all posts
May 3, 20263 min readAI-generated

Docker Commands Cheat Sheet

1. Quick Reference

cheat-sheetreferencequick

TechSilo

Curated by human, written by AI

1. **Quick Reference**

| Command | Description |

| --- | --- |

| docker ps | List all running containers |

| docker ps -a | List all containers (running and stopped) |

| docker run -it | Run a new container from an image and open a terminal |

| docker stop | Stop a running container |

| docker rm | Remove a stopped container |

| docker images | List all available images |

| docker rmi | Remove an image |

2. **Syntax**

The basic syntax for a Docker command is: docker [options] [arguments].

* : the Docker command to execute (e.g., run, stop, rm)

* [options]: optional flags to modify the command's behavior (e.g., -it, -a)

* [arguments]: required or optional values for the command (e.g., , )

Example: docker run -it --name my_container ubuntu /bin/bash

* docker: the Docker command

* run: the command to execute

* -it: options to allocate a pseudo-TTY and keep STDIN open

* --name my_container: option to specify the container name

* ubuntu: the image to use

* /bin/bash: the command to execute inside the container

3. **Common Patterns**

* Run a new container from an image and open a terminal:

bash
docker run -it --name <container_name> <image> /bin/bash

* Create a new image from a Dockerfile:

bash
docker build -t <image_name> <path_to_dockerfile>

* Push an image to a Docker registry:

bash
docker tag <image_name> <registry_url>/<image_name>
docker push <registry_url>/<image_name>

* Pull an image from a Docker registry:

bash
docker pull <registry_url>/<image_name>

4. **Gotchas**

* Forgetting to specify the container name: If you don't specify a container name, Docker will generate a random one, making it harder to manage your containers.

* Not using the -it flags: Without these flags, you won't be able to interact with the container.

* Not specifying the correct image: If you specify an incorrect image, Docker will try to pull it from the registry, which can lead to errors.

* Not cleaning up stopped containers: Stopped containers can take up disk space, so it's essential to remove them regularly.

5. **Related Commands**

* docker-compose: a tool for defining and running multi-container Docker applications

* docker swarm: a tool for clustering and orchestrating Docker containers

* kubectl: a command-line tool for managing Kubernetes clusters

Example of using docker-compose:

yml
version: '3'
services:
  web:
    build: .
    ports:
    - "80:80"

Enjoyed this?

This post was AI-generated and human-curated. Want more like this?