skip to Main Content

I have the following docker-compose.yml, but need to model a public/private network split where the Redis instance must only be accessible to localhost.

version: "2.2"  # for compatibility, I can upgrade if needed
services:
  nginx:
    image: nginx
    ports:
    - 8080:80
  redis:
    image: redis
    ports:
    - 6379:6379

This seems straightforward if I needed to restrict it to being accessible only within the docker network. Consider:

version: "2.2"
services:
  nginx:
    image: nginx
    ports:
    - 8080:80
    networks:
    - frontend
    - backend
  redis:
    image: redis
    ports:
    - 6379:6379
    networks:
    - backend

networks:
  frontend: {}
  backend:
    internal: true

However our local web developers need to be able to access that Redis instance from their host machine (outside of the docker network) when they build, run, and debug locally.

2

Answers


    • Run Redis Web UI called redis-commander.
    • Use env vars to point to the running redis.
    • Expose this new container & access it instead of exposing Redis container.
      services:
        redis:
          # Do comment ports! no need to expose it
          # ports:
          # - 6379:6379
        // ....
        redis-commander:
          image: rediscommander/redis-commander:latest
          restart: unless-stopped
          environment:
            REDIS_HOST: redis:6379 # <-- 🔴 Here you point to your redis
            # REDIS_PASSWORD # <- in case redis is protected with password
          ports:
            - "8081:8081"
    

    Let your developers go to http://localhost:8081 and enjoy.

    Find more details about that image

    Login or Signup to reply.
  1. Just bind the service port of redis to localhost(127.0.0.1).
    Try follows…

    ...
      redis:
        image: redis
        ports:
        - 127.0.0.1:6379:6379
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search