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

2.1K
Vistas
How to connect frontend to backend via docker-compose networks

I'm trying to dockerize my angular + express application. I have a docker-compose file that creates the two containers, and I am able to hit the containers from my host machine(using my browser), but I'll just get a "ERR_NAME_NOT_RESOLVED" whenever I try to hit the backend from http requests made by my frontend.

I've looked up the issue, and it seems like most suggest that the service name and container port should be enough to hit the the other container when they're on the same network. I've tried to hit "http://express:8000/user?user=030f0e70-9a8f-11e9-b5d1-f5cb6c0f3616" which I think should work given what I've seen from other places, but regardless, I get the same error.

My docker-compose file looks like

version: '3' # specify docker-compose version

# Define the services/containers to be run
services:
  angular: # name of the first service
    build: ./ # specify the directory of the Dockerfile
    ports:
      - "4200:80" # specify port forewarding
    links:
      - "express"
    depends_on:
      - "express"

  express: #name of the second service
    build:  # specify the directory of the Dockerfile
      context: ./
      dockerfile: dockerfile.be
    ports:
      - "8000:8000" #specify ports forewarding
    expose:
      - "8000"

Ideally, I'd like my frontend to be able to hit the other container with a set endpoint, so I could deploy the application with minimal changes. I'd appreciate any advice. I feel like I'm missing something really simple, but after a few hours of tinkering, I still haven't caught it.

Thanks!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

A few things:

  1. By using expose, you are making the container's published ports only available to linked/networked services. This is one reason why you are unable to access it locally.
  2. Instead of hitting http://express:8000/ you should try to hit http://localhost:8000. The service is being published to your localhost system and is not being served by anything by default (e.g., IIS, NGINX).
  3. Add a custom defined network in your compose file instead of using links. This is now the main way to network containers together:

    version: '3' # specify docker-compose version
    
    services:
      angular: # name of the first service
        build: ./ # specify the directory of the Dockerfile
        ports:
          - "4200:80" # maps port 4200 on localhost to 80 in container
        network:
          - mynetwork
        depends_on:
          - "express"
    
      express: # name of the second service
        build:  # specify the directory of the Dockerfile
          context: ./
          dockerfile: dockerfile.be
        ports:
          - "8000:8000" # maps port 8000 on localhost to 8000 in container
        networks:
          - mynetwork
    
    networks:
      mynetwork:

    2: https://docs.docker.com/compose/compose-file/#expose

over 4 years ago · Santiago Trujillo Denunciar

0

In fact your traffic is as next:

  1. User browser request page from angular container, then all pages will rendered to user's browser.

  2. The front javascript code using angular HttpClient to fetch the data from express container.

    At that time, although docker-compose setup a customized network for you which afford auto-dns to resolve angular & express, but this dns just works among containers, not effect for host.

    But, your augular HttpClient which call http://express was happened on user's browser which not in container, so the auto-dns defintly not work for you to resolve the name express.

For you, if you just want to open browser from your docker host, you can use localhost, but if you also want the user from other machine to visit your web, you had to use the ip of your dockerhost.

Then, in angular HttpClient you need to use something like http://your_dockerhost_ip:8000 to visit express container.

If interested, you can visit this to see User-defined bridges provide automatic DNS resolution between containers.

over 4 years ago · Santiago Trujillo Denunciar

0

    **sample docker-compose.yaml**
      version: '3.5'
      services:     
        angular:
          image: "angular-alpine:0.0.1"
          container_name: angular 
          tty: true
          stdin_open: true
          networks:
            app_net:
            ipv4_address: 172.16.238.05
          depends_on:
            - express

       express:
         image: "express:0.0.1"
         container_name: express
         tty: true
         stdin_open: true
         networks:
           app_net:
           ipv4_address: 172.16.238.10

      networks:
        app_net:
          driver: bridge
        ipam:
        driver: default
        config:
          - subnet: 172.16.238.0/24

>"1. Angular will run on 172.16.238.05:4200 and express will run on 172.16.238.10:some-port. 2. modify your config.ts or parameter.ts or anyfile where you configure express url into 172.16.238.10:some-port. now your angular will connect to express."

over 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