docker-nginx: Port 80 and Port 443 not accessible when published via `ports`

Hi,

I’ve been trying unsuccessfully to redirect calls to http://localhost and https://localhost to a docker container by using nginx listening on port 80 and 443 inside its own docker container, with ports published via docker-compose.yml, i.e:

        image: nginx:alpine
        ports:
            - 127.0.0.1:80:80
            - 127.0.0.1:443:443

I cannot get this to work. I get connection refused. When I change the ports (on the host) to anything else (441, 81, 8443, 8080, …), everything works just fine. I’m not positive this is an nginx issue, but considering nginx image exposes port 80/443, I was wondering if there is some conflict.

I’ve checked to see that port 80 and 443 are not in use, and they aren’t, except for docker itself because of the above docker-compose.yml config. I’ve also checked pf filter rules – nothing in there that would forward traffic on port 80/443 to some other ip:port.

Any ideas?

ETA:

I’m on macOS Sierra. I’m using docker 1.12.5, docker-compose 1.9.0, docker-machine 0.8.2.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 10
  • Comments: 18

Most upvoted comments

same here.

@ian-axelrod Just to be sure, you did try:

        ports:
            - 0.0.0.0:80:80
            - 0.0.0.0:443:443

correct? This works for me.

same situation here with

docker-machine version 0.10.0
docker-compose version 1.11.2
Docker version 17.03.1-ce

This is driving me crazy.

I went as far as tcpdumping the interface, but the log doesn’t really help. basically there’s an initial burst of traffic to localhost:http and some randomly allocated port, but then silence. With port 81 (xfer), there’s that same burst of traffic, but then a flurry of new requests between localhost:xfer and a few randomly assigned ports (sockets). Basically tcp handshakes then sequence increments. Happens with safari, too. Makes me think there’s something in the kernel itself that is causing this, or something else very low level even though system logs suggest nothing is amiss.

This is very frustrating.

It appears to happen for /any/ docker container that has a service listening on port 80 (host), not only nginx, so I suppose that this ticket should be transferred to docker for mac.

Yes, I’m sure.

9b906f5a1422  nginx:alpine  "nginx -g 'daemon off" 18 minutes ago  Up 18 minutes 127.0.0.1:80->80/tcp, 127.0.0.1:443->443/tcp composer_web_1

Forgot to say I’m on docker for mac, so no virtualbox / docker-machine.

Also, I’ve tried having it listen on 0.0.0.0 instead of localhost. That didn’t help.

Works fine here:

thresh@fruity ~/tmp/dock $ cat docker-compose.yml
version: '3'
services:
    web:
        image: nginx:alpine
        ports:
            - 127.0.0.1:80:80
            - 127.0.0.1:443:443
thresh@fruity ~/tmp/dock $ docker-compose up
Creating network "dock_default" with the default driver
Pulling web (nginx:alpine)...
alpine: Pulling from library/nginx
128191993b8a: Pull complete
655cae3ea06e: Pull complete
dbc72c3fd216: Pull complete
f391a4589e37: Pull complete
Digest: sha256:34aa80bb22c79235d466ccbbfa3659ff815100ed21eddb1543c6847292010c4d
Status: Downloaded newer image for nginx:alpine
Creating dock_web_1 ...
Creating dock_web_1 ... done
Attaching to dock_web_1
web_1  | 172.18.0.1 - - [21/Dec/2017:11:03:57 +0000] "HEAD / HTTP/1.0" 200 0 "-" "-" "-"

(while in the other terminal):

thresh@fruity ~ $ telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
HEAD / HTTP/1.0

HTTP/1.1 200 OK
Server: nginx/1.13.7
Date: Thu, 21 Dec 2017 11:03:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 01 Dec 2017 21:45:50 GMT
Connection: close
ETag: "5a21cd8e-264"
Accept-Ranges: bytes

Connection closed by foreign host.

Versions:

thresh@fruity ~ $ docker --version
Docker version 17.09.1-ce, build 19e2cf6
thresh@fruity ~ $ docker-compose --version
docker-compose version 1.17.1, build 6d101fb
thresh@fruity ~ $ docker-machine --version
docker-machine version 0.13.0, build 9ba6da9