Redis is a powerful key-value store that can be quickly deployed as a Docker container. This article will show you how to create and deploy a Redis container using the Docker Hub platform. Redis is a popular key-value store that can be used for caching, logging, and other purposes. It can be quickly deployed as a Docker container using the Docker Hub platform. This article will show you how to create and deploy a Redis container using the Docker Hub platform. To create a Redis container, first install the docker client on your computer. Next, use the docker command to launch an interactive session with the docker hub platform: docker login Next, use the docker pull command to download the latest version of Redis from the Docker Hub: docker pull redis:latest ..
Redis is an in-memory key-value store which can save abstract data structures with high performance. The open-source software is typically used for database, messaging, and caching functions.
Docker is the leading toolkit for packaging applications into containers. It lets you isolate software components into independent environments with their own filesystem.
In this guide, we’ll use Docker to quickly deploy Redis using the official image on Docker Hub. Compared to bare metal installation, Docker enables a simpler set up procedure and won’t pollute your host with new packages. Make sure you’ve got a functioning Docker installation on your host before you continue.
Getting Started
The Redis image includes both the server component and the official CLI. It’s preconfigured to launch the server with functioning default config values when you start a new container.
Variations of the image are available to cover different Redis versions (5.0 and 6.0) and operating systems (Alpine and Debian). Browse the tag list to find the best option for your environment.
The simplest deployment is as follows:
This starts a new container called redis running Redis 6.0. The -d flag is used to detach from the container. The server will stay up in the background until you stop the container with docker stop redis.
Redis listens on port 6379 by default. The -p flag binds this port to your host. Your applications will be able to access Redis on localhost:6379. Beware that this is insecure though – if your machine is internet-exposed, anyone could access your Redis data!
Data Storage
Redis supports several persistence mechanisms that can save your in-memory database to disk. The data will be saved to the /data directory in the container. As Docker containers are ephemeral, you need to mount a volume to this directory. Otherwise your data will be lost when your container stops.
The –save flag is passed to the Redis server. It configures the persistence strategy to use. This example writes a snapshot of your database every 60 seconds. The operation’s skipped unless 1 database write has occurred since the last snapshot.
A Docker volume called redis-data is created by the -v flag. This will store your data outside the container so it’ll remain accessible after restarts. The volume will persist until it’s deleted by running docker volumes rm redis-data.
Configuring Your Server
The quickest way to add config parameters is to pass CLI arguments to the Redis server as part of your docker run command. This is illustrated by the –save example above. Anything after the image name in docker run will get passed through to the command executed inside the container. In the case of the Redis image, that command is the Redis server.
Using CLI flags quickly becomes repetitive. You can make Redis use a config file by passing a path as the server’s first argument. This file is conventionally located at /usr/local/etc/redis/redis.conf. Use a Docker bind mount to get a redis.conf from your local filesystem mounted into the container:
Place a redis.conf inside redis-conf in your working directory. Docker will mount this path into the container, giving Redis access to the file specified in the docker run command.
Managing Redis Security
The Redis Docker images are configured to run Redis in unprotected mode by default. This makes it easier to access the Redis server from your other Docker containers, using Docker networks. However, it also means anything that can reach your container will have full access to your data.
Protected mode is a Redis feature that only responds to unauthenticated queries issued from your host’s loopback addresses such as localhost. You can enable it by adding protected-mode yes to your redis.conf. When used with a Dockerized installation, this will result in Redis only being accessible within its own container, which is not usually ideal.
You can set up basic password authentication instead by adding requirepass example to your redis.conf. Redis will only accept queries from clients that present the configured password. Redis 6 also supports more fully-featured access control that lets you set up multiple user accounts with differing privileges.
To use authentication, follow the guidance in the preceding section to create a Redis config file and mount it into your container. If you don’t want to set up a password, keep your installation secure by only joining it to the Docker networks needed for your application. Do not bind port 6379 to your host without setting up authentication first.
Using Your Redis Installation
Now that Redis is fully set up, you can move on to accessing it from your clients. If you’re connecting from your host, you can use the Docker container’s IP (visible by running docker inspect redis, adjusted for your container’s name) and port 6379.
To access Redis from another Docker container, it’s best to join both the containers into a Docker network:
Now your application container will be able to reach Redis via port 6379 on the redis hostname. Docker makes container names accessible as hostnames when they share a Docker network.
You can manually interact with your database using the redis-cli binary that’s included in the container image. Start your container in detached mode (-d) so it runs in the background. Then use docker exec to run the redis-cli command:
This will drop you into a Redis CLI session within your container.
Summary
Docker makes it quick and easy to start a new Redis instance without installing the software onto your machine. Use the official Docker image to start your container, then add command flags or mount a config file to suit your needs.
Two things to always keep in mind are storage and security: if you need to use Redis persistence features, you should use a Docker volume to avoid losing your data. Remember that Dockerized Redis defaults to unprotected mode with no authentication, so exposing port 6379 could have disastrous consequences in a worst case scenario.