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.
- Can I connect without a password from outside?
- If you publish the port with a ports: line, Redis is reachable from outside the host. Keep the password on, and bind to 127.0.0.1 if only your computer needs it.
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.
- Where does Redis store its data in the container?
- In /data. The image declares it as a volume, so mount a named volume or folder there to keep the data.
- Why does the official image show --save instead of --appendonly?
- The image page example uses redis-server --save 60 1, a snapshot every 60 seconds if one write happened. appendonly yes, used in this file, logs every write instead. Both need the /data volume.
- How do I connect from another container?
- Use the service name as the host, for example redis-cli -h redis -a yourpassword from a container on the same compose network.
Free tools that help
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 port mappingWhat -p 8080:80 and ports: "8080:80" mean in Docker: the first number is on your computer, the second is inside the container. Which URL to open and how to fix 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.