Running & Listing Containers

docker run - the Big One

Every Docker session starts the same way: you have an image (that read-only template from the last module) sitting on your machine, and you want to actually run it as a container (a live process). The command that does that is docker run - it's the single most-typed Docker command, so it's worth reading one example letter by letter before anything else:

$ docker run -d --name web -p 8080:80 nginx:alpine

Reading left to right:

FlagMeaning
-ddetached - run in the background
--name weba human name (otherwise Docker invents one like angry_swanson)
-p 8080:80publish port: host 8080 → container 80
nginx:alpinethe image (always last, before any command)
Tip: The -p HOST:CONTAINER order trips everyone up at least once. The left number is the port you browse to; the right one is what the app listens on inside.

See what's running (-a includes stopped ones - crucial when things crash):

$ docker ps
CONTAINER ID   IMAGE          STATUS         PORTS                  NAMES
3f2a91c04d11   nginx:alpine   Up 2 minutes   0.0.0.0:8080->80/tcp   web
$ docker ps -a          # also shows Exited / Created containers
Note: docker run = create + start. A container that failed to start still exists - docker ps -a is the first debugging command you should reach for.