Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codingforentrepreneurs/faf94fecb60dbcac7b9cdb3898e24e26 to your computer and use it in GitHub Desktop.
Save codingforentrepreneurs/faf94fecb60dbcac7b9cdb3898e24e26 to your computer and use it in GitHub Desktop.
How I use Redis for new projects with Docker Compose

compose.yaml

version: '3.9'
services:
    redis:
        image: redis
        restart: always
        ports:
            - 6178:6379
        volumes:
            - redis_data_a:/data
        entrypoint: redis-server --appendonly yes
    with-pw:
        image: redis
        restart: always
        ports:
            - 6179:6379
        volumes:
            - redis_data_b:/data
        env_file: .env
        entrypoint: redis-server --appendonly yes  --requirepass ${REDIS_PASSWORD}
    
volumes:
    redis_data_a:
    redis_data_b:

networks:
  default:
    name: redis_network

.env

REDIS_PASSWORD=abc

This compose file yields:

  • redis://localhost:6179
  • redis://localhost:6178

Running

  • docker compose pull - gets the lastest redis image
  • docker compose up - runs the redis databases via Docker compose in the foreground (e.g. blocking)
  • docker compose up -d - runs the redis databases via Docker compose in the background (e.g. non-blocking). This blocks the PORT value too.

View what's running

  • docker compose ps -- see all docker compose services that are running
  • docker ps -- see all docker containers that are running (similar but different)

Stopping what's running

  • docker compose down - stop + keep database data. Frees up the PORT value (regardless of how docker compose is running)
  • docker compose down --rmi all -v - DANGER - this will stop the database running AND delete all data.

Using other compose files:

  • docker compose -f compose.other.yaml Anything after -f needs to be a docker compose file. Consider using this for staging.

Example using redis-cli client

redis-cli -h localhost -p 6178 PING

Should respond with PONG

redis-cli -h localhost -p 6179 PING

Should respond with (error) NOAUTH Authentication required.

Using auth:

redis-cli -h localhost -p 6179

Then use the abc password set in .env file:

> auth abc 

Using Redis in the Redis shell (redis-cli):

set some_key some_value
get some_key

These method(s) will work for redis that has auth and redis that doesnt. If redis has auth, you must authenticated before these commands will work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment