正如标题所示,我需要能够检索docker托管的IP地址以及从主机到容器的portmap,并在容器内部完成这些工作。


当前回答

也许我创建的容器也很有用https://github.com/qoomon/docker-host

你可以简单地使用容器名称dns访问主机系统,例如curl http://dockerhost:9200,所以不需要任何IP地址的麻烦。

其他回答

与https://docs.docker.com/machine/install-machine/

A) $ docker-machine IP

b)获取一台或多台机器的IP地址。

  $ docker-machine ip host_name

  $ docker-machine ip host_name1 host_name2

从版本18.03开始,您可以使用host.docker.internal作为主机的IP。

适用于Mac的Docker, Windows的Docker,以及其他平台。

这是mac专用docker.for.mac的更新。Localhost(17.06版后可用)和docker.for.mac.host.internal(17.12版后可用),它们也可以在该平台上运行。

注意,在Mac和Windows文档中,这仅用于开发目的。

例如,我在主机上设置了环境变量:

MONGO_SERVER=host.docker.internal

在我码头式的写作中。yml文件,我有这个:

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

唯一的方法是在创建容器时将主机信息作为环境传递

run --env <key>=<value>

所以…如果你使用Rancher服务器运行你的容器,Rancher v1.6(不确定2.0是否有)容器可以访问http://rancher-metadata/,其中有很多有用的信息。

从容器内部可以在这里找到IP地址: curl http://rancher-metadata/latest/self/host/agent_ip

详情见: https://rancher.com/docs/rancher/v1.6/en/rancher-services/metadata-service/

这是一个在Node.js中使用前面提到的EC2元数据实例在AWS EC2实例上运行主机的最简单实现

const cp = require('child_process');
const ec2 = function (callback) {
    const URL = 'http://169.254.169.254/latest/meta-data/local-ipv4';
    // we make it silent and timeout to 1 sec
    const args = [URL, '-s', '--max-time', '1'];
    const opts = {};
    cp.execFile('curl', args, opts, (error, stdout) => {
        if (error) return callback(new Error('ec2 ip error'));
        else return callback(null, stdout);
    })
        .on('error', (error) => callback(new Error('ec2 ip error')));
}//ec2

并用作

ec2(function(err, ip) {
        if(err) console.log(err)
        else console.log(ip);
    })