FresherTechnicalDocker
What do the flags in docker run -d -p 8080:80 --name web nginx do?
Try answering first
Model answer
The command starts a container from the nginx image and runs it in the background with a published port and a fixed name.
-druns the container detached, in the background, and prints its id. Without it, the container runs in the foreground and holds your terminal.-p 8080:80publishes a port in the formhost:container. Traffic to port 8080 on the host is forwarded to port 80 inside the container, where nginx listens.--name webgives the container a readable name, instead of a random one, so you can rundocker logs webordocker stop web.nginxis the image. Without a tag it meansnginx:latest, and Docker pulls it first if it is not local.
docker run -d -p 8080:80 --name web nginx
curl http://localhost:8080
docker logs web
docker rm -f web
By default the port is bound on all interfaces. Use -p 127.0.0.1:8080:80 to expose it only to the local machine.
The left number is always the host, and the right one is the container.
Key points
- -d runs the container in the background
- -p host:container publishes a port
- --name sets a readable container name
- No tag means latest, and the image is pulled if missing
Likely follow-ups
- How would you publish the port only on localhost?
Common mistakes
- Swapping the host and container ports in -p
- Running without --name and then guessing the generated name