Hey everyone, I'm new to Docker and noticed that a lot of projects like to run on ports 8000 or 8080. I'm curious—why don't developers just choose a random unused port instead? Also, is there a way to run multiple things on the same port? I have a feeling that's not possible, but if it is, are there any tools like a doorman that can redirect traffic saying 'hey, that port is in use, try this one instead'?
4 Answers
Honestly, there’s nothing wrong with having every service run on 8080. It keeps things simple. Just be sure to change the host port in your `docker-compose.yml` file if there's a conflict. For example, in your compose file, you might have `ports: - "8080:8080"`, and if 8080 is in use, switch it to `8081:8080`. It usually works just fine!
Good question! Using 8080 is just a standard practice since it's unprivileged and commonly set up for web applications. If you find port 8080 is occupied, Docker lets you map ports however you want, like using `-p 8081:8080` to access it. Alternatively, MACvlan can assign each container its unique IP, letting you use whichever ports you want, but that approach can add complexity. Just remember, it’s all about mapping the ports correctly for access!
Don't stress too much about the internal port a container uses. The outside port you map it to is what matters. You can totally choose different ports for each container. If you're working with dynamic containers, you can even create a small script to automatically assign a random available port. But in most cases, everyone just sticks to the common ports for simplicity since it keeps things predictable as you clone or scale projects.
It's cool to see you diving into Docker! The reason many folks use ports 8000 and 8080 is due to the conventions in web development. Port 80 is for HTTP traffic, but since ports below 1024 need root access in Linux, 8000 and 8080 serve as convenient alternatives. If you're trying to run multiple services, you can look into using a reverse proxy like NGINX or Traefik. They listen on one port and can direct traffic to the right service based on the URL paths or subdomains, essentially acting like that doorman you mentioned!
Oh wow, I had no idea about the reverse proxy function. That makes a lot of sense!
Thanks! That makes it easier to understand how to troubleshoot port usage!