Essential Docker Commands

Master the essential Docker commands you'll use every day. This guide covers container management (run, ps, stop, rm), image management (images, pull, push, rmi), and useful flags that make Docker powerful.

docker run docker ps docker stop docker rm
Quick Command Reference
CommandDescription
docker run <image>Create and start a container from an image
docker psList running containers
docker ps -aList all containers (including stopped)
docker stop <container>Stop a running container
docker start <container>Start a stopped container
docker rm <container>Remove a stopped container
docker imagesList downloaded images
docker pull <image>Download an image from registry
docker push <image>Upload an image to registry
docker rmi <image>Remove an image
docker exec <container> <cmd>Run command in running container
docker logs <container>View container logs
docker system pruneClean up unused resources
docker run: Create and Start Containers

docker run is the primary command for creating and starting containers. It pulls the image if not already on your system, creates a new container, and starts it. Here are the most important flags you'll use:

-d (detached) runs the container in the background. Without this, the container runs in the foreground and takes over your terminal. Use -d for long-running services like web servers or databases.

-p (publish) maps a port on your host to a port in the container. For example, -p 8080:80 maps host port 8080 to container port 80. This allows you to access the container's service from your browser.

--name gives your container a custom name. Without this, Docker assigns a random name like "nostalgic_shannon". Naming containers makes them easier to reference later.

-v (volume) mounts a directory from your host into the container, useful for persisting data or sharing code.

# Run nginx in the foreground (press Ctrl+C to stop) docker run nginx # Run nginx in the background, map port 8080, name it web docker run -d -p 8080:80 --name web nginx # Run interactive Ubuntu container with shell access docker run -it ubuntu bash # Run with environment variable and volume docker run -d -e MYSQL_ROOT_PASSWORD=secret -v mysql-data:/var/lib/mysql mysql
Container Management: ps, stop, start, rm, logs

docker ps lists running containers. Use docker ps -a to see all containers, including those that have stopped. This shows container ID, image, command, creation time, status, ports, and name.

docker stop gracefully stops a running container by sending SIGTERM then SIGKILL. Use container ID or name. docker kill immediately stops a container (SIGKILL) without waiting.

docker start restarts a stopped container. docker restart stops and starts a container.

docker rm removes a stopped container, freeing disk space. Add -f to force remove a running container. docker logs shows the stdout/stderr output from a container, invaluable for debugging.

# List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a container docker stop web # Force stop (kill) a container docker kill container_name # Remove a stopped container docker rm web # Remove a running container (force) docker rm -f web # View container logs docker logs web # Follow logs in real-time docker logs -f web
Image Management: images, pull, push, rmi, tag

docker images lists all images stored on your system, showing repository, tag, image ID, size, and creation time. Images are the templates from which containers are created.

docker pull downloads an image from a registry (Docker Hub by default) without running it. This is useful for pre-loading images before running them.

docker push uploads your local image to a registry. You need to tag your image with your registry username first. For example, docker tag myapp username/myapp then docker push username/myapp.

docker rmi removes an image from your system. You cannot remove an image that has running containers. Use docker rmi -f to force removal, or stop and remove containers first.

# List images docker images # Pull an image without running docker pull nginx:alpine # Pull specific version docker pull postgres:15 # Tag an image for pushing to registry docker tag myapp myusername/myapp:latest docker tag myapp myusername/myapp:v1.0.0 # Push to Docker Hub docker push myusername/myapp:latest # Remove an image docker rmi myapp:latest # Remove unused images docker image prune
Running Commands Inside Containers: exec

docker exec lets you run commands inside a running container. This is essential for debugging, inspecting files, or running one-off tasks. The most common use is docker exec -it container bash to get an interactive shell inside the container.

Flags: -i keeps STDIN open, -t allocates a pseudo-TTY (together, -it gives you an interactive terminal). -e sets environment variables, -w sets working directory.

This command works on any running container, regardless of the base image (Ubuntu, Alpine, or distroless as long as it has a shell). For Alpine-based images, use sh instead of bash.

# Get bash shell inside container (if bash exists) docker exec -it web bash # Get sh shell (works on Alpine) docker exec -it web sh # Run a single command docker exec web ls -la /app # Run command with environment variable docker exec -e ENV=production web npm run migrate
docker exec -it web bash is invaluable for debugging. Once inside, you can check logs, examine files, test network connectivity, and verify that everything is configured correctly.
Cleanup Commands: prune

Over time, Docker can consume significant disk space with unused containers, images, volumes, and networks. The docker system prune command cleans up everything not in use. Use docker system prune -a to also remove unused images, including those without tags.

You can also prune specific resource types: docker container prune for containers, docker image prune for images, docker volume prune for volumes, and docker network prune for networks.

# Remove all stopped containers, unused networks, dangling images docker system prune # Remove everything (including all unused images) docker system prune -a # Remove only stopped containers docker container prune # Remove only dangling images docker image prune # Remove all unused images docker image prune -a # Remove unused volumes (caution: data loss!) docker volume prune
docker volume prune permanently deletes data. Always verify volumes aren't needed before pruning.
Docker Command Cheatsheet
PurposeCommand
Run container (foreground)docker run nginx
Run container (background)docker run -d nginx
Run with port mappingdocker run -d -p 8080:80 nginx
Run with custom namedocker run -d --name web nginx
Run interactive shelldocker run -it ubuntu bash
List running containersdocker ps
List all containersdocker ps -a
Stop containerdocker stop web
Start containerdocker start web
Remove containerdocker rm web
Force remove containerdocker rm -f web
View container logsdocker logs web
Follow logsdocker logs -f web
Execute command in containerdocker exec web ls
Get shell inside containerdocker exec -it web bash
List imagesdocker images
Pull imagedocker pull nginx
Tag imagedocker tag myapp user/myapp
Push imagedocker push user/myapp
Remove imagedocker rmi myapp
Clean up systemdocker system prune
Show Docker infodocker info
Show Docker versiondocker --version
Frequently Asked Questions
What's the difference between docker stop and docker kill?
docker stop sends SIGTERM, allowing the container to shut down gracefully (default 10 seconds). docker kill sends SIGKILL, immediately terminating the container. Use stop first; only use kill if the container is unresponsive.
How do I remove all stopped containers at once?
Use docker container prune to remove all stopped containers, or docker system prune to clean containers, networks, and dangling images.
Why is my container name already in use?
Container names must be unique. Either remove the existing container with docker rm <name> or use a different name with --name.
How do I see logs for a container that already exited?
Use docker ps -a to find the container, then docker logs <container_id> to view its logs.
What's the difference between docker run and docker start?
docker run creates a new container from an image and starts it. docker start restarts an existing, stopped container. Use docker run for first-time launches, docker start for subsequent launches.
How do I get a shell in an Alpine-based container?
Alpine uses sh instead of bash. Use docker exec -it container sh.
Why do I need to use sudo with docker commands on Linux?
The Docker daemon runs as root. Add your user to the docker group with sudo usermod -aG docker $USER to run docker without sudo, then log out and back in.
How do I see resource usage (CPU, memory) of containers?
Use docker stats to see real-time resource usage for all running containers, similar to Linux's top command.
Previous: What is Docker? Next: Docker Images Guide

Master these essential Docker commands and you'll be productive with containers from day one. Practice with different images and flags to build confidence.