📜  redis docker requirepass (1)

📅  最后修改于: 2023-12-03 14:47:02.711000             🧑  作者: Mango

Redis Docker Requirepass

Redis is an open-source, in-memory key-value data store. It is often used as a cache or a message broker, but it also supports a wide range of data structures. Docker is a platform for packaging, deploying, and running applications in containers. Redis can be easily deployed in a Docker container, and the Docker image for Redis includes the ability to set a password, or "requirepass", to secure access to the Redis server.

Setting the requirepass

To set the requirepass for a Redis container, you can use the redis-cli command and pass in the password as an argument.

$ docker run --name my-redis -d redis redis-server --requirepass mypassword

This will start a Redis container named my-redis and set the password to mypassword. You can test that the password is working by trying to connect without any authentication:

$ redis-cli -h localhost ping
$ (error) NOAUTH Authentication required.

You can then authenticate by passing in the password as an argument:

$ redis-cli -h localhost -a mypassword ping
$ PONG
Using the requirepass in your code

To connect to a Redis container with a requirepass from your code, you need to pass in the password as an option when creating a Redis client. The Redis client libraries for various programming languages provide a way to set the requirepass option.

For example, in Python, you can use the redis library and set the password when creating a client:

import redis

r = redis.Redis(host='localhost', port=6379, db=0, password='mypassword')

In Node.js, you can use the redis library and set the password when creating a client:

const redis = require('redis');

const client = redis.createClient({
  host: 'localhost',
  port: 6379,
  password: 'mypassword'
});
Conclusion

Setting a password for your Redis container is an important step in securing access to your Redis server. With Docker, it is easy to set a password when creating a Redis container, and the Redis client libraries provide a way to pass in the password when connecting to the Redis server.