docker run: Usage, Options & Common CI Errors
Create and start a container from an image, with the flags you actually use.
docker run creates a container from an image and starts it. It is by far the most-used Docker command. This page covers the essential flags and the run errors that show up in CI.
What it does
docker run is docker create plus docker start in one step. It pulls the image if missing, creates a writable container layer, and runs the image command (or a command you pass).
Common usage
docker run --rm -it ubuntu:22.04 bash
docker run -d -p 8080:80 --name web nginx
docker run --rm -e NODE_ENV=test -v "$PWD":/app -w /app node:20 npm test
docker run --rm --memory=512m --cpus=1 myappOptions
| Flag | Does |
|---|---|
| --rm | Remove the container when it exits |
| -d, --detach | Run in the background |
| -it | Interactive + TTY (combine -i and -t) |
| -p host:container | Publish a port |
| -v src:dst | Bind-mount or volume |
| -e KEY=val | Set an environment variable |
| --name | Assign a container name |
| -w | Working directory inside the container |
Common errors in CI
Passing -it in CI causes "the input device is not a TTY" because there is no terminal attached. Drop -t (use -i alone, or neither) in non-interactive pipelines. Another: "docker: Error response from daemon: ... port is already allocated" when a previous container still holds the port - give it a unique --name and run with --rm, or stop the old container first.