Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
HttpClient request on networked docker container

I am new to docker containers and Im running into the following issue. I am developing a web page that makes requests to an exposed api. Both of them are published on different containers that are linked through a network. This is my docker-compose.yml file:

version: '2'

services:
  api:
    image: my/api
    networks:
      - api
    ports:
      - "8080:5000"    
    container_name: api  

  web:
    build:
      context: ./DockerWeb      
    networks:
      - api      
    ports:
      - "80:5000"    
    container_name: web

networks:    
  api:
    driver: bridge

I have noticed that if I attach to the container running my webpage I can ping the api container without any issue using ping api

The issue comes when trying to make any web service call to the api container. I am trying to initialize an HttpClient object, however the BaseAddress property of such an object must be set to a valid Uri.

_client = new HttpClient();
_client.BaseAddress = new Uri(config.ApiUrl); //config.ApiUrl = "api"

This throws the following error

UriFormatException: Invalid URI: The format of the URI could not be determined.

Is there a way to either a) Expose the container name in a different way, or b) make a call to a web service without the need of using a URI? How can I accomplish this?

UPDATE

I have updated the ApiUrl to contain the value http:api:8080, however now I am getting an exception when performing the call

System.AggregateException: One or more errors occurred. (An error occurred while sending the request.) ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.CurlException: Couldn't connect to server

I have also tried the calling the api endpoint directly from my web container using wget

wget http://api:8080/ 

However I still receive back an error:

Connecting to api (api)|172.24.0.4|:8080... failed: Connection refused

Could this be an API configuration error?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

api is just the hostname. The URI would be http://api:8080 (or https://api:someotherport if you set up SSL/TLS.)

about 4 years ago · Santiago Trujillo Relatório

0

Here there is 2 kinds of communication:

Container web > Container api

In this case, indeed, you can just use the hostname "api" to connect to your api. This is what you would have to do if your html is generated server side.

Web Client > Container api

Here however I suppose that you are using some kind of JavaScript web application (Angular or other). In that case the html (and the call) is not made from the web container but directly from your web browser.

So in that case you have to use http://localhost:8080 (I suppose that Docker is running on your local machine).

I hope it helps

about 4 years ago · Santiago Trujillo Relatório

0

The internal port is used for connections inside the docker network. So in this case it is the request needs to go to http://api:5000

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda