6

Trying to wrap my head around all these Docker tutorials, and there is really no explanation around what port 80 is all about. Just, "bind to port 80".

This is the 3rd Docker tutorial I've taken with the same error after running the sample Dockerfile:

Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

So, I've understood that port 80 is basically the default port, which would allow my app to run at example.com instead of example.com:80 - for example. My web server, and local machine complain that this port is in use. Of course it is, it's in use by default.

So, why are all these Docker tutorials binding to port 80? I bet they are doing it right, and I am missing something... but, cannot find a clear solution or description.

Here is the tutorial I'm doing: Digital Ocean's Install WordPress with Docker: https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-docker-compose

Sure enough, port 80 fails for me:

webserver:
  depends_on:
    - wordpress
  image: nginx:1.15.12-alpine
  container_name: webserver
  restart: unless-stopped
  ports:
    - "80:80"
  volumes:
    - wordpress:/var/www/html
    - ./nginx-conf:/etc/nginx/conf.d
    - certbot-etc:/etc/letsencrypt
  networks:
    - app-network

Changing this to throws no error, but this means we can only resolve http://example.com:90 -

ports:
  - "90:80"

What am I missing here? Why are all of these definitions of port 80 failing locally on my Mac and on a remote Digital Ocean Ubuntu8.1 server?

lakewood
  • 607
  • 1
  • 5
  • 21
  • 1
    Two potential problems: 1. ports below 1024 are privileged and can only be used by root 2.Port 80 is in use. In either case, try to map it as `8080:80` – Stavr00 Sep 25 '19 at 18:23
  • @Stavr00, thanks for this. Are you able to confirm this binding? Are we saying here, bind port 8080 to port 80, or, vice versa? When I make your change, I can only hit the url with `http://ipaddress:8080`. Conceptually, if I am running a live web server that has nginx installed and is actively serving pages 'the old way' - a VM/app/nginx config... is this what's happening? Fundamentally, would this routing only work on a box with no web server running on the default port 80? As in, would you suggest a second server with no nginx apps running? – lakewood Sep 25 '19 at 18:51

3 Answers3

6

Do you have something else running on port 80? You can try curl localhost:80 or lsof -i :80; you might have Apache or something else running there by default that you'd need to kill.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • There are a number of nginx lines in the output of `lsof -i :80`, and an "Empty reply from the server" with the `curl` command. This is fundamentally what I don't understand about containerization. Do I basically need a clean, brand new server that has no web applications running to get started with Docker containers? Is Docker not suitable on a VM that hosts non-containerized web apps on nginx? – lakewood Sep 25 '19 at 18:41
  • 2
    No, not necessarily. If you want to keep Nginx running, I would expose your container on a different port, and then use Nginx to route to it on the appropriate hostname. As in your example, if you have `ports: - "8080:80"` or something, you can have a block like ``` server { listen 80; server_name something.com; location / { proxy_pass http://127.0.0.1:8080; } } ``` (excuse the formatting, no multi-lines in comments) – Zac Anger Sep 25 '19 at 19:04
2

If you're using a mac like me, sudo apachectl stop helped resolve this for me. Macs have a built-in web server, and mine was running by default. Maybe due to some defaulted websharing feature on the macbook pro.

jvschoen
  • 21
  • 1
0

example.com and example.com:80 are same thing btw. Here, some application in your host is already listening to port 80, it has got nothing to do with the container. Possibly, you are running an nginx server in the host as well. Are you ?

Partha
  • 814
  • 6
  • 13
  • Yes. This is what is happening. The answer above about configuring the server block above is correct. – lakewood Sep 25 '19 at 20:13