Back to course

Lesson 4: Your First Container and Essential Commands

Docker Zero to Hero: The Complete Containerization Course for Beginners

Lesson 4: Your First Container and Essential Commands

We will now move beyond hello-world and run a useful application: an Alpine Linux container.

Running a Container Interactively

The most fundamental command is docker run. We will use three essential flags:

  • -it: Stands for interactive (-i) and TTY (-t). This allows us to interact with the container's shell.
  • --rm: Automatically removes the container filesystem when the container exits. Great for temporary tasks.

Let's start an Alpine container and execute the ls -l command:

bash docker run -it --rm alpine sh

Breakdown:

  1. docker run: Execute a container.
  2. -it: Connect our terminal to the container's shell.
  3. --rm: Delete the container when we exit.
  4. alpine: The image name.
  5. sh: The command (shell) to execute inside the container.

Once inside the container, you will see the shell prompt changes (e.g., # /).

Try running a few commands inside the container:

bash / # ls -l / # echo "Hello from the container!" / # exit

When you type exit, the container stops and is automatically removed because of the --rm flag.

Listing Running Containers

To see which containers are currently active, use docker ps:

bash docker ps

Since our Alpine container exited immediately, this list will likely be empty.

Listing All Containers (Including Stopped Ones)

To see all containers that have been run (including stopped ones):

bash docker ps -a

Notice that the container created in the previous step (without --rm) will be listed here with a status like Exited (0) 5 seconds ago.