How to access host port from docker container

前端 未结 14 1534
南旧
南旧 2020-11-22 03:41

I have a docker container running jenkins. As part of the build process, I need to access a web server that is run locally on the host machine. Is there a way the host web s

14条回答
  •  北海茫月
    2020-11-22 03:48

    I've explored the various solution and I find this the least hacky solution:

    1. Define a static IP address for the bridge gateway IP.
    2. Add the gateway IP as an extra entry in the extra_hosts directive.

    The only downside is if you have multiple networks or projects doing this, you have to ensure that their IP address range do not conflict.

    Here is a Docker Compose example:

    version: '2.3'
    
    services:
      redis:
        image: "redis"
        extra_hosts:
          - "dockerhost:172.20.0.1"
    
    networks:
      default:
        ipam:
          driver: default
          config:
          - subnet: 172.20.0.0/16
            gateway: 172.20.0.1
    

    You can then access ports on the host from inside the container using the hostname "dockerhost".

提交回复
热议问题