docker-compose for Redis: example file with password and persistence
Run Redis in Docker with a password, saved data and a memory limit.
This docker-compose file runs Redis with a password, optional data saving and an optional memory limit. Choose your settings below, then run docker compose up -d and test it with redis-cli.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
services:
redis:
image: redis:7
restart: unless-stopped
command:
- redis-server
- --requirepass
- change-me-redis
- --appendonly
- "yes"
ports:
- "6379:6379"
volumes:
- redisdata:/data
volumes:
redisdata:How this setup works
Step-by-step setup
- Save the config as docker-compose.yml in an empty folder.
- Start it: docker compose up -d.
- Test it: redis-cli -h 127.0.0.1 -p 6379 ping. With a password, add -a and your password. The answer PONG means it works.
- Read the log if needed: docker compose logs redis.
What the important lines mean
- --requirepass sets the password. Without it anyone who can reach the port can use Redis.
- --appendonly yes writes every change to disk, so data survives a restart.
- --maxmemory with allkeys-lru removes the least used keys when the limit is reached. Use it for a cache.
Common errors and how to fix them
- NOAUTH Authentication required
- The server has a password. Use redis-cli -a yourpassword or put the password in your app’s connection string.
- Could not connect to Redis: Connection refused
- The container is not running, or you used the wrong port. Check docker compose ps.
- Port 6379 is already allocated
- Another Redis or container uses it. Pick another port above, such as 6380.
- Data is lost after a restart
- Save data to disk must be ticked, and the volume must stay. Do not use docker compose down -v.
Frequently asked questions
- How do I test that Redis is working?
- Run redis-cli ping. The reply PONG means it is running.
- Is it safe to expose Redis to the internet?
- No. Keep it on localhost or a private network, and always set a password.
- What is the difference between a cache and saved data?
- A cache can lose keys safely, so use a memory limit. For data you cannot lose, turn on saving to disk and skip the memory limit.
Free tools that help
- Password generatorCreate a random password with the length and characters you choose. It runs in your browser, uses secure randomness and shows how strong it is.
- Common ports listSearch a list of common TCP and UDP port numbers: SSH 22, HTTP 80, HTTPS 443, MySQL 3306, PostgreSQL 5432 and more, with dev server ports.
Fix a problem with this setup
- Docker port already allocatedFix "port is already allocated" in Docker and Docker Compose: find the container or program that holds the port and free it.
- Docker container cannot reach localhostFix "connection refused" when an app in Docker calls localhost: use host.docker.internal, a Compose service name or host networking.
More container configs
- docker-compose for WordPressRun WordPress and its database locally in Docker with one command and keep your site between restarts.
- Dockerfile for Node.jsA small, working Dockerfile for a Node.js app, with build step, non-root user and health check options.
- Dockerfile for PythonA working Dockerfile for a Python web app, with Gunicorn, Uvicorn or a plain script.
- Docker Compose for MailpitRun a fake mail server on your computer, so test emails from your app show up in a web page instead of being sent.
- Kubernetes Deployment and ServiceA Deployment and Service YAML for your container image, with replicas, port, resource limits and health checks.
- Docker Compose for MySQL and phpMyAdminRun MySQL and the phpMyAdmin web page on your computer with one docker compose command.