从 Windows 主机访问 dockerized redis [英] Access dockerized redis from windows host

查看:49
本文介绍了从 Windows 主机访问 dockerized redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 windows 上的节点后端开发,我试图在 docker 容器中设置 redis,因为 redis windows 版本对我来说似乎有问题.我对 docker 很陌生,我不知道随之而来的所有原则.

For node backend development on windows I am trying to setup redis in a docker container as the redis windows version seems to be buggy for me. I am very new to docker and I am not aware of all the principles coming along with it.

到目前为止我做了什么:

  1. 安装的docker
  2. 运行'docker pull redis'
  3. 运行'docker run --name some-redis -d redis redis-server --appendonly yes'来启动redis容器

问题:

我尝试连接到 127.0.0.1:6379(当我在系统上本地安装 redis 时,它曾经可以工作),但它超时了.我以为redis容器有它自己的ip地址,我认为它的ip地址是172.17.0.2.但是连接到这个 ip 也没有用.

I tried connecting to 127.0.0.1:6379 (which used to work when I had redis installed natively on my system), but it is timeouting. I thought that the redis container has it's own ip address and I figured it's ip addres sis 172.17.0.2. Connecting to this ip didn't work either though.

PS C:WINDOWSsystem32> docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' some-redis
172.17.0.2

PS C:WINDOWSsystem32> docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d3b796e9df5c        redis               "docker-entrypoint..."   About an hour ago   Up 8 minutes        6379/tcp            some-redis

为了从本地机器连接到容器内的 redis,我缺少什么?(我的节点应用程序没有 dockerized)

What am I missing in order to connect from my local machine to redis inside of my container? (My node application is not dockerized)

推荐答案

你错过了暴露端口.使用命令运行 redis 容器

You miss to expose port. Run redis container with command

docker run --name some-redis -p6379:6379 -d redis redis-server --appendonly yes

如果 Dockerfile 包含 EXPOSE <some_port> 这意味着 另一个容器到同一个 docker 网络 可以连接到这个端口.仅此而已.

If Dockerfile contains EXPOSE <some_port> it means another containers into same docker network can connect to this port. Nothing more.

如果你想从主机连接到容器,你需要说 docker.

If you want to connect to container from host machine you need say docker about it.

  • 您可以在 docker run 命令中添加 -P 选项.在这种情况下码头工人将所有定义的端口暴露给本地机器上的随机端口.
  • 或者你可以添加选项 -p: 然后你暴露某些端口.
  • you can add -P option to docker run command. In this case docker exposes all defined ports to random ports on you local machine.
  • Or you can add option -p<port_on_host_machine>:<port_inside_docker_container> then you expose certain port.

这篇关于从 Windows 主机访问 dockerized redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆