Mastering Docker Commands: A Hands-On Guide

Welcome back! In this lecture, we’ll take a closer look at some of the essential Docker commands. To make things practical, we’ll run through these commands on a live Linux system. So, let’s dive right in and start mastering Docker commands.

List Containers with docker ps

First things first, we need to understand how to list running containers. The primary command for this is docker ps. However, I strongly recommend using docker container ps for clarity. So, give it a try:

docker container ps

In this initial example, you’ll likely see an empty list since no containers are currently running. But worry not; we’ll run some containers in a moment.

If you want to view all containers, including the stopped ones, you can use the -a option with docker container ps:

docker container ps -a

This command will display both running and stopped containers.

To list only the container IDs without additional details, you can use the -q option, which stands for quiet mode. Here’s how you do it:

docker container ps -q

This can be particularly useful when you want to perform batch operations on containers.

Running Containers with docker container run

The heart of Docker is creating and running containers, and you do that using the docker run command. In recent Docker releases, there’s an alternative command, docker container run, which offers the same functionality. While both commands currently work, it’s a good practice to start using docker container run to align with other Docker subcommands.

Let’s run a simple container to see how it works. In this example, we’ll use the hello-world container:

docker container run hello-world

When you execute this command, Docker will pull the hello-world image from the Docker registry, create a container from it, and run the default command, which displays a “Hello from Docker!” message.

Customizing Container Names

By default, Docker generates container names for you. However, you can assign custom names to your containers by using the --name option. Let’s run an Ubuntu container with a custom name, “a1alpine”:

docker container run --name a1alpine alpine

The --name option allows you to provide user-friendly names to containers, making it easier to work with them.

Running Interactive and Detached Containers

Docker containers can be run in different modes. The -i option allows interactive mode, while the -t option attaches a terminal. To illustrate this, let’s run an interactive Ubuntu container with a terminal attached:

docker container run -it --name u1 ubuntu

This command launches the Ubuntu container in interactive mode, providing a shell that you can work in. You can run various commands inside the container, and when you exit, the container will stop.

For detached mode, use the -d option. Here’s how you run a detached Ubuntu container:

docker container run -d --name u2 ubuntu

The detached container runs in the background. You can later interact with it using the docker exec command.

Executing Commands Inside Containers

Once you have a detached container running, you can execute commands within it using the docker exec command. To illustrate, let’s run a shell command inside a detached Ubuntu container:

docker exec -it u2 bash

This command enters the detached container and starts a Bash shell. You can run commands and perform tasks within the container environment.

Running and Cleaning Up with --rm

A useful option when running containers is --rm, which tells Docker to automatically remove the container once it exits. This can be convenient for disposable containers you don’t need to keep. Here’s how you can use it:

docker container run --rm --name u100 ubuntu

In this example, you run a container named “u100” using the --rm option. When you exit the container, it will be automatically removed.


You’ve now been introduced to some of the fundamental Docker commands. We’ve explored how to list containers, run them with custom names, work with interactive and detached modes, execute commands within containers, and use the --rm option for automatic cleanup. With these commands in your toolkit, you’re well on your way to mastering Docker and managing containers with confidence.

In the next lecture, we’ll delve into more Docker commands and explore advanced topics such as managing ports. Stay tuned for more Docker mastery!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top