Quick Reference
Docker common commands reference
Setting Up Docker
Install Docker
Verify Docker installation:
docker --versionDocker Images
Building an Image
To create a Docker image from a Dockerfile:
docker build -t your-image-name:tag .your-image-name: Name of the image.tag: Version tag (commonlylatestfor the most recent version).
Listing Images
To view all Docker images on your system:
docker imagesRemoving an Image
To remove a specific Docker image:
docker rmi your-image-name:tagRemoving All Images
To remove all Docker images:
docker rmi $(docker images -q)Docker Containers
Running a Container
To run a Docker container from an image:
docker run -p host-port:container-port your-image-name:tagListing Running Containers
To view all running containers:
docker psListing All Containers
To see all containers, including stopped ones:
docker ps -aStopping a Container
To stop a specific running container:
docker stop container-id-or-nameRemoving a Container
To remove a stopped container:
docker rm container-id-or-nameRemoving All Containers
To remove all containers:
docker rm $(docker ps -aq)Debugging and Interactivity
Running a Container Interactively
To run a container in interactive mode (often used with a shell):
docker run -it your-image-name:tag /bin/shBash Into a Running Container
To enter the shell of a running container:
docker exec -it container-id-or-name /bin/shMaintenance and Cleanup
Removing Stopped Containers, Unused Volumes, and Networks
To perform a cleanup:
docker system pruneRemoving Unused Images
To remove dangling (unused) images:
docker system prune -aOther Handy Commands
Viewing Logs of a Container
To view the logs of a running or stopped container:
docker logs container-id-or-nameCopying Files from/to a Container
To copy files between your host and a container:
# From container to host
docker cp container-id-or-name:/path/in/container /path/on/host
# From host to container
docker cp /path/on/host container-id-or-name:/path/in/containerDebugging and Interactivity with Docker Containers
Debugging containers often requires entering the container's environment, executing commands, or inspecting files. Here's how to navigate these tasks.
Entering a Container's Shell
Sometimes, to debug or inspect the container's internals, you may want to get a shell inside a running container. Here's how you can do that:
docker exec -it container-id-or-name /bin/shIf the container is using a different shell, you might need to replace
/bin/shwith/bin/bashor another appropriate shell.
Running a Container with an Interactive Shell
If you are just starting a container and want it to give you a shell immediately, use:
docker run -it your-image-name:tag /bin/shViewing Container Logs
To view the logs produced by a running or a stopped container:
docker logs container-id-or-nameInspecting Container Metadata
For a detailed report on a container (like IP addresses, volumes, environment variables, etc.):
docker inspect container-id-or-nameChecking Container Processes
To see which processes are running inside a container:
docker top container-id-or-nameDebugging a Container That Won't Start
If a container is failing to start, it's often useful to override its entrypoint to keep it running and then bash into it to inspect:
docker run -it --entrypoint /bin/sh your-image-name:tagLast updated
