The most important Docker commands are docker run, docker ps, docker images, docker build, docker pull, docker exec, docker stop, and docker rm. With these eight commands you can pull or build an image, start a container, list what is running, execute commands inside it, stop it, and clean up. Below is a practical breakdown of each command, how they work together, and the pitfalls that trip up most beginners.
Background: Containers vs. Virtual Machines
Docker packages an application with its dependencies into a container, which runs on the host kernel but in isolated user space. Unlike a virtual machine (VM), a container does not include a full operating system; it shares the host’s kernel. This makes containers lighter and faster to start, often in under a second. The Docker CLI is the primary way users interact with the Docker daemon, which manages images, containers, networks, and volumes. Understanding the difference between an image (a read-only template) and a container (a running instance of an image) is the foundation for using the commands correctly.
How Docker Commands Work
Docker commands follow a simple pattern: docker [option] [command] [arguments]. The CLI sends a request to the Docker daemon via a REST API, and the daemon performs the action. Most commands require sudo unless your user is in the docker group. Here are the core commands you will use daily:
docker pull <image>– Downloads an image from a registry (default is Docker Hub). Example:docker pull nginx.docker images– Lists all locally stored images with their repository, tag, image ID, and size.docker run <image>– Creates and starts a container from an image. The most common options are-d(detached, runs in background),-p host:container(port mapping),--name(assign a name), and-v host:container(mount a volume). Example:docker run -d -p 8080:80 --name web nginx.docker ps– Shows running containers. Add-ato show all containers, including stopped ones.docker exec -it <container> <command>– Runs a command inside a running container. The-itflags allocate a pseudo-TTY and keep stdin open, which is needed for interactive shells likebash.docker stop <container>– Gracefully stops a running container (sends SIGTERM).docker rm <container>– Removes a stopped container. Use-fto force remove a running one.docker rmi <image>– Removes an image. You must stop and delete all containers using it first, or use-f.
Choosing the Right Command for the Job
New users often confuse docker run with docker start. docker run creates a new container from an image; docker start restarts an existing stopped container. Similarly, docker exec is for running commands in a running container, while docker run can run a one-off command in a new container. If you just want to test an image, use docker run --rm <image> <command> – the --rm flag automatically removes the container when it exits, saving cleanup. For interactive debugging, use docker run -it <image> /bin/bash to get a shell inside a fresh container.
Common Problems and How to Fix Them
Several errors appear frequently:
- “Cannot connect to the Docker daemon” – The daemon is not running or your user lacks permission. Start Docker (systemctl start docker on Linux, or launch Docker Desktop on macOS/Windows) and ensure your user is in the
dockergroup. - Port already in use – If
docker run -p 8080:80fails with “bind: address already in use”, another process is using port 8080. Change the host port (e.g.,-p 8081:80) or find and kill the process. - Container exits immediately – Many images (like
alpine) have no long-running process by default. Use a command that stays alive, such asdocker run -it alpine /bin/sh. - “Unable to find image locally” – This is not an error; Docker automatically pulls the image if it is not present. If the pull fails, check your internet connection or the image name (e.g.,
nginxvs.nginx:latest). - Permission denied – On Linux, add your user to the docker group with
sudo usermod -aG docker $USERand log out/in, or usesudofor every command.
Useful Commands Beyond the Basics
Once you master the basics, these commands will make your workflow smoother:
docker logs <container>– Shows the logs of a container. Add-fto follow the output live.docker inspect <container>– Returns detailed JSON metadata about the container (IP address, mounts, config).docker build -t <name> .– Builds an image from a Dockerfile in the current directory. The-tflag tags the image.docker system prune– Removes unused containers, networks, and dangling images. Add-ato also remove unused images. This is the easiest way to reclaim disk space.docker cp <container>:<path> <host-path>– Copies files between a container and the host.docker network ls– Lists networks. Containers on the same user-defined network can communicate by name.
Safety and Best Practices
Running containers with root privileges inside can be a security risk. Always use the least-privileged user if the image supports it (e.g., --user 1000). Avoid using docker run --privileged unless absolutely necessary, as it gives the container full access to the host. Also, be careful with volume mounts: -v /host:/container gives the container read/write access to the host directory, which can overwrite or delete files. For sensitive data, use Docker secrets or environment variables, not hard-coded values in images. Finally, always specify a tag (e.g., nginx:1.25) instead of latest for production, because latest changes over time and can break reproducibility.
Alternatives to the Docker CLI
While the CLI is the standard, other tools can simplify container management:
- Docker Compose – Defines multi-container applications in a YAML file. Instead of long
docker runcommands, you writedocker-compose.ymland usedocker compose up. - Podman – A daemonless alternative that is CLI-compatible with Docker. It runs containers as a non-root user by default.
- Kubernetes – For orchestrating containers across multiple machines. It uses
kubectlrather than Docker commands, but Docker images are used directly. - Portainer – A web-based UI that wraps the Docker API, letting you manage containers visually.
For most single-container tasks, the CLI is sufficient. Compose becomes essential when you have multiple services (e.g., a web app and a database).
Frequently Asked Questions
What is the difference between docker run and docker start?
docker run creates a new container from an image and starts it. If you run it again with the same image, you get a second container. docker start restarts an existing container that was stopped, preserving its filesystem and configuration. Use docker ps -a to see stopped containers that you can start.
How do I get a shell inside a running container?
Use docker exec -it <container-name> /bin/bash. If the image does not have bash (like Alpine), use /bin/sh instead. The -it flags are necessary for an interactive session; without them, the command runs non-interactively and exits immediately.
Why does my container exit immediately after starting?
Most likely the container’s main process finished. Containers run only as long as the process specified in the image (or in your docker run command) is alive. If you run docker run alpine without a command, Alpine’s default shell exits immediately. Use docker run -it alpine /bin/sh to keep it alive, or run a service like nginx that stays in the foreground.
How do I remove all stopped containers and unused images?
Use docker system prune -a. This removes all stopped containers, unused networks, and all images not used by any container. Add --volumes to also delete unused volumes, but be careful: volumes may contain important data. To remove only containers, use docker container prune.
What is the difference between docker rm and docker rmi?
docker rm removes a container (a running or stopped instance). docker rmi removes an image (the template). You cannot remove an image while a container is using it, even if that container is stopped. Delete the container first, then the image.
Can I use Docker commands on Windows or macOS?
Yes, Docker Desktop provides the Docker CLI on both platforms. The commands are identical, but the underlying virtualization differs (Hyper-V on Windows, HyperKit on macOS). On Windows, you may need to run commands in PowerShell or CMD, and line endings can affect shell scripts inside containers.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It