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

2.1K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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