Refer to the official Docker documentation for OS-specific installation instructions.
Verify Docker installation:
docker --version
Docker 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 (commonly latest for the most recent version).
Listing Images
To view all Docker images on your system:
docker images
Removing an Image
To remove a specific Docker image:
docker rmi your-image-name:tag
Removing 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:tag
Listing Running Containers
To view all running containers:
docker ps
Listing All Containers
To see all containers, including stopped ones:
docker ps -a
Stopping a Container
To stop a specific running container:
docker stop container-id-or-name
Removing a Container
To remove a stopped container:
docker rm container-id-or-name
Removing 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/sh
Bash Into a Running Container
To enter the shell of a running container:
docker exec -it container-id-or-name /bin/sh
Maintenance and Cleanup
Removing Stopped Containers, Unused Volumes, and Networks
To perform a cleanup:
docker system prune
Removing Unused Images
To remove dangling (unused) images:
docker system prune -a
Other Handy Commands
Viewing Logs of a Container
To view the logs of a running or stopped container:
docker logs container-id-or-name
Copying 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/container
Debugging 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/sh
If the container is using a different shell, you might need to replace /bin/sh with /bin/bash or 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/sh
Again, replace /bin/sh if a different shell is used in the container.
Viewing Container Logs
To view the logs produced by a running or a stopped container:
docker logs container-id-or-name
Inspecting Container Metadata
For a detailed report on a container (like IP addresses, volumes, environment variables, etc.):
docker inspect container-id-or-name
Checking Container Processes
To see which processes are running inside a container:
docker top container-id-or-name
Debugging 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:tag
This will override the container's default entry point with a shell, allowing you to start the intended command manually or inspect why it might be failing.