Access sibling service from docker container's service

后端 未结 1 1302
暗喜
暗喜 2021-01-16 06:11

I run Node image based docker container (Docker quickstart terminal from Windows)

FROM node:7.8.0
ENV NPM_CONFIG_LOGLEVEL warn

VOLUME /tmp
#copy server sour         


        
相关标签:
1条回答
  • 2021-01-16 06:52

    There are few things you can do here

    Let other container run on network of other container

    docker run --net container:<id> MyDockerImage
    

    Now your mongodb will be accessible on localhost. But the port needs to be exposed in the container whose network is used

    Create network yourself and use it

    docker network create myapps
    
    docker run --name mongodb_service --net myapps mongodb
    docker run -p 3000-3009:3000-3009 --net myapps MyDockerImage
    

    Now inside your MyDockerImage, mongodb can be reached at mongodb_service

    Use docker compose

    You can use docker-compose to run both of them as a composition

    version: '3'
    services:
      mongo: 
        image: mongodb
      app:
        build:
          context: .
        ports:
          - "3000-3009:3000-3009"
    

    And now in app mongodb will be reachable with name mongo

    0 讨论(0)
提交回复
热议问题