reggaemahn
reggaemahn

Reputation: 6668

Use/reference container name in a different service with docker compose

My docker-compose.yml file looks like this:

version: '3.7'

networks: 
  myNetwork:

volumes:
  mongo_data:

services:

  db:
    image: mongo
    restart: always
    networks: 
      - myNetwork
    volumes: 
      - mongo_data:/data/db
    ports: 
        - "27017:27017"

  mongoExpress:
    image: mongo-express
    networks: 
      - myNetwork
    ports:
      - "8081:8081"
    environment: 
      - ME_CONFIG_MONGODB_SERVER=<db-container-name>
    depends_on: 
      - db

For mongo-express to work, I need to be able to pass it the db/mongo container name. Is there a way to achieve this with compose?

Upvotes: 1

Views: 4022

Answers (1)

DazWilkin
DazWilkin

Reputation: 40356

The service name is the host name on the network (myNetwork) created by compose.

You may use db as the host name and value for <db-container-name>

i.e.

ME_CONFIG_MONGODB_SERVER=db

Upvotes: 2

Related Questions