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.
| Command | Description |
|---|---|
docker run <image> | Create and start a container from an image |
docker ps | List running containers |
docker ps -a | List 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 images | List 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 prune | Clean up unused resources |
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
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
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
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.
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.
| Purpose | Command |
|---|---|
| Run container (foreground) | docker run nginx |
| Run container (background) | docker run -d nginx |
| Run with port mapping | docker run -d -p 8080:80 nginx |
| Run with custom name | docker run -d --name web nginx |
| Run interactive shell | docker run -it ubuntu bash |
| List running containers | docker ps |
| List all containers | docker ps -a |
| Stop container | docker stop web |
| Start container | docker start web |
| Remove container | docker rm web |
| Force remove container | docker rm -f web |
| View container logs | docker logs web |
| Follow logs | docker logs -f web |
| Execute command in container | docker exec web ls |
| Get shell inside container | docker exec -it web bash |
| List images | docker images |
| Pull image | docker pull nginx |
| Tag image | docker tag myapp user/myapp |
| Push image | docker push user/myapp |
| Remove image | docker rmi myapp |
| Clean up system | docker system prune |
| Show Docker info | docker info |
| Show Docker version | docker --version |
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.docker container prune to remove all stopped containers, or docker system prune to clean containers, networks, and dangling images.docker rm <name> or use a different name with --name.docker ps -a to find the container, then docker logs <container_id> to view its logs.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.sh instead of bash. Use docker exec -it container sh.sudo usermod -aG docker $USER to run docker without sudo, then log out and back in.docker stats to see real-time resource usage for all running containers, similar to Linux's top command.Master these essential Docker commands and you'll be productive with containers from day one. Practice with different images and flags to build confidence.