DOCKER Commands Cheatsheet — Essential Reference
Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. These containers are isolated from each other and bundle their own software, libraries, and configuration files, ensuring consistent environments across different machines.
Master DOCKER with Top Reference Books
Level up your DevOps skills. Find the best guide books and cheatsheet references for your engineering stack.
Commands
| Command | Description | Example |
|---|---|---|
docker run |
Runs a command in a new container. If the image is not found locally, Docker will pull it from Docker Hub. | docker run -it --rm ubuntu:latest /bin/bash |
docker ps |
Lists running containers. Use -a to show all containers (running and stopped). | docker ps -a |
docker images |
Lists all local Docker images. | docker images |
docker build |
Builds an image from a Dockerfile and a context. | docker build -t myapp:1.0 . |
docker stop |
Stops one or more running containers. | docker stop my_container_name |
docker rm |
Removes one or more stopped containers. | docker rm my_container_name |
docker rmi |
Removes one or more local images. | docker rmi myapp:1.0 |
docker exec |
Runs a command in a running container. | docker exec -it my_container_name /bin/bash |
docker-compose up |
Builds, (re)creates, starts, and attaches to containers for a service defined in a docker-compose.yml file. | docker-compose up -d |
docker-compose down |
Stops and removes containers, networks, volumes, and images created by `up`. | docker-compose down |
Pro Tips
- Always use `--rm` with `docker run` for ephemeral containers to automatically remove them when they exit, preventing clutter.
- Utilize `docker-compose` for multi-container applications to manage their lifecycle and networking easily.
- Inspect containers and images with `docker inspect <container_id_or_name>` or `docker inspect <image_id_or_name>` for detailed information.
- Clean up unused Docker objects (containers, images, volumes, networks) regularly with `docker system prune -a`.
Frequently Asked Questions
What is the difference between an image and a container?
An image is a read-only template with instructions for creating a Docker container. A container is a runnable instance of an image. You can think of an image as a class and a container as an object of that class.
How do I install Docker?
Installation varies by operating system. For Linux, follow the official Docker Engine installation guide for your distribution. For Windows and macOS, download and install Docker Desktop from the official Docker website.
How can I share data between my host and a container?
You can use bind mounts or volumes. Bind mounts link a file or directory on the host machine to a path inside the container. Volumes are managed by Docker and are the preferred way for persistent data storage.