Unable to connect to dockerized redis instance from outside docker

15,556

Solution 1

make sure the port 6379 on the host is forwarded to port 6379 on docker.
use -p 6379:6379 to fixed the problem:

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

Solution 2

The VM has it's own IP 192.168.99.100 so I was able to connect by binding to 192.168.99.100:6379:6379 and then connecting as below.

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

Related videos on Youtube

selflearner
Author by

selflearner

Updated on July 09, 2022

Comments

  • selflearner
    selflearner almost 2 years

    I have the latest docker installation (without boot2docker) and I am unable to connect to a dockerized redis instance running locally. Could you please tell me what I'm doing wrong here?

    Created the docker, mapped port 6379 to 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 output:

    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
    

    Tried connecting from outside container (but the same host where container is running), connection failed:

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

    It works if I try to connect from another container though..

    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> 
    

    Here's the docker inspect for the container:

    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": {}
        }
    }
    ]
    

    Am I missing something here?

  • jaycode
    jaycode over 8 years
    How did you find VM's IP?
  • Cameron
    Cameron about 8 years
    @jaycode typically you can find the IP via docker-machine ip default or see the full list with docker-machine ls