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

2

3 Answers

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 you should try to hit . 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:

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 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 to visit express container.

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

4
 **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."

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like