简体   繁体   中英

How to access other container id inside docker-compose.yml?

I have a situation in which I have a docker-compose.yml file as follows

version: '3'

services:

  database:
    image: mcr.microsoft.com/mssql/server:2017-latest
    environment:
      - SA_PASSWORD=king@1234
      - ACCEPT_EULA=Y
    ports:
      - "8110:1433"
    container_name: app_db_con

  api:
    build:
      context: /home/shubham/Employee/EmployeeBackEnd/
    image: app-api:dev
    ports:
      - "8111:5001"
    container_name: app_api_con
    depends_on:
      - database

  web:
    build:
      context: /home/shubham/Employee/EmployeeFrontEnd/
    image: app-ui:dev
    ports:
      - "8112:80"
    container_name: app_web_con
    depends_on:
      - database
      - api

I want to access the database container id inside my api container as an argument. For example the args in below code:

  api:
    build:
      context: /home/shubham/Employee/EmployeeBackEnd/
    image: app-api:dev
    args:
      - Database_Server: database_container_id
    ports:
      - "8111:5001"
    container_name: app_api_con
    depends_on:
      - database

I am not sure how to achieve this. Please help if there is a different approach as well.

Thank You

By default, with docker-compose, each container is accessible by it's service name defined in the compose file.

For example, in your case, if you want to access the database container from the api , you'll just have to use its name. You can try it out with this command :

$ docker container exec -ti [your_api_container] ping database

For more information about networking in compose, you can look at the official doc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM