🎓 A1Learner
🔍
0 of 50 lessons completed

Docker Run, Step by Step

docker run takes flags that shape the container: -d for background, -p for ports, --name for a name, and -it for a terminal. Each flag does one job.

Run detached with a name

This container runs in the background under the name g3-web.

docker run -d --name g3-web nginx:1.27-alpine

Run with a terminal

The -it flags open a shell inside a throwaway container.

docker run -it --rm alpine:3.20 sh

Run and remove

The --rm flag removes the container when it stops.

docker run --rm hello-world

Example

docker run -d --name g3-web -p 8081:80 nginx:1.27-alpine >/dev/null
docker ps --format "{{.Names}} {{.Image}} {{.Ports}}"

Key points

  • Use -d to run detached, in the background.
  • Use -p to map a host port to a container port.
  • Use --name to give the container a name for later commands.
  • Use -it to attach a terminal and type inside the container.
  • Use --rm to remove the container when it stops.

In this example, docker run starts nginx detached, with a name and a port mapping. docker ps then lists it by name, image and ports. The row proves each flag took effect.

Note

Flags come before the image name. The command after the image runs inside the container.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Which flag runs a container in the background?