None of these answers seemed to answer the question directly, which is how to create a custom username and a custom password with the redis:latest container image with docker-compose. (Not just applying a custom password for the default user)
First of all, there isn't an easily supported way to do this with the redis:latest image. You can accomplish this directly with redis-stack images, but that is not what op requested. Not to say its impossible, but the way redis handles permissions with its access control lists requires you to create a user and grant permissions with their acl files in a script.
I was able to accomplish creating a custom user, assign them a password, and
permissions with docker-compose + Dockerfile + a custom-entrypoint.sh. I also added the ability to disable the default user as well. These are the steps:
- Directory structure
.
├── docker-compose.yaml
├── .env
│ └── .env.dev.msg-broker.reddis
└── services
└── msg-broker
├── custom-entrypoint.sh
└── Dockerfile.dev
- Configure your docker-compose.yaml
version: '3.3'
services:
msg-broker:
container_name: "msg-broker"
build:
context: ./services/msg-broker
dockerfile: Dockerfile.dev
env_file:
- ./.env/.env.dev.msg-broker.reddis
ports:
- "6379:6379"
- Configure the env var file. You can also accomplish this by applying the variables directly in the docker-compose.yaml by creating an
environment key instead. This is where you can configure you username and password, and if you want to keep the default user or not.
REDIS_USERNAME=myadmin
REDIS_PASSWORD=MyAdm1nP455w0rd
REDIS_DISABLE_DEFAULT_USER="true"
- Configure Dockerfile, notice how the custom-entrypoint.sh is copied over and ran instead of the default one directly.
FROM redis:latest
# Copy the custom entrypoint script
COPY custom-entrypoint.sh /usr/local/bin/custom-entrypoint.sh
RUN chmod +x /usr/local/bin/custom-entrypoint.sh
ENTRYPOINT ["custom-entrypoint.sh"]
- Configure the custom-entrypoint.sh. This is where the real configuration happens.
#!/bin/sh
# Set up Redis configuration directory
mkdir -p /usr/local/etc/redis
# Dynamically generate Redis configuration and ACL files here, using environment variables
echo "aclfile /usr/local/etc/redis/custom_aclfile.acl" > /usr/local/etc/redis/redis.conf
# Generate ACL file using environment variables
if [ -n ${REDIS_USERNAME} ] && [ -n ${REDIS_PASSWORD} ]; then
echo "user ${REDIS_USERNAME} on allkeys allchannels allcommands >${REDIS_PASSWORD} " > /usr/local/etc/redis/custom_aclfile.acl
fi
# Disable default user
if [ $(echo ${REDIS_DISABLE_DEFAULT_USER}) == "true" ]; then
echo "user default off nopass nocommands" >> /usr/local/etc/redis/custom_aclfile.acl
fi
# Call the original Docker entrypoint script with redis-server and the path to the custom Redis configuration
exec docker-entrypoint.sh redis-server /usr/local/etc/redis/redis.conf
- Run it
docker-compose up --build
You should be able to adjust the permissions for your users needs by tweaking the echo "user ${REDIS_USERNAME} on allkeys allchannels allcommands >${REDIS_PASSWORD} " > /usr/local/etc/redis/custom_aclfile.acl command in the custom-entrypoint script. I know the permissions I assigned give my user god like control, however I needed this for my celery instance to work, see this post for more info.