Docker Command Practice
Docker runs a process in an isolated filesystem you build as an image and start as a container. This track drills run, exec, logs, volumes, networks, and Compose so you can debug a stack from the terminal instead of a dashboard.
Command Reference
container
| Command | Description |
|---|---|
docker ps | List running containers. |
docker ps -a | List all containers, including stopped ones. |
docker run <image> | Create and start a new container from an image. |
docker run -d <image> | Run a container in detached (background) mode. |
docker start <container> | Start one or more stopped containers. |
docker stop <container> | Stop one or more running containers. |
docker restart <container> | Restart one or more containers. |
docker rm <container> | Remove one or more stopped containers. |
docker logs <container> | Fetch the logs of a container. |
docker logs -f <container> | Follow log output from a container. |
docker port <container> | Show port mappings of a container. |
docker rename <container> <name> | Rename an existing container. |
docker exec -it <container> <command> | Run a command inside a running container interactively. |
docker inspect <name> | Return low-level information on Docker objects. |
docker top <container> | List running processes inside a container. |
docker stats <container> | Stream live resource usage statistics for a container. |
docker pause <container> | Pause all processes in a running container. |
docker ps -s | List running containers with their CPU and memory usage. |
docker run -it <image> | Run a container interactively with a TTY attached. |
docker run -p <host>:<container> <image> | Run a container and publish a port to the host. |
docker create <image> | Create a new container from an image without starting it. |
docker unpause <container> | Resume all paused processes in a container. |
docker kill <container> | Send a SIGKILL signal to stop a container immediately. |
docker attach <container> | Attach local stdin/stdout/stderr to a running container. |
docker update --cpu-shares <num> -m <size> <container> | Update resource limits of a running container. |
docker wait <container> | Block until a container stops, then print its exit code. |
docker events <container> | Stream real-time events from a container. |
docker diff <container> | Show filesystem changes made to a container since it started. |
image
| Command | Description |
|---|---|
docker images | List locally stored images. |
docker pull <image> | Download an image from a registry. |
docker push <image> | Upload an image to a registry. |
docker rmi <image> | Remove one or more local images. |
docker build -t <name> . | Build an image from a Dockerfile in the current directory. |
docker build -f <file> . | Build an image using a specific Dockerfile path. |
docker tag <source> <target> | Create a new tag that refers to an existing image. |
docker history <image> | Show the history of an image's layers. |
docker save <image> > <file> | Save an image to a tar archive file. |
docker image prune | Remove all dangling images not tagged and not referenced by any container. |
docker image prune -a | Remove all images not used by any existing container. |
docker load < <file> | Load an image from a tar archive file. |
docker commit <container> | Create a new image from a container's current state. |
volume
| Command | Description |
|---|---|
docker volume ls | List Docker volumes. |
docker volume create <name> | Create a named volume. |
docker volume rm <name> | Remove one or more volumes. |
docker volume inspect <name> | Display detailed information on one or more volumes. |
docker volume prune | Remove all volumes not used by at least one container. |
network
| Command | Description |
|---|---|
docker network ls | List Docker networks. |
docker network rm <network> | Remove one or more Docker networks. |
docker network connect <network> <container> | Connect a container to a network. |
docker network disconnect <network> <container> | Disconnect a container from a network. |
docker network create <name> | Create a new Docker network. |
docker network inspect <network> | Display detailed information about a Docker network. |
docker network create -d <driver> <name> | Create a Docker network with a specific driver (bridge, overlay, etc.). |
compose
| Command | Description |
|---|---|
docker compose up | Create and start containers defined in a Compose file. |
docker compose up -d | Start Compose services in detached mode. |
docker compose down | Stop and remove containers, networks created by Compose. |
docker compose ps | List containers for a Compose project. |
docker compose logs -f | Follow log output from Compose services. |
docker compose build | Build or rebuild services defined in a Compose file. |
registry
| Command | Description |
|---|---|
docker search <term> | Search Docker Hub for images matching a term. |
docker login | Authenticate to a Docker registry. |
docker logout | Log out from a Docker registry. |
system
| Command | Description |
|---|---|
docker info | Display system-wide information about the Docker installation. |
docker system prune | Remove unused data such as stopped containers and dangling images. |
docker system prune -a | Remove all unused data including stopped containers and all unused images. |
docker container prune | Remove all stopped containers. |
docker system df | Show Docker disk usage. |
docker stop $(docker ps -a -q) | Stop all running containers at once. |
docker rmi -f $(docker images -q) | Force-remove all local images. |
docker rm -f $(docker ps -a -q) | Force-remove all containers, including running ones. |
Key Use Cases
- Start a container, attach a shell, and read its logs
- Bind a named volume so data survives container recreate
- Connect two services on a user-defined bridge network
- Bring a Compose stack up, scale a service, and tear it down
- Prune unused images and check disk usage with docker system
Frequently Asked Questions
What is the difference between docker run and docker start?
run creates a new container from an image. start resumes an existing container ID. Restarting a named container is start, not a second run.
When do I need docker exec instead of docker attach?
exec opens a new process inside a running container (often a shell). attach reconnects to the main process stdio. Use exec for debugging without hijacking PID 1.
Bind mount or named volume?
A bind mount maps a host path. A named volume is managed by Docker and survives recreate. This track tests both docker run -v forms and volume subcommands.
Does Compose replace docker run?
Compose files declare multi-service stacks. You still need run, exec, and logs for one-off containers and live debugging.
Ready to master Docker commands?
Test your muscle memory with our spaced-repetition quiz system. Free forever.
