Skip to content

Your First Docker Container


Be exicted and ready to run your first Docker contanier. In this lesson, you’ll run your first Docker container and see how Docker turns an image into a running container. You’ll also learn what happens behind the scenes when you use docker container run.

Installation and Setup

Follow the official Docker documentation to install Docker for your operating system.

After installation, you can allow your user to run Docker commands without sudo:

sudo usermod -aG docker $USER

This command adds your user to the docker group, allowing you to run Docker commands without sudo.

Log out and log back in for the change to take effect.

Run Your First Container

First, just identify which OS or Application you want. Suppose it’s Ubuntu OS you want to After that, running your first container is as easy as this:

docker run container ubuntu echo "My First Container"

When you are starting, don’t get overwhelmed by command — with some practice you will be in God’s mode. The command is telling to run echo "My First Container" in Ubuntu OS container. If your first container is truely your first container, then you will get the output similar to the following:

Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
617772c7d19b: Pull complete
a7fb98a8eddd: Pull complete
cc2ffdbc1bf7: Download complete
Digest: sha256:678c6550cc43645e08669028bc177f50be4e7c5b8cca677067b1914d4afc7a03
Status: Downloaded newer image for ubuntu:latest
My First Container

Let’s break the output through diagram:

    sequenceDiagram
    participant A as Docker Client
    participant B as Docker Daemon
    participant C as Docker Registry
    participant D as Ubuntu Container

    A->>B: docker container run ubuntu<br>echo "My First Container"
    B->>B: Check for ubuntu image locally

    alt Image found locally
        B->>D: Create container
    else Image not found locally
        B->>C: Pull ubuntu:latest
        C-->>B: Return image layers
        B->>D: Create container
    end

    B->>D: Start container with<br>echo "My First Container"
    D->>D: Execute echo
    D-->>B: stdout: My First Container
    B-->>A: My First Container
  

When you run docker container run ubuntu echo "My First Container", the Docker Client sends the request to the Docker Daemon. The daemon checks if the Ubuntu image exists locally; if not, it downloads it from the Docker Registry. Conceptually, it then creates and starts a container from that image with the echo command. The command runs inside the container, and its output is returned to you.

Since echo runs and exits immediately, the container stops as soon as the command finishes, and Docker returns its output to you.

Wanna see your container? Issue:

docker container ls -a

Without -a, Docker only return running container. Since our container has been stopped, -a is required to list it.

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
56ce8c18b226   ubuntu    "echo 'My First Cont…"   16 seconds ago   Exited (0) 14 seconds ago             zen_elbakyan

Try Your Second Container

Now that ubuntu image is locally available after your first container, if you run your second container:

docker container run ubuntu echo "My Second Container"

The output is only:

My Second Container

All the hassle of pulling and getting image locally is only the first time you start the container using particular image.

Cleanup: Remove Containers

We have run two containers in this chapter. docker container ls -a shows that:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
1ee3e6c38938   ubuntu    "echo 'My Second Con…"   12 minutes ago   Exited (0) 12 minutes ago             pedantic_burnell
56ce8c18b226   ubuntu    "echo 'My First Cont…"   52 minutes ago   Exited (0) 52 minutes ago             zen_elbakyan

We can remove or delete them to cleanup:

docker container prune

Warning

If you were already working on Docker before, there might be more containers that might be useful for you. So, delete with caution. This exercise was only for this lesson learners.

Approve that you want to delete all stopped containers:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
1ee3e6c38938bf4036ec2fe7b90f5c772561c9488948bb6085621e58bc63d0af
56ce8c18b226ed53190f44a90b1d3b7b605dacc910a1052d9241e85f9b1eef0d

Total reclaimed space: 8.192kB

Verify with:

docker container ls -a

You won’t see any output.

Last updated on