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
2
Answers
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:
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:
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
Connections between containers always use the standard port for the destination service; they do not consider
ports:
at all. No matter what you haveports:
set to you should keep your current configurationports:
are only needed if you need to access Redis from outside a container, maybe usingredis-cli
for debugging or for doing local development against the container Redis. If you don’t need this, it’s safe to just delete theports:
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.