Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

289
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda