How to get the IP address of the docker host from inside a docker container

后端 未结 24 1419
鱼传尺愫
鱼传尺愫 2020-11-22 06:41

As the title says. I need to be able to retrieve the IP address the docker hosts and the portmaps from the host to the container, and doing that inside of the container.

24条回答
  •  渐次进展
    2020-11-22 07:34

    As of version 18.03, you can use host.docker.internal as the host's IP.

    Works in Docker for Mac, Docker for Windows, and perhaps other platforms as well.

    This is an update from the Mac-specific docker.for.mac.localhost, available since version 17.06, and docker.for.mac.host.internal, available since version 17.12, which may also still work on that platform.

    Note, as in the Mac and Windows documentation, this is for development purposes only.

    For example, I have environment variables set on my host:

    MONGO_SERVER=host.docker.internal
    

    In my docker-compose.yml file, I have this:

    version: '3'
    
    services:
      api:
        build: ./api
        volumes:
          - ./api:/usr/src/app:ro
        ports:
          - "8000"
        environment:
          - MONGO_SERVER
        command: /usr/local/bin/gunicorn -c /usr/src/app/gunicorn_config.py -w 1 -b :8000 wsgi
    

提交回复
热议问题