ssh隧道到远程dockerized mongo服务器

时间:2015-09-09 14:47:25

标签: mongodb ssh ssh-tunnel tunneling

我在远程服务器上运行了一个mongo docker实例,从本地计算机访问命令行的正确方法是什么?

如果我登录远程主机,我可以通过以下方式访问:

$ docker exec -it mongo-dev mongo ccc-mongo

但我不确定如何从我的本地机器上执行此操作。

我试过了:

$ ssh -L 4321:localhost:27017 khine@ccc1 -f -N

Are you sure you want to continue connecting (yes/no)? yes
khine@ccc1's password: 
khine@dhegdheer:~/Sandboxes/$ mongo --port 4321
MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:4321/test
channel 2: open failed: connect failed: Connection refused
Wed Sep  9 15:36:44.386 DBClientCursor::init call() failed
Wed Sep  9 15:36:44.388 Error: DBClientBase::findN: transport error: 127.0.0.1:4321 ns: admin.$cmd query: { whatsmyuri: 1 } at src/mongo/shell/mongo.js:147
exception: connect failed

在我的远程计算机上我运行了3个mongo实例

khine@ccc1 /ccc $ docker ps                                
CONTAINER ID        IMAGE                        COMMAND                CREATED             STATUS              PORTS                                      NAMES

22a32b4f6a1d        redis:2.8                    "/entrypoint.sh redi   7 days ago          Up 7 days           6379/tcp                                   redis-web            
167b022ab793        mongo:2.4                    "/entrypoint.sh mong   7 days ago          Up 7 days           27017/tcp                                  mongo-web            
ab84ea6cb44a        redis:2.8                    "/entrypoint.sh redi   2 weeks ago         Up 2 weeks          6379/tcp                                   redis-www            
04dcc306af04        redis:2.8                    "/entrypoint.sh redi   2 weeks ago         Up 2 weeks          6379/tcp                                   redis-dev            
02c0c18307dc        mongo:2.4                    "/entrypoint.sh mong   2 weeks ago         Up 2 weeks          27017/tcp                                  mongo-www            
61df69ec7edb        mongo:2.4                    "/entrypoint.sh mong   2 weeks ago         Up 2 weeks          27017/tcp                                  mongo-dev        

运行docker inspect,我明白了:

khine@ccc1 /ccc $ docker inspect 61df69ec7edb
[{
    "AppArmorProfile": "",
    "Args": [
        "mongod"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "mongod"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": [
            "/entrypoint.sh"
        ],
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "MONGO_VERSION=2.4.14"
        ],
        "ExposedPorts": {
            "27017/tcp": {}
        },
        "Hostname": "61df69ec7edb",
        "Image": "mongo:2.4",

    "HostConfig": {
        "Binds": [
            "/ccc/mongo-data/dev:/data/db"
        ],
        "CapAdd": null,
        "CapDrop": null,
        "CgroupParent": "",

    "Name": "/mongo-dev",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.34",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "fe80::42:acff:fe11:22",
        "LinkLocalIPv6PrefixLen": 64,
        "MacAddress": "02:42:ac:11:00:22",
        "PortMapping": null,
        "Ports": {
            "27017/tcp": null
        }
    },
    "Path": "/entrypoint.sh",
    "ProcessLabel": "",
    "ResolvConfPath": "/var/lib/docker/containers/61df69ec7edb6995f06d797f5b2eed420d0c4daa4cd089c3b9174900d72d0b13/resolv.conf",
    "RestartCount": 0,
    "State": {
        "Dead": false,
        "Error": "",
        "ExitCode": 0,
        "FinishedAt": "0001-01-01T00:00:00Z",
        "OOMKilled": false,
        "Paused": false,
        "Pid": 15346,
        "Restarting": false,
        "Running": true,
        "StartedAt": "2015-08-26T06:01:55.361817334Z"
    },
    "Volumes": {
        "/data/db": "/ccc/mongo-data/dev"
    },
    "VolumesRW": {
        "/data/db": true
    }
}
]

如果我添加实例的IP地址,我会收到此警告

$警告:侦听端口4321的远程端口转发失败 任何建议都非常感激。

0 个答案:

没有答案