Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why Connection Refused error on simple Docker + nginx configuration?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading