Connecting to Redis running in Docker Container from Host machine

后端 未结 5 1667
别跟我提以往
别跟我提以往 2021-02-01 14:20

I see lots of people struggling with this, sort of feel like maybe there is a bug in the redis container image, and others seem to be chasing a similar problem.

I\'m usi

相关标签:
5条回答
  • 2021-02-01 14:36

    It might be easier now with version 4.0.9 (Docker Toolbox on Win10). Simply connect with a redis client, then:

    set bind 0.0.0.0
    save
    

    The new setting sticks after stop/start.

    0 讨论(0)
  • 2021-02-01 14:50

    The problem is with your bind, You should set the following:

    bind 0.0.0.0
    

    This will set redis to bind to all interfaces available, in a containerized environment with one interface, (eth0) and a loopback (lo) redis will bind to both of the above. You should consider adding security measures via other directives in config file or using external tools like firewalls. because with this approach everyone can connect to your redis server.

    The default setting is bind 127.0.0.1 and this setting will cause redis to only listen on loopback interface, and it will be only accessible from inside the container. (for security)

    To run redis with custom configuration file:

    sudo docker run -d --name redis-test -p 6379:6379  -v /path/to/redisconf/redis.conf:/redis.conf redis redis-server /redis.conf
    

    Now to verify on docker host with redis-tools installed:

    redis-cli                           
    127.0.0.1:6379> 
    127.0.0.1:6379> set farhad likes:stackoverflow
    OK
    127.0.0.1:6379> get farhad
    "likes:stackoverflow"
    127.0.0.1:6379> 
    

    You can also connnect to your redis container from an external host via:

    redis-cli -h 'IP-address-of-dockerhost-running-redis-container'
    
    0 讨论(0)
  • 2021-02-01 14:55

    Here are some instructions to make this work properly.

    Install Official Docker not Distro repo.

    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    systemctl enable docker ; systemctl start docker; systemctl status docker
    

    Refer to Install using the convenience script

    Connect to Redis container from host

    mkdir -p /etc/redis/
    chown -R 1000:1000 /etc/redis
    sudo docker run -d --name redis -p 6379:6379 --restart unless-stopped -v /etc/redis/:/data redis redis-server /data
    

    NOTE: The important part that is key to your solution is to have port expose (-p 6379:6379) to your docker host and route to container port. Refer to Redis Docker Documentation

    Install "redis-tools" in your docker host. Centos install redis via epel release.

    0 讨论(0)
  • 2021-02-01 15:00

    This is simpler way to set up a Redis container.

    Download image and run container

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

    If you don't have the image, this command will pull it. And then, if you need to access from redis-cli to console, can use:

    docker exec -it some-redis bash
    

    For enter to container console, and kind in the console:

    root@72c388dc2cb8:/data# redis-cli
    

    Output:

    127.0.0.1:6379> 
    

    This was enough for my use case (easy and fast local development).

    0 讨论(0)
  • 2021-02-01 15:00

    create Redis container using below command

    sudo docker run -d --name redis-test -p 6379:6379  -v /redis/redis.conf:/redis.conf redis redis-server /redis.conf --appendonly yes --requirepass "redis"
    

    you can access the Redis in the same machine using Redis-CLI and if you are using other machines use host machine IP. if you are accessing Redis container in the same host another docker container uses the private IP of the machine.

    0 讨论(0)
提交回复
热议问题