LESSON · 1 OF 6

docker run

docker run

docker run is the command you will type more than any other. It takes an image, creates a container from it, and starts the process inside. The basic shape is:

bash
docker run IMAGE [command]

If the image is not already on the machine, Docker pulls it from the registry first, then runs it.

Running a command in a container

Most images have a default command, but you can override it by adding your own after the image name. This runs echo inside a fresh Alpine Linux container:

bash
docker run alpine echo "hello from inside a container"

Docker pulls the tiny alpine image, starts a container, runs echo, and the moment echo finishes the container stops. A container lives exactly as long as its main process. When the process exits, so does the container.

The foreground is the default

By default docker run attaches your terminal to the container and shows its output directly, then hands control back when it exits. Try a command that lists the container's own filesystem:

bash
docker run alpine ls /

You see the root directory of the container, not your host. That is the isolation from the last topic in action - the process sees its own filesystem.

Pulling happens once

The first docker run alpine downloads the image. Every run after that reuses the cached copy and starts almost instantly. You do not pull by hand for normal use - docker run handles it.

Commands this node introduces

  • docker run IMAGE - create and start a container from an image
  • docker run IMAGE command args - override the default command
Spin up a fresh environment and practice live.
docker · fresh machine · ready in under a minute