英文:
Docker port binding for Redis with Laravel Sail
问题
我有多个 Laravel 本地项目,我正在尝试更改一个项目的 Redis 端口,以便它不与另一个项目发生冲突。
这些项目在同一个网络上,以便它们可以相互通信,但它们具有单独的 Redis 实例。
这是我的 Docker Compose 文件中的 Redis 服务部分:
redis:
image: 'redis:alpine'
ports:
- '6380:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
我正在尝试将 Redis 设置为端口 :6380
上可用。
在 Docker Desktop 中,我可以看到端口绑定似乎正常工作:
我的 .env
文件中 Redis 部分是默认的,只更改了端口到新的值:
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6380
问题
当我尝试运行 sail artisan cache:clear
时,我总是得到 RedisException Connection refused
错误。
我尝试过的解决方法
我尝试将 REDIS_HOST
环境变量更改为与容器名称完全相同。还尝试使用 sail artisan config:clear
清除配置,但都没有成功。
注意: 由于某种原因,仍然可以访问端口 :6379
。我想象它不应该可访问,因为它应该绑定到 :6380
。
英文:
I have a multiple Laravel local projects and I'm trying to change one projects Redis port, so that it doesn't conflict with another project.
Projects are one the same network so that they can communicate with one another, but they have separate Redis instances.
This is my Redis service inside docker-compose.yml
redis:
image: 'redis:alpine'
ports:
- '6380:6379'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
I'm trying to have Redis available on port :6380
.
In Docker Desktop I can see, that port bind seems to work correctly:
My .env
redis section is stock, just changed the port to the new one:
REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6380
Problem
When I try to run sail artisan cache:clear
I always get RedisException Connection refused
.
What I have tried
I've tried changing the REDIS_HOST
environment variable to be exact as container name. Also tried clearing the config with sail artisan config:clear
. All with no luck.
Note: for some reason the :6379
port is still accessible. I imagine that it shouldn't be as it should be binded to :6380
答案1
得分: 3
6380:6379
意味着容器中的端口6379将在主机上的端口6380上可用,但是当使用Sail时,您的应用程序在另一个容器中运行,而不是在主机上。
解决方案是更改Redis监听的端口,您可以修改您的docker-compose文件:
redis:
image: 'redis:alpine'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
command:
- redis-server
- "--port"
- "6380"
如果您不想更改容器命令,另一种选择是配置您的应用程序通过主机访问Redis:
REDIS_HOST=172.17.0.1
REDIS_PASSWORD=null
REDIS_PORT=6380
这里的172.17.0.1是默认情况下Docker网络的主机IP地址,但您的值可能会有所不同。
英文:
6380:6379
means port 6379 in the container is going to be available at 6380 on the host however when using sail your app is running in another container and not on the host.
The solution is to just change the port Redis is listening on you can modify your docker-compose file:
redis:
image: 'redis:alpine'
volumes:
- 'sail-redis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
retries: 3
timeout: 5s
command:
- redis-server
- "--port"
- "6380"
An alternative if you don't want to change the container command would be to configure your app to get to redis via the host:
REDIS_HOST=172.17.0.1
REDIS_PASSWORD=null
REDIS_PORT=6380
Here 172.17.0.1 is what is set up as the host IP of the docker network by default, however your value may vary
答案2
得分: 1
容器之间的连接始终使用目标服务的标准端口;它们根本不考虑 ports:
。无论您将 ports:
设置为什么值,都应该保持当前的配置
REDIS_HOST=redis
REDIS_PORT=6379
只有在需要从容器外部访问Redis时才需要使用 ports:
,也许是为了调试或者在容器Redis上进行本地开发时使用 redis-cli
。如果不需要这样做,完全可以安全地删除 ports:
部分。如果您决定保留它,第二个端口号仍然需要是标准端口号6379,因此请将第一个端口号更改为主机上不冲突的端口号。
英文:
Connections between containers always use the standard port for the destination service; they do not consider ports:
at all. No matter what you have ports:
set to you should keep your current configuration
REDIS_HOST=redis
REDIS_PORT=6379
ports:
are only needed if you need to access Redis from outside a container, maybe using redis-cli
for debugging or for doing local development against the container Redis. If you don't need this, it's safe to just delete the ports:
block entirely. If you do keep it, the second number needs to continue to be the standard port number 6379, so change the first port number to a non-conflicting port on the host.
答案3
得分: 0
已通过创建一个用于共享通信的单独网络来解决了此问题,所有单独的 Redis 容器都无法访问此网络。这使我能够保留默认的 Redis 配置(除了将绑定的端口更改为未占用的端口,但正如 @david-maze 所说,这实际上不会对 Redis 产生任何影响)。
英文:
Solved the issue by simply creating a separate network for shared communication which all separate Redis containers don't have access to. This allows me to leave default Redis configuration (except changing binded port to a unoccupied port, but as @david-maze said this doesn't actually have any affect for Redis).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论