Pudding
Pudding

Reputation: 11

Is it possible for webservice to read apiservice via container_name on docker?e.g. http://container_name:port

I'm new to docker and I composed a web service and other services used in the web in a docker-compose file. I wonder if it's possible to have access to the services(e.g. api service) for the web service via container_name. like http://container_name:8080. Container_name is specified in docker-compose file, and web service on docker containers can read other service via http://localhost:port. I want to replace localhost to container_name, can docker do this mapping via some configuration? I tried depends_on and links and none of them work.

Part of my docker-compose.yml:

version: "3.7"
services:
 mywebservice:
   container_name: mywebservice
   ports:
    - "8080:80"
   depends_on:
    - myapiservice
 myapiservice:
   container_name:myapiservice
   ports:
     - "8081:80"

Upvotes: 1

Views: 140

Answers (1)

Charlie
Charlie

Reputation: 23838

You can resolve your container name to the container ip via the hosts file.

192.168.10.10   mywebservice

You can have this file in your application source and get docker to copy it to /etc

Upvotes: -1

Related Questions