What does docker run -p 8080:8080 mean?
Fast recall
Quick revision
Core answer
docker run -p 8080:8080 IMAGE publishes container TCP port 8080 on host port 8080. Without a host IP, Docker commonly binds the published port on all host addresses, not only localhost. Use -p 127.0.0.1:8080:8080 when the service should be reachable only from the host.Interview-ready guide
Interview answer
The -p flag maps ports between your host machine and the container.
The format is -p followed by host port, colon, container port. So -p 8080:8080 means when you hit localhost:8080 on your machine, Docker forwards that traffic to port 8080 inside the container. The reason you need this is that containers run in isolated networks — each container has its own private localhost.
When your Spring Boot app starts in a container and listens on 8080, that's port 8080 in the container's network, not your machine's network. Without the -p flag, you can't reach it even though the container is running.
A common pattern I use is -p 9090:8080 when I'm running multiple services and port 8080 is already taken by something else. The container still listens on 8080 internally, but I access it through 9090 on my host. That's really useful in microservices development.
You can also map multiple ports with multiple -p flags, like -p 8080:8080 -p 5005:5005 if you want to attach a debugger to your containerized app. One mistake I see freshers make is forgetting the -p flag completely, then wondering why localhost:8080 doesn't work. The container is actually fine and listening, but it's in a separate network namespace.
Another mistake is reversing the order — the host port always comes first, then the container port. It helps to think of it as traffic flowing from left to right, from your machine into the container.
What does docker run -p 8080:8080 mean
Direct Answer
docker run -p 8080:8080 IMAGE publishes container TCP port 8080 on host port 8080. Without a host IP, Docker commonly binds the published port on all host addresses, not only localhost. Use -p 127.0.0.1:8080:8080 when the service should be reachable only from the host.
Deep Dive & Architecture
A process in the container listens on a container port. Publishing creates a host-side listener and a forwarding rule to that container endpoint. In -p 8080:8080, the first 8080 is the host port and the second is the container port. They do not need to match: -p 8081:8080 serves the same container on host port 8081.
The missing host address is important. Docker normally publishes on all host addresses, so saying 'localhost only' can expose a development service farther than intended. Use -p 127.0.0.1:8080:8080 for loopback-only access.
Publishing is different from Dockerfile EXPOSE, which supplies metadata and supports -P but does not itself create a host mapping. Also verify that the application listens on the container interface and expected port.
Code Example
// View all logs from container
docker logs order-api
// Follow logs in real time (essential during development)
docker logs -f order-api
// See only last 50 lines (avoid scrolling through startup messages)
docker logs --tail 50 order-api
// View logs from last 10 minutes
docker logs --since 10m order-api
// Combine flags for targeted debugging
docker logs --since 5m --tail 100 -f order-api
// Filter logs with grep for specific errors
docker logs order-api | grep ERROR
// View logs with timestamps
docker logs -t order-api
// Save logs to file for analysis
docker logs order-api > order-api-logs.txt
Common Mistakes & Pitfalls
- Forgetting the -p flag entirely: Freshers run docker run without port mapping, see the container running with docker ps, then spend an hour debugging why localhost:8080 doesn't work. The container is fine — it's listening on 8080 inside its network — but that network is isolated from your host.
- Reversing the port order: Using -p 8080:9090 when you meant -p 9090:8080 leads to confusing errors. The pattern is always host first, container second. Remember the traffic flows from left to right: from your machine into the container.
- Not checking for port conflicts: Running docker run -p 8080:8080 when another container or local process already uses 8080 fails with a bind error. Use docker ps to see what's using ports, or use a different host port like 9090.
Check your understanding
Answer these without looking back at the article.
- What are the essential Docker commands every developer should know?
- How do you view logs from a running Docker container?
- How do you clean up Docker resources (stopped containers, unused images)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.