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

Couldn't resolve host name #1884

Closed
azchatlanin opened this issue Jul 19, 2024 · 2 comments
Closed

Couldn't resolve host name #1884

azchatlanin opened this issue Jul 19, 2024 · 2 comments

Comments

@azchatlanin
Copy link

azchatlanin commented Jul 19, 2024

There are two docker containers. Both are on the same network.
When I make a post request from the first to the second, everything is fine. But if the second container is disabled, the first one crashed during the execution of the post request. (exited with code 0)

When I do the same thing, but using curl. curl throw an exception:

I would like to be able to intercept this exception, and not completely crushed the docker.

services:
  service_1:
    image: image
    container_name: service_1
    environment:
     - USER_UID=1000
     - USER_GID=1000
       #restart: always
    networks:
      - service_net

networks:
  service_net:
    external: true
// base request with httplib
httplib::Client m_cli { "service_2:5000" };


httplib::Headers headers = {
  { "Content-Type", "application/json" }
};

try
{
   auto r = m_cli.Post("/", headers, payload.dump(), "application/json" );
   log(r->status);
}
catch(...)
{
   log("error");
}
// dont catch error
// docker container exited with code 0
// base curl
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if (curl) {
  curl_easy_setopt(curl, CURLOPT_URL, "service_2:5000");
      
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Content-Type: application/json");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "param1=value1&param2=value2");

  res = curl_easy_perform(curl);
  if (res != CURLE_OK)                        <--- here i can catch error. docker container dont exited
  { 
    std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
  }

  curl_easy_cleanup(curl);
  curl_slist_free_all(headers);
}
@yhirose
Copy link
Owner

yhirose commented Aug 30, 2024

@azchatlanin sorry for the late replay. Could you show me the smallest example (server code and client code, and Dockerfile files for each, and docker compose yaml) to reproduce this problem? Then, I'll take a look at it. Thanks!

@yhirose
Copy link
Owner

yhirose commented Aug 31, 2024

@azchatlanin I made to test code to reproduce it on my machine, but I am not able.

You can test in [this branch] with the following command.

$ docker compose -f docker-compose-issue1884.yml up
...
[+] Running 1/0
 ✔ Container cpp-httplib-client-1  Created                                                           0.0s
Attaching to client-1
client-1  | ### cpp-httplib client test...
client-1  | Could not establish connection (2)
client-1 exited with code 0

I'll close it for now since I am not able to reproduce it. Thanks!

@yhirose yhirose closed this as completed Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants