keepcalculator

06 Numbers and Powers

Docker Commands: 7 Terminal Basics That Make It Click

Card No. 06·52130 · Updated · Standard formula, shown below

Yes, Docker becomes far less intimidating once you learn seven core terminal commands: docker pull, docker run, docker ps, docker stop, docker rm, docker images, and docker rmi. These commands cover the essential workflow of getting an image, starting a container, checking what’s running, stopping it, and cleaning up—everything a beginner needs to move from confusion to confidence.

What Docker Actually Does

Docker packages software into containers—lightweight, isolated environments that include everything the software needs to run: code, runtime, system tools, libraries, and settings. Unlike virtual machines, containers share the host operating system’s kernel, so they start in seconds and use far less memory. A Docker image is a read-only template; a container is a running instance of that image. You can have many containers from the same image, each isolated from the others.

Why Docker Feels Complicated at First

The initial confusion usually comes from the vocabulary and the sheer number of commands. Terms like image, container, volume, and registry are new, and the official documentation lists dozens of subcommands. But the core workflow is simple: you pull an image, run it, list your containers, stop one, and remove what you no longer need. Once you internalize that loop, the rest is just variations.

The 7 Commands That Unlock Everything

  • docker pull <image> – Downloads an image from a registry (default is Docker Hub). Example: docker pull nginx fetches the official Nginx web server image.
  • docker run <image> – Creates and starts a container from an image. Add flags like -d (detached, run in background), -p 8080:80 (map host port 8080 to container port 80), and --name mynginx to give it a name.
  • docker ps – Lists running containers. Add -a to show all containers, including stopped ones. This is your go-to for checking what’s active.
  • docker stop <container> – Gracefully stops a running container. You can use the container ID or name.
  • docker rm <container> – Deletes a stopped container. Use docker rm -f to force-remove a running one (not recommended unless necessary).
  • docker images – Lists all images you have downloaded locally, with their tags and sizes.
  • docker rmi <image> – Removes an image from your local disk. You must stop and delete any containers using it first.

How to Use Them Together: A Real Workflow

Here’s a typical beginner exercise: run a simple web server. First, pull the image: docker pull nginx. Then run it in detached mode with port mapping: docker run -d -p 8080:80 --name mynginx nginx. Now open http://localhost:8080 in your browser—you’ll see the Nginx welcome page. Check it’s running with docker ps. To stop it: docker stop mynginx. To remove the container: docker rm mynginx. To remove the image: docker rmi nginx. That’s the entire lifecycle.

Common Mistakes and How to Avoid Them

  • Forgetting -d for background tasks – Without it, the container runs in the foreground, blocking your terminal. Press Ctrl+C to stop it, but that also stops the container.
  • Port conflicts – If port 8080 is already in use, docker run fails with “port is already allocated.” Change the host port, e.g., -p 8081:80.
  • Confusing images and containers – docker rm removes containers, docker rmi removes images. You can’t remove an image while a container uses it.
  • Using docker stop vs docker kill – stop sends a SIGTERM (graceful), kill sends SIGKILL (immediate). Prefer stop for normal shutdown.
  • Not cleaning up – Over time, unused images and stopped containers consume disk space. Use docker system prune to remove all unused data (but be careful—it deletes everything not in use).

How to Choose Between Commands and Flags

You don’t need to memorize every flag. Start with the basics: -d for detached, -p for port mapping, -it for interactive terminal (useful for running commands inside a container), and --name for easy identification. For most beginner tasks, these four flags cover 90% of what you’ll do. As you grow, you’ll add -v for volumes (persistent data) and --env for environment variables, but those can wait until you’re comfortable with the core seven.

Docker vs. Virtual Machines and Alternatives

Containers are often compared to virtual machines (VMs). VMs virtualize hardware and run a full operating system, while containers virtualize the OS and share the host kernel. This makes containers lighter and faster to start, but less isolated—a kernel vulnerability could affect all containers. Alternatives to Docker include Podman (daemonless, rootless), containerd (a lower-level runtime), and LXC (Linux Containers). For most developers, Docker’s ecosystem and tooling make it the best starting point, but knowing Podman exists is useful if you need a daemonless option.

Safety and Best Practices

  • Only pull images from trusted sources – Docker Hub has official images (e.g., nginx, python) that are maintained. Third-party images may contain vulnerabilities.
  • Don’t run containers as root – Use the --user flag or a Dockerfile that creates a non-root user.
  • Keep images updated – Regularly docker pull to get security patches.
  • Use docker system df – Shows disk usage of images, containers, and volumes so you know what to clean.
  • Never store secrets in images – Use environment variables or Docker secrets instead.

Frequently Asked Questions

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 container that you stopped (using docker stop). You can’t start a container that was never created; you must run it first.

Why does docker ps show nothing even though I ran a container?

If you ran a container without -d, it runs in the foreground and exits when you close the terminal. Also, if the container’s main process finishes (e.g., a script that runs and exits), the container stops. Use docker ps -a to see stopped containers and check their logs with docker logs <container>.

How do I get a shell inside a running container?

Use docker exec -it <container> /bin/bash. If bash isn’t available, try /bin/sh. This opens an interactive terminal inside the container, letting you inspect files or run commands.

What does -p 8080:80 actually do?

It maps port 8080 on your host machine to port 80 inside the container. Traffic sent to localhost:8080 is forwarded to the container’s port 80, where the web server listens. The left side is the host port, the right side is the container port.

Can I run multiple containers from the same image?

Yes, each docker run creates a separate container with its own filesystem and network namespace. They don’t interfere unless you explicitly share volumes or ports. If you map the same host port, the second container will fail—use different host ports.

How do I remove all unused Docker data?

Run docker system prune -a to remove all stopped containers, unused networks, dangling images, and build cache. Add --volumes to also delete unused volumes, but be careful—volumes may contain persistent data you want to keep.

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

The whole Numbers and Powers drawer →