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!
23 Answers
A few things:
- 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.
- 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).
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:
User browser request page from
angular container, then all pages will rendered to user's browser.The front javascript code using
angular HttpClientto fetch the data fromexpress container.At that time, although
docker-composesetup a customized network for you which afford auto-dns to resolveangular&express, but this dns just works among containers, not effect for host.But, your
augular HttpClientwhich callwas happened on user's browser which not in container, so the auto-dns defintly not work for you to resolve the nameexpress.
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.
**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."