Build production ready Redis docker container with password and limited memory usage

If you simply execute docker run -d redis, you will get a redis instance. But, it should not be used in production environment, reasons are:

  • It is not protected by a password. This is very dangerous.
  • It does not limit memory usage. A container may take up too much memory of the server.

Create a better redis container

Here is an example command:

docker run --name dk_redis -d -p 16379:6379 redis redis-server \
--requirepass pass1 \
--maxmemory 512mb
  • requirepass argument set the password. Please replace pass1 with your actual password.
  • maxmemory argument set the maximum memory that redis can take up.

Test the created redis container

Access inside the container:

docker exec -it dk_redis redis-cli -a pass1

Access outside the container:

redis-cli -p 16379 -a pass1

Check if memory is limited

We set --maxmemory 512mb earlier, now let's check the effect:

docker exec -it dk_redis redis-cli -a pass1 CONFIG GET maxmemory

The output will be like:

1) "maxmemory"
2) "536870912"
Posted on 2022-04-12