Home > database >  why Connection Refused error on simple Docker nginx configuration?
why Connection Refused error on simple Docker nginx configuration?

Time:04-10

I have this Dockerfile:

FROM nginx:latest
COPY devops/nginx_proxy.conf /etc/nginx/conf.d/default.conf
EXPOSE 8080

and a devops/nginx_proxy.conf:

server {
  listen 8080;
  client_max_body_size        32M;
  underscores_in_headers      on;
}

Running the Dockerfile with docker run -p 8080:80 test and then testing with curl http://localhost/, I see this error:

curl: (7) Failed to connect to localhost port 80: Connection refused

Even more curious, curl http://localhost:8080/ returns this:

curl: (52) Empty reply from server

Why am I getting these errors?

CodePudding user response:

With Docker you can bind containers ports to host ports using the -p option.

General rule

docker run -p HOST_PORT:CONTAINER_PORT

Bind container 8080 port to the 80 of the host

docker run -p 80:8080 test

Ports which are not bound to the host (i.e., -p 80:80 instead of -p 127.0.0.1:80:80) are accessible from the outside

Bind the port limiting the access to localhost

docker run -p 127.0.0.1:80:8080 test
  • Related