-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Unable to talk to docker daemon through sock file #2021
Comments
@bialasjaroslaw thanks for the report. What is the request line of the curl request? You can check with Also what HTTP status code will be returned with |
@yhirose thanks for response. Here are the responses for curl for both with and without localhost curl --unix-socket /run/docker.sock /v1.39/images/json -v
* URL rejected: No host part in the URL
* closing connection #-1
curl: (3) URL rejected: No host part in the URL curl --unix-socket /run/docker.sock localhost/v1.39/images/json -v
* Trying /run/docker.sock:0...
* Connected to localhost (/run/docker.sock) port 0
> GET /v1.39/images/json HTTP/1.1
> Host: localhost
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Api-Version: 1.46
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/27.1.2 (linux)
< Date: Fri, 17 Jan 2025 07:53:32 GMT
< Transfer-Encoding: chunked
<
... Running c++ app with HTTP Error: 400 I tried various things and I could omit curl --unix-socket /run/docker.sock v1.39/images/json -v
* Trying /run/docker.sock:0...
* Connected to v1.39 (/run/docker.sock) port 0
> GET /images/json HTTP/1.1
> Host: v1.39
> User-Agent: curl/8.9.1
> Accept: */*
>
... I can even type whatever I want for host. The only requirement, that seems to be necessary, is to have something hostlike before curl --unix-socket /run/docker.sock whatever/v1.39/images/json -v
* Trying /run/docker.sock:0...
* Connected to whatever (/run/docker.sock) port 0
> GET /v1.39/images/json HTTP/1.1
> Host: whatever
> User-Agent: curl/8.9.1
> Accept: */*
>
... I tried the same things with raw sockets and it is not working the same way curl does (protocol is required) # These 2 are not working
"GET localhost/v1.39/images/json HTTP/1.1\r\n"
"GET v1.39/images/json HTTP/1.1\r\n"
# This one still works as expected
"GET http://whatever/images/json HTTP/1.1\r\n" Result from first two examples HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close
400 Bad Request Result from last example HTTP/1.1 200 OK
Api-Version: 1.46
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/27.1.2 (linux)
Date: Fri, 17 Jan 2025 08:13:35 GMT
Connection: close
Transfer-Encoding: chunked
... This is so confusing because I thought that there is some kind of host validation on the docker side. |
Thanks. How about |
Also if you call |
Also what if the following change in your code? const char* data = "GET /v1.39/images/json HTTP/1.1\r\n"
"Host: localhost\r\n"
"Connection: close\r\n\r\n"; |
One more. 😄 How about |
@yhirose thank you for response and all of your suggestions. curl --unix-socket /run/docker.sock http://localhost/v1.39/images/json -v
* Trying /run/docker.sock:0...
* Connected to localhost (/run/docker.sock) port 0
> GET /v1.39/images/json HTTP/1.1
> Host: localhost
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Api-Version: 1.46
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/27.1.2 (linux)
< Date: Fri, 17 Jan 2025 18:20:20 GMT
< Transfer-Encoding: chunked
< curl --unix-socket /run/docker.sock whatever/v1.39/images/json -v
* Trying /run/docker.sock:0...
* Connected to whatever (/run/docker.sock) port 0
> GET /v1.39/images/json HTTP/1.1
> Host: whatever
> User-Agent: curl/8.9.1
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 200 OK
< Api-Version: 1.46
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/27.1.2 (linux)
< Date: Fri, 17 Jan 2025 18:21:18 GMT
< Transfer-Encoding: chunked
< C++ code with header HTTP/1.1 200 OK
Api-Version: 1.46
Content-Type: application/json
Docker-Experimental: false
Ostype: linux
Server: Docker/27.1.2 (linux)
Date: Fri, 17 Jan 2025 18:22:01 GMT
Connection: close
Transfer-Encoding: chunked curl --unix-socket /run/docker.sock /v1.39/images/json -H 'Host: localhost' -v
* URL rejected: No host part in the URL
* closing connection #-1
curl: (3) URL rejected: No host part in the URL Solution: httplib::Client cli("/run/docker.sock");
cli.set_address_family(AF_UNIX);
httplib::Headers headers{
{"Host", "localhost"},
{"Connection", "close"},
};
auto res = cli.Get("/images/json", headers); This is it. Host header make it work. |
Fantastic! I'll put 'information' tag, so that others could benefit from the solution. Thanks! |
I was trying to use cpp-httplib to communicate with my local docker daemon using sock file. There is a possibility to do that according to this
After testing my connection with curl I was pretty sure that it will be easy to do however problem (IMO) is caused by the fact that docker engine is expecting to receive request on endpoint
http://localhost/v1.39/images/json
and not/v1.39/images/json
. I can not see how to do that usinghttplib::Client
object because its only argument is a path to sock file.Also curl is working with that socket
curl --unix-socket /run/docker.sock /v1.39/images/json
I am attaching a short example comparing httplib and raw socket usage
g++ example.cpp && ./a.out
g++ example.cpp -DHTTP_LIB && ./a.out
I am using httplib 0.18.3 from vcpkg, but I also tried with trunk version. My GCC is 14.2.1 and Docker 27.1.2
The text was updated successfully, but these errors were encountered: