无法从外部 docker 连接到 dockerized redis 实例

Posted

技术标签:

【中文标题】无法从外部 docker 连接到 dockerized redis 实例【英文标题】:Unable to connect to dockerized redis instance from outside docker 【发布时间】:2015-12-31 16:44:10 【问题描述】:

我有最新的 docker 安装(没有 boot2docker),我无法连接到本地运行的 dockerized redis 实例。你能告诉我我在这里做错了什么吗?

创建了 docker,将端口 6379 映射到 127.0.0.1:6379

bash-3.2$ docker run -p 127.0.0.1:6379:6379  --name webmonitor-redis -d redis
3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9

docker ps 输出:

bash-3.2$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
3291541d58ab        redis               "/entrypoint.sh redis"   14 seconds ago      Up 6 seconds        127.0.0.1:6379->6379/tcp   webmonitor-redis

尝试从外部容器连接(但与运行容器的主机相同),连接失败:

bash-3.2$ ./src/redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected> exit

如果我尝试从另一个容器连接,它会起作用..

bash-3.2$ docker run -it --link webmonitor-redis:redis --rm redis sh -c 'exec redis-cli -h "$REDIS_PORT_6379_TCP_ADDR" -p "$REDIS_PORT_6379_TCP_PORT"'
172.17.0.8:6379> 

这是容器的 docker 检查:

bash-3.2$ docker inspect 3291541d58ab
[

    "Id": "3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9",
    "Created": "2015-10-03T15:48:17.818355794Z",
    "Path": "/entrypoint.sh",
    "Args": [
        "redis-server"
    ],
    "State": 
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 7769,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2015-10-03T15:48:17.954436198Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    ,
    "Image": "2f2578ff984f013c9a5d6cbb6fe061ed3f73a17380a4c9b53b76d4b8da3eda7d",
    "NetworkSettings": 
        "Bridge": "",
        "EndpointID": "b787e46d1219f36d4f1b1ea35c5f750f7174221137fb01889a26a3bc1e1c6aee",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "HairpinMode": false,
        "IPAddress": "172.17.0.8",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "MacAddress": "02:42:ac:11:00:08",
        "NetworkID": "30176c9c7c14a6a052af784014832a0c52b5966089d7bcfe535041569e6bb1c9",
        "PortMapping": null,
        "Ports": 
            "6379/tcp": [
                
                    "HostIp": "127.0.0.1",
                    "HostPort": "6379"
                
            ]
        ,
        "SandboxKey": "/var/run/docker/netns/3291541d58ab",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null
    ,
    "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/resolv.conf",
    "HostnamePath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/hostname",
    "HostsPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/hosts",
    "LogPath": "/mnt/sda1/var/lib/docker/containers/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9/3291541d58ab16c362f9e0cd7017d179c0bc9aef3a1323e79f1e1ca075e171c9-json.log",
    "Name": "/webmonitor-redis",
    "RestartCount": 0,
    "Driver": "aufs",
    "ExecDriver": "native-0.2",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "",
    "ExecIDs": null,
    "HostConfig": 
        "Binds": null,
        "ContainerIDFile": "",
        "LxcConf": [],
        "Memory": 0,
        "MemorySwap": 0,
        "CpuShares": 0,
        "CpuPeriod": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "CpuQuota": 0,
        "BlkioWeight": 0,
        "OomKillDisable": false,
        "MemorySwappiness": -1,
        "Privileged": false,
        "PortBindings": 
            "6379/tcp": [
                
                    "HostIp": "127.0.0.1",
                    "HostPort": "6379"
                
            ]
        ,
        "Links": null,
        "PublishAllPorts": false,
        "Dns": null,
        "DnsSearch": null,
        "ExtraHosts": null,
        "VolumesFrom": null,
        "Devices": [],
        "NetworkMode": "default",
        "IpcMode": "",
        "PidMode": "",
        "UTSMode": "",
        "CapAdd": null,
        "CapDrop": null,
        "GroupAdd": null,
        "RestartPolicy": 
            "Name": "no",
            "MaximumRetryCount": 0
        ,
        "SecurityOpt": null,
        "ReadonlyRootfs": false,
        "Ulimits": null,
        "LogConfig": 
            "Type": "json-file",
            "Config": 
        ,
        "CgroupParent": "",
        "ConsoleSize": [
            0,
            0
        ]
    ,
    "GraphDriver": 
        "Name": "aufs",
        "Data": null
    ,
    "Mounts": [
        
            "Name": "643a80cbd7a50cfd481acc48721b34030c8ce55ba64ac3bc161d5b330c9374d2",
            "Source": "/mnt/sda1/var/lib/docker/volumes/643a80cbd7a50cfd481acc48721b34030c8ce55ba64ac3bc161d5b330c9374d2/_data",
            "Destination": "/data",
            "Driver": "local",
            "Mode": "",
            "RW": true
        
    ],
    "Config": 
        "Hostname": "3291541d58ab",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": 
            "6379/tcp": 
        ,
        "PublishService": "",
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "REDIS_VERSION=3.0.3",
            "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.0.3.tar.gz",
            "REDIS_DOWNLOAD_SHA1=0e2d7707327986ae652df717059354b358b83358"
        ],
        "Cmd": [
            "redis-server"
        ],
        "Image": "redis",
        "Volumes": 
            "/data": 
        ,
        "VolumeDriver": "",
        "WorkingDir": "/data",
        "Entrypoint": [
            "/entrypoint.sh"
        ],
        "NetworkDisabled": false,
        "MacAddress": "",
        "OnBuild": null,
        "Labels": 
    

]

我错过了什么吗?

【问题讨论】:

注意:主机是Mac OS X 【参考方案1】:

确保将主机上的 6379 端口转发到 docker 上的 6379 端口。 使用-p 6379:6379 解决问题:

docker run -d --name redisDev -p 6379:6379 redis

【讨论】:

【参考方案2】:

VM 有自己的 IP 192.168.99.100,所以我可以通过绑定到 192.168.99.100:6379:6379 进行连接,然后如下所示进行连接。

0c4de9a25467:redis-stable electron$ ./src/redis-cli -h 192.168.99.100
192.168.99.100:6379> 

【讨论】:

你是如何找到VM的IP的? @jaycode 通常您可以通过docker-machine ip default 找到 IP 或通过docker-machine ls查看完整列表【参考方案3】:

docker run -p 6379:6379 -host 0.0.0.0 --name webmonitor-redis -d redis

【讨论】:

以上是关于无法从外部 docker 连接到 dockerized redis 实例的主要内容,如果未能解决你的问题,请参考以下文章

无法从 Elastic Beanstalk Docker 连接到 RDS

从外部连接到 docker 容器中的 Postgresql

sh 从外部连接到Docker容器中的Postgresql

从 docker 主机外部远程连接到在 docker 容器上运行的 oracle 数据库

可以从docker容器外部连接到数据库,但不能从内部(在laravel中)连接到数据库

无法从本地连接到 mysql docker