Published on

How to Manage Docker Containers: A 30-Minute Beginner’s Guide

Managing Docker containers effectively involves using the Docker CLI (Command Line Interface) to create, monitor, and clean up resources through simple commands like docker run, docker ps, and docker rm. By following a standard lifecycle—starting, stopping, and removing containers—you can keep your development environment organized and prevent your computer from running out of memory. Most beginners can master these core management tasks in under 30 minutes.

What are the first steps to running a container?

To manage containers, you first need to start one. You do this by pulling an image (a read-only template containing your software) from a registry like Docker Hub and turning it into a running container.

It is normal to feel nervous about running commands for the first time, but Docker is designed to be very forgiving. You can always delete what you create and start over.

  1. Step 1: Open your terminal. This could be Command Prompt on Windows, Terminal on macOS, or your preferred Linux shell.
  2. Step 2: Run a test container. Type the following command to start a simple web server.
# docker run: starts a new container
# -d: runs it in 'detached' mode (in the background)
# -p 8080:80: maps port 8080 on your computer to port 80 inside the container
# --name my-web-server: gives your container a friendly name
# nginx: the name of the software image to use
docker run -d -p 8080:80 --name my-web-server nginx
  1. Step 3: Verify it works. Open your web browser and go to localhost:8080. You should see a "Welcome to nginx!" message.

How can you see what is currently running?

Once you have containers started, you need to keep track of them. If you leave too many containers running at once, they will consume your computer's RAM (Random Access Memory - the "short-term" memory your computer uses to run apps).

To see your active containers, use the ps command. This command acts like a task manager for your Docker environment.

# docker ps: lists all currently running containers
docker ps

If you want to see every container you have ever started, even those that have stopped or crashed, you need to add a specific flag (an extra instruction added to a command).

# -a: stands for 'all'; shows running and stopped containers
docker ps -a

What is the best way to stop and remove containers?

Stopping a container is like turning off a light; the container still exists, but it isn't using active processing power. Removing a container is like throwing away the lightbulb; it clears the files off your system entirely.

We’ve found that naming your containers makes this process much easier because you don't have to memorize long ID numbers. To stop the web server you created earlier, use the stop command.

# docker stop: sends a signal to the container to shut down safely
docker stop my-web-server

After stopping it, the container still takes up a small amount of disk space. To fully delete it, use the rm (remove) command.

# docker rm: deletes the container permanently
docker rm my-web-server

How do you monitor container health and logs?

Sometimes a container might not work as expected. To fix this, you need to look at the logs (a chronological record of everything the software inside the container has said or done).

Accessing logs is the fastest way to troubleshoot errors. If your web server isn't loading, the logs will usually tell you why.

# docker logs: prints the output from inside the container
# my-web-server: the name of the container you want to check
docker logs my-web-server

You can also check how much CPU and memory your containers are using in real-time. This is helpful if your computer starts feeling slow.

# docker stats: shows a live stream of resource usage statistics
docker stats

How can you clean up unused Docker resources?

As you experiment, you will likely accumulate "dangling" images and stopped containers that you no longer need. These can quickly eat up gigabytes of storage space on your hard drive.

Docker provides a powerful "prune" command to help with this. This command is a "janitor" for your system that sweeps away everything not currently in use.

# docker system prune: removes all stopped containers and unused networks
# This will ask for confirmation before deleting anything
docker system prune

Don't worry if you accidentally prune something. Since Docker images are stored in the cloud, you can always download them again using the docker pull command.

What are the common mistakes beginners make?

One common mistake is trying to run a new container with the same name as an old, stopped container. Docker will give you an error saying "Conflict. The container name is already in use."

To fix this, you must either remove the old container with docker rm or give your new container a different name. Another frequent issue is forgetting to map ports.

If you run a web server but forget the -p flag, the server will run inside the container, but you won't be able to see it in your browser. Always double-check your port mappings if you can't reach your application.

What should you learn after the basics?

Once you are comfortable managing individual containers, you should explore how to manage groups of containers at once. This is where tools like Docker Compose come in.

Docker Compose allows you to define a whole stack of software—like a database and a website—in a single file. You can then start or stop everything with one command instead of typing individual lines for every piece of software.

You might also want to learn about "Volumes." These allow you to save data (like a database file) so that it doesn't disappear when you delete a container.

For more information, visit the official Docker documentation.


Read the Manage Documentation