Welcome to the Docker Cheatsheet! This cheat sheet provides a quick reference to essential Docker commands and their usage. Docker is a powerful platform for developing, shipping, and running applications in containers.
- List All Available Images
- Pull an Image
- List Running Containers
- List All Containers
- Start a Container
- Run a Container
- Run a Container with Options
- Stop a Container
- Kill a Container
- Remove a Container
- Remove a Container (Forcefully)
- Prune Images
- Prune Containers
- Exec command
docker images
docker pull <image_name>:<tag>
Pull the latest version of an image (if the tag is not mentioned)
docker ps
docker ps -a
docker start <container_name/id>
docker run <image_name>
Create and start a container, giving it a random name
docker run -d -p <host_port>:<container_port> --rm --name <container_name> <image_name>
Create and run a container with the specified options:
-d
: Run the container in detach mode, which means the container continues running even if we kill the terminal.-p <host_port>:<container_port>
: Map a port on the host to a port on the container.--rm
: Automatically remove the container when it exits.--name <container_name>
: Specify a custom name for the container.-it
: Run the container in interactive mode (Eg. docker run -it node).-e
: Used for mentioning the environment variables.-v
: For mind bound volumes. Syntax: -v hostmachine_working_directory:docker_container_directory. Eg.-v ($pwd):/app
( ($pwd) is a shortcut for getting current directory in mac or linux )
docker stop <container_name/id>
Stop a running container with cleanup
docker kill <container_name/id>
Forcefully terminate a container without cleanup
docker rm <container_name/id>
docker rm -f <container_name/id>
Forcefully remove a container (even if it's not stopped)
docker image prune
Remove all unused images
docker container prune
Remove all unused containers
docker exec [OPTIONS] <container_name/id> COMMAND [ARG...]
To execute a command inside a running container.(Eg. docker exec -it my-sql-container bash)