Changing Redis port in Docker Compose not working
Asked Answered
S

4

22

I have a Docker Compose file that starts two services: Redis and Redis Commander. Using the default Redis port 6379 works fine. After changing the Redis port to 6380 Redis Commander cannot connect to Redis anymore.

Error:

setUpConnection Redis error Error: connect ECONNREFUSED 172.19.0.2:6380

This is the docker-compose.yml file:

version: '3.7'
services:
  redis:
    container_name: redis
    hostname: redis
    image: sameersbn/redis:4.0.9-2
    ports:
      - "6380:6379"
    expose:
      - "6380"
    volumes:
      - type: volume
        source: redis-data
        target: /data
    restart: always
  redis-commander:
    container_name: redis-commander
    hostname: redis-commander
    image: rediscommander/redis-commander:latest
    restart: always
    environment:
      - REDIS_HOSTS=local:redis:6380
    ports:
      - "8082:8081"
volumes:
  redis-data: {}

I can connect to Redis on port 6380 using the following Node code:

import redis from 'redis'

const config = {
  host: '127.0.0.1',
  port: 6380,
  no_ready_check: true
}

const client = redis.createClient(config.port, config.host)

client.set('expireName', 'nidkil', (err, reply) => {
  if (err) {
    console.error('Error occurred:', err)
  } else {
    console.log('Response:', reply)
  }
})

If I change the port back to 6379 in the docker-compose.yml then Redis Commander can connect.

Any suggestions how I can make Redis Commander connect to Redis on port 6380?

Scutcheon answered 14/5, 2019 at 17:44 Comment(0)
S
38

The answer of @Mihai helpt me figure out the solution. I needed to change the port Redis is running on as well as the exposed port. This is the working Docker compose file.

version: '3.7'
services:
  redis:
    container_name: redis
    hostname: redis
    image: sameersbn/redis:4.0.9-2
    command: --port 6380
    ports:
      - "6380:6380"
    expose:
      - "6380"
    volumes:
      - type: volume
        source: redis-data
        target: /data
    restart: always
  redis-commander:
    container_name: redis-commander
    hostname: redis-commander
    image: rediscommander/redis-commander:latest
    restart: always
    environment:
      - REDIS_HOSTS=local:redis:6380
    ports:
      - "8082:8081"
volumes:
  redis-data: {}
Scutcheon answered 14/5, 2019 at 18:25 Comment(3)
I was just replying that. Well done! I think you can also customise only "command: --port 6380" instead of the whole entrypoint. It would be nicer in my opinion.Locker
Done :-) Is more concise. Thx.Scutcheon
Thanks. I just add command --port 6379 and it worked even use official redis image.Gimmick
L
10

You changed the exposed port on the host. You did not change the internal port in the container. Your redis instance continues to run on the default port (6379).

Also this statement expose: - "6380" can be omitted since it is not useful.

Locker answered 14/5, 2019 at 17:54 Comment(2)
How do I change the internal port of the container without changing the Dockerfile?Scutcheon
That’s highly dependent on the configuration of the actual service; it might be a command-line argument or a configuration setting. This is one of the key benefits of using Docker. You can let the service run on its “normal” port, and because of Docker’s network isolation it won’t conflict with other services.Curtilage
L
7

.env

REDIS_PORT=8379

docker-compose.yml

redis:
    image: redis:alpine
    command: --port ${REDIS_PORT}  // <- this line work for me
    ports:
      - '${REDIS_PORT}:${REDIS_PORT}'
Lakh answered 6/11, 2023 at 3:21 Comment(0)
H
0

The port forwarding command will map the port on the host machine to the port on the docker container. Note that the port on the left of the : refers to the port on to the host machine which is 6380 in this case however the redis on host is running on port 6380. By default redis on the container will run on port 6379. To run it on desired port command --port <<port_number>> should be used.

Hoppe answered 23/4, 2023 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.