Docker Cheat Sheet
Most usefule commands¶
docker run
: Runs a container from an image.docker start
: Starts a container that has been stopped.docker stop
: Stops a running container.docker restart
: Restarts a running container.docker rm
: Removes a container.docker rmi
: Removes an image.docker ps
: Lists all running containers.docker images
: Lists all images on the system.docker exec
: Runs a command in a running container.docker build
: Builds an image from a Dockerfile.docker pull
: Pulls an image from a registrydocker push
: Pushes an image to a registry.docker login
: Logs in to a registry.docker logout
: Logs out of a registry.docker cp
: Copies files or directories between a container and the host machine.docker volume
: Manages volumes for containers.docker network
: Manages networks for containers.
Example:
# Run a container from the image "myimage"
docker run -it myimage
# Start a container named "mycontainer"
docker start mycontainer
# Stop a container named "mycontainer"
docker stop mycontainer
# Remove a container named "mycontainer"
docker rm mycontainer
# Remove an image named "myimage"
docker rmi myimage
# List all running containers
docker ps
# Build an image from the Dockerfile in the current directory
docker build -t myimage .
# Pull an image named "myimage" from the "myregistry" registry
docker pull myregistry/myimage
# Push an image named "myimage" to the "myregistry" registry
docker push myregistry/myimage
Note: (Almost) Always use --rm
option while running the container to remove the container automatically when it exits.
More¶
Shell¶
- For a temporary shell in a new container (exploring an image):
docker run -it –rm/bin/sh (or /bin/bash) - For a shell in an already running container:
docker exec -it/bin/sh (or /bin/bash)
Container Inspection & Interaction¶
docker logs <container_id_or_name>
: Fetches the logs of a container.- Often used with
-f
or--follow
to stream logs live:docker logs -f mycontainer
- Often used with
docker inspect <container_id_or_name | image_id_or_name | volume_name | network_name>
: Returns low-level information on Docker objects (containers, images, volumes, networks) in JSON format. Very useful for debugging.docker ps -a
(ordocker container ls -a
): Lists all containers, including stopped ones (your sheet mentionsdocker ps
for running ones, but-a
is crucial).docker top <container_id_or_name>
: Displays the running processes inside a container.docker port <container_id_or_name>
: Lists the port mappings for a container.docker attach <container_id_or_name>
: Attaches your terminal’s standard input, output, and error (or any combination) to a running container. (Oftenexec
is preferred for new commands, butattach
can re-attach to the main process).docker kill <container_id_or_name>
: Sends a SIGKILL signal to a container (force stop).docker stop
sends SIGTERM first, then SIGKILL after a grace period.
Image Management¶
docker tag <source_image[:tag]> <target_image[:tag]>
: Creates a tagtarget_image
that refers tosource_image
. Essential for versioning and pushing to different repositories/tags.- Example:
docker tag myimage myregistry/myusername/myimage:v1.0
- Example:
docker history <image_id_or_name>
: Shows the history of an image (the layers and commands used to build it).docker save -o <path_to_tar_file> <image_name>
: Saves one or more images to a tar archive (streamed to STDOUT by default).docker load -i <path_to_tar_file>
: Loads an image from a tar archive or STDIN.
System & Cleanup¶
docker system prune
: Removes unused data:- Stopped containers
- Dangling images (those without a tag and not referenced by any container)
- Unused networks
- Unused build cache
- Often used with
-a
to remove all unused images (not just dangling ones) and--volumes
to also prune unused volumes. Use with caution, especially--volumes
!
docker info
: Displays system-wide information about the Docker installation.docker version
: Shows the Docker version information (client and server).docker stats <container_id_or_name...>
: Displays a live stream of container(s) resource usage statistics (CPU, memory, network I/O, block I/O).
Volume Management¶
docker volume ls
: Lists volumes.docker volume create <volume_name>
: Creates a volume.docker volume inspect <volume_name>
: Displays detailed information on one or more volumes.docker volume rm <volume_name>
: Removes one or more volumes.
Network Management¶
docker network ls
: Lists networks.docker network create <network_name>
: Creates a network.docker network inspect <network_name>
: Displays detailed information on one or more networks.docker network rm <network_name>
: Removes one or more networks.docker network connect <network_name> <container_name>
: Connects a container to a network.docker network disconnect <network_name> <container_name>
: Disconnects a container from a network.
Important docker run
Flags¶
-d
or--detach
: Run container in background and print container ID.-p <host_port>:<container_port>
(e.g.,-p 8080:80
): Publish a container’s port(s) to the host.-v <host_path_or_volume_name>:<container_path>
(e.g.,-v mydata:/data
or-v /path/on/host:/path/in/container
): Mount a volume or bind mount a host directory.--name <container_name>
: Assign a name to the container.-e <VAR_NAME>=<value>
(e.g.,-e "NODE_ENV=production"
): Set environment variables.--restart <policy>
(e.g.,--restart always
,--restart on-failure
): Restart policy to apply when a container exits.--network <network_name>
: Connect a container to a specific network.
Docker Compose¶
docker-compose up [-d]
docker-compose down [-v --rmi all]
docker-compose ps
docker-compose logs [-f]
docker-compose build
docker-compose exec <service_name> <command>
#dockerfile #containers
Page last modified: 2025-06-04 18:53:31