Back to all posts
May 3, 20262 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 run | Run a new container |

| docker ps | List all running containers |

| docker stop | Stop a running container |

| docker rm | Remove a stopped container |

| docker logs | View container logs |

| docker exec | Execute a command in a running container |

2. **Syntax**

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

* [command]: the Docker command to run (e.g., run, ps, stop)

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

* [arguments]: the values required by the command (e.g., image name, container ID)

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

3. **Common Patterns**

* Run a new container and open a shell: docker run -it --name container_name image_name /bin/bash

* Map a port from the container to the host: docker run -p host_port:container_port image_name

* Run a container in detached mode: docker run -d --name container_name image_name

* Run a command in a running container: docker exec -it container_name command

4. **Gotchas**

* Forgetting to specify the -it flags when running a container with an interactive shell can cause the container to exit immediately.

* Not mapping ports correctly can prevent access to the container's services.

* Using the wrong container ID or name can result in the wrong container being affected.

* Not cleaning up stopped containers can lead to disk space issues.

5. **Related Commands**

* docker-compose: for managing multi-container applications

* docker swarm: for managing clusters of Docker containers

* docker volume: for managing persistent data in containers

Example use case:

bash
# Run a new container and open a shell
docker run -it --name my_container ubuntu /bin/bash

# Run a command in the running container
docker exec -it my_container ls -l

# Stop and remove the container
docker stop my_container
docker rm my_container

Enjoyed this?

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