Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

curl only results in "Empty reply from server" #2522

Closed
muellermichel opened this issue Nov 1, 2013 · 46 comments
Closed

curl only results in "Empty reply from server" #2522

muellermichel opened this issue Nov 1, 2013 · 46 comments

Comments

@muellermichel
Copy link

I still haven't figured out docker networking.

  • docker 0.65
  • on Ubuntu 12.04
  • ufw on host: disabled
  • image based on 'ubuntu'
  • nc tests as in http://docs.docker.io/en/latest/use/basics/ work.
  • running the container in interactive mode, opening a server in the background and doing a curl on the localhost within the container, does work.

The problem:
Servers I start within the container are not reachable from the host. Curl on the host respond with "curl: (52) Empty reply from server". Note that this is different behavior than trying to connect to a port that hasn't been forwarded, which would result in "curl: (56) Recv failure: Connection reset by peer". I guess since the nc tests work, I can actually send from the outside, but the container cannot seem to send. It really looks like a firewall problem, but ufw is disabled on host and in the container it's not even installed.

Any ideas?

Edit: some example
docker ps shows

127.0.0.1:7000->5984/tcp 

on my container. Inside the container I can do:

curl localhost:5984
{"couchdb":"Welcome","uuid":"0248a6778bcfe29b2376b8c137efcb56","version":"1.4.0","vendor":{"version":"1.4.0","name":"The Apache Software Foundation"}}

Everything fine there. But from the host:

root@ubuntu:/home/muellermichel# curl -v http://localhost:7000
* About to connect() to localhost port 7000 (#0)
*   Trying 127.0.0.1... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:7000
> Accept: */*
> 
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server
* Closing connection #0
@muellermichel
Copy link
Author

Ok I'm an idiot. It was just a misconfiguration of the server to only listen on localhost. I simply never thought of it before because I always proxied with ssh -L to my servers.

@reiz
Copy link

reiz commented Jun 20, 2014

I had exactly the same issue and I was already banging my head. You just gave me the right hint to solve it. Thanks man!

@muellermichel
Copy link
Author

Glad I could help. Funny thing is, I don't even remember anymore what setting I was talking about here, it's a shame I didn't describe it better.

@reiz
Copy link

reiz commented Jun 22, 2014

It was good enough for me :)

@dbenque
Copy link

dbenque commented Aug 10, 2014

Thanks! I was getting crazy with the same issue...

For people playing with GO AppEngine inside container, specify the host when launching your server instance:

goapp serve -host=0.0.0.0 myapp/

If you don't specify -host=0.0.0.0 .you will not be able to query your server from outside the container, even with a good port mapping configuration at Docker level.

@ameuret
Copy link

ameuret commented Sep 26, 2014

+1. Got me too. 😆

@ianzapolsky
Copy link

@muellermichel @reiz @ameuret @dbenque could you please try to elaborate on the setting you are talking about? Running into the same problem but I can't figure it out.

@muellermichel
Copy link
Author

@ianzapolsky It's basically the same when you want to make couchdb accessible from outside your host, no matter whether it runs in a container or not: you need to bind it's address to 0.0.0.0:

[httpd]
bind_address = 0.0.0.0

@ianzapolsky
Copy link

cheers @muellermichel, thanks a lot. This solved my problem. For anyone stumbling upon this thread in the future, my specific issue was this: I am running a gunicorn server serving a Django app inside a docker container, exposing it to the host on port 8000 via EXPOSE 8000 in the Dockerfile and the command line argument -p 127.0.0.1:8000:8000 in the docker run command. Like the guys above, I was curling port 8000 from the host and getting an empty reply from server. The simple fix is to bind the address that gunicorn serves on to 0.0.0.0. So all I had to do was add -b 0.0.0.0:8000 to the gunicorn run command and everything worked beautifully.

@KirillGrishin
Copy link

@ianzapolsky Thanks for your comment. It helped me realise that the issue with empty reply from server was not the docker setup, but the actual application. In my case was trying to run sample Spray app but was getting this error. All I needed to do is to change localhost to 0.0.0.0 at this line https://github.com/spray/spray-template/blob/on_spray-can_1.3_scala-2.11/src/main/scala/com/example/Boot.scala#L20

For anyone who runs into this in future.

@nileshsutar
Copy link

@KirillGrishin Thanks for your valuable comment. My problem is also solved :).
but didn't got answer why we need to change localhost to 0.0.0.0?

@thaJeztah
Copy link
Member

why we need to change localhost to 0.0.0.0?

Basically, that tells the server to listen on all networks, not just to localhost connections, otherwise the server is only accessible from within the container (which is the local host for the server running there)

@nileshsutar
Copy link

Thank You @thaJeztah

jw3 referenced this issue in jw3/example-gpio-service Nov 8, 2015
@ghost
Copy link

ghost commented May 31, 2016

That's really helpful!

@lastcoolnameleft
Copy link

Encountered the same issue building my own ElasticSearch container. Glad to see I'm not the only one.

ernix added a commit to internship2016/django-uwsgi-nginx that referenced this issue Aug 5, 2016
テスト時はhostを0.0.0.0に設定すること

moby/moby#2522

    ./manage.py runserver 0.0.0.0:8000
@wenhoujx
Copy link

wenhoujx commented Nov 16, 2016

thanks, i had the same bind to localhost issue

@Hyvi
Copy link

Hyvi commented Nov 20, 2017

I love you all. the same issue trouble me a long day.

@bzamecnik
Copy link

It happened to me. The problem were two hosts with SSH forwarding to the same port on localhost. I was connecting to other host where jupyter was not running. 🤦‍♂️

@dahiyashish-portfolio
Copy link

screen shot 2018-05-16 at 4 38 38 pm

i had the issue with the same ... even i had bind ip with 0.0.0.0:port please help me in that

@davestimpert
Copy link

Had the same issue with a python flask app doing app.run on "localhost". Fixed by changing it to "0.0.0.0"

@bobf
Copy link

bobf commented Sep 29, 2018

@muellermichel Saved me a headache as well - thank you ! Glad to see we're not alone as idiots. : )

@alexperezau
Copy link

I had this error but it was due to my corporate proxy server. For some reason my VM was using the proxy server to resolve localhost?
Anyway, I ran curl like this to check my container flask app:
curl --noproxy 127.0.0.1 http://127.0.0.1:5000

@bvenkatr
Copy link

bvenkatr commented Nov 27, 2018

I also have the same problem, My nodejs server is running in a docker container, it has exposed some REST endpoints, but when I make a curl request to those REST endpoints, I got below error

curl http://127.0.0.1:3001/ping
curl: (52) Empty reply from server

Solution:-
Change localhost to 0.0.0.0 in below line

host: process.env.HOST || 'localhost' => host: process.env.HOST || '0.0.0.0'
const application = require('./dist');

module.exports = application;

if (require.main === module) {
  // Run the application
  const config = {
    rest: {
      port: +process.env.PORT || 3000,
      host: process.env.HOST || '0.0.0.0',
      openApiSpec: {
        // useful when used with OASGraph to locate your application
        setServersFromRequest: true,
      },
    },
  };
  application.main(config).catch(err => {
    console.error('Cannot start the application.', err);
    process.exit(1);
  });
}

@iamrenejr
Copy link

I'm a bit lucky that I've only been banging my head for an hour or so before I found this. Of course, the app needed to be bound to 0.0.0.0 and not 127.0.0.1. I'd already solved this problem before but the configurations on this project were not done by me. This thread gave me the answer I needed to not go insane. :D

@ameyxd
Copy link

ameyxd commented Jul 8, 2019

I did the same thing. I'm running a Flask app and all I had to do was add host='0.0.0.0' to the app.run() as a parameter for it to work properly again.

@hoanghiep1x0
Copy link

I got the same error I had to change the configuration file in the project and rebuild the image.

Before that when not in project nodejs

host: process.env.HOST || '0.0.0.0',
port: process.env.POST || 8600

https://imgur.com/0CT5i6R

After having it my application worked

@bzani
Copy link

bzani commented Nov 22, 2019

I'm a bit lucky that I've only been banging my head for an hour or so before I found this. Of course, the app needed to be bound to 0.0.0.0 and not 127.0.0.1. I'd already solved this problem before but the configurations on this project were not done by me. This thread gave me the answer I needed to not go insane. :D

exactly the same situation here lol

@JayjeetAtGithub
Copy link

Same problem faced and fixed. Thanks to @muellermichel. Listening on 0.0.0.0 instead of localhost solved it for me.

@mchikyt3
Copy link

Thank you very much!!!!

@juliofalbo
Copy link

juliofalbo commented Apr 28, 2020

The same for Spring Boot applications, you need to change the property server.address to server.address=0.0.0.0

@aashitvyas
Copy link

This helped me today as I was trying to spin up go app server in a docker container. I have to change fromlocalhost to 0.0.0.0 so that it can listen to all the networks. 👍

@tlonist-sang
Copy link

So the point is making elasticsearch server listen to
0.0.0.0'? Did I get it right? If so, I guess I should change elasticsearch configuration yml file and set network.host = '0.0.0.0'?

@jonatan2m
Copy link

I got this issue for the title but my problem wasn't totally related to that.
I had a scenario which I have two apps, a server and a client API, using Flask (python).
My Flask config file wasn't getting the correct port that I set on the terminal.
export APP_PORT=5002; docker-compose up -d

As I'm using docker-compose, I need to set this variable on the environment 's service section:

version: "3"
services:
    web:
        build: "./py-client"
        ports:
            - "5002:${APP_PORT}"
        environment: 
            - APP_PORT=${APP_PORT}
    api:
        build: "./py-api"
        ports:
            - "5000:5000"

@michaelpulis
Copy link

If I am not able to change the address from 127.0.0.1 to 0.0.0.0, is there another workaround ?

@gavinnhh
Copy link

What if I can't change from localhost to 0.0.0.0? My app needs google sign in, and inside google client services I can't put http://0.0.0.0:8080/auth/ as authorized redirect URIs because it is invalid somehow.....

@michaelpulis
Copy link

@Gavin-Hoang I posted my solution on stack overflow linked here. I basically ended up using port forwarding to solve the issue

@gavinnhh
Copy link

@michaelpulis Thank you very much. Will try it!

@SohaibMoinuddin
Copy link

Hopefully this helps someone, this post made me find my mistake:

Make sure the PORT you're running the app on matches the one specified in the docker run command!

In my case, my node express app was running on port 5000 but I ran the command docker run -p 49160:8080 -d <container-name>. Switched 8080 to 5000 and no issues!

@Tan-CM
Copy link

Tan-CM commented Sep 23, 2022

Almost gone insane with this problem until I found this thread. Work before containerisation with localhost 127.0.0.1 but with containerization, the IP (for hostPort) for http.ListenAndServe(hostPort, router) has to be 0.0.0.0, so as to accept any IP.

@tonginbox
Copy link

This problem's classic, binding in docker localhost:5000 with docker run -p 5000:5000, then cannot access the port by my machine using curl http://localhost:5000 and I got Empty reply from server. After couple hours, then i founded this thread then i cried.
I surely now remember always bind 0.0.0.0.

@txwdzxq
Copy link

txwdzxq commented Mar 25, 2023

is worked

cherrythia added a commit to NUS-EVCHARGE/ev-user-service that referenced this issue Aug 24, 2023
@rohanailoni
Copy link

+1 this saved me a lot to time

@bdzyubak
Copy link

Thank you so much, guys! The combination of the following solved it for me:

ENV GUNICORN_CMD_ARGS="--timeout 60 -k gevent -b 0.0.0.0:5001"
EXPOSE 5001
ENV SERVER_PORT 8000
ENV SERVER_HOST 0.0.0.0

@AntonyZhang726
Copy link

I did the same thing. I'm running a Flask app and all I had to do was add host='0.0.0.0' to the app.run() as a parameter for it to work properly again.

Thank you very much. you pointed out how to change it when using Flask
image

@fernandosesma
Copy link

fernandosesma commented May 20, 2024

For anyone else who may be stuck on this issue and is running a Spring Boot application. I found that I had a misconfiguration where I set my Tomcat port to be 8081.

image

Which was also the same port I configured Docker to forward traffic to.

image

In this configuration, Docker will forward traffic received on port 8081 of the host to port 8080 of the container. Since the application is not listening on port 8080 inside the Docker container, the connection is closed, and you get a "socket hang up" error. So in this case, I needed to set SERVER_PORT to 8080 for everything to work.

@ks982579
Copy link

I would like to thank everyone for this post, as I wasted a couple hours debugging containerizing my actix-web application. I am very very surprised Google Gemini wasn't able to diagnose this issue at all. For anyone else trying actix-web, I did this:

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(anything))
            .route("/health-check", web::get().to(health_check))
    })
    // .bind(("127.0.0.1", 8080))?
    .bind(("0.0.0.0", 8000))?
    .run()
    .await
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests