What does docker run -d -p 8080:80 --name web nginx do?
It starts a container named web from the nginx image in the background (-d) and publishes container port 80 on host port 8080, so http://localhost:8080 reaches nginx.
Explain it simply
Think of a restaurant order slip. The image is the dish, the flags are your instructions: 'pack it to go, call it web, and serve it at counter 8080 even though the kitchen calls that counter 80'.
docker run is really two steps in one: create a container from the image (pulling the image first if it is not on your machine) and start it. -d means detached, so your terminal is returned immediately instead of showing nginx logs. --name gives a friendly name instead of a random one like quirky_turing. -p host:container maps a port on your machine to a port inside the container. Without -p, the container is reachable only from other containers on the same network.
Example
docker run -d -p 8080:80 --name web nginx
docker ps # web is listed, PORTS shows 0.0.0.0:8080->80/tcp
curl http://localhost:8080 # returns the nginx welcome page
docker stop web && docker rm webWhat to say in the interview
"docker run creates and starts a container from an image, pulling it if needed. -d runs it in the background, --name sets its name, and -p 8080:80 publishes container port 80 on host port 8080. The order is host port first, container port second."
Common mistakes
- Writing -p 80:8080 the wrong way round: host port comes first.
- Running the same command twice and hitting a 'name already in use' error because the first container still exists.