Docker Redis Configuration

2/13/2022by admin

Introduction

Build the Redis with Docker Compose. Now save all of the settings and then execute the following docker-compose command to spin-up the container, after it is built from the YAML configuration: 1. Docker-compose up -build. The container should now be running in the foreground of the terminal window. Modifying a file on disk (eh) Setting the password in the clear for the docker run command (ew) It would be nice if we could set an environment variable for the password, as then we can easily integrate password protection and provide our users a modicum of security. See dokku/dokku-redis#58 for (a lot less) detail. Mar 21, 2019 The key point is the docker container, which allows me to run Redis on Windows, even if the Redis project does not officially support Windows. The Redis image is pulled from the Docker Hub, and you should get a similar figure in the terminal as seen in Figure 1, saying the Redis ‘server is now ready to accept connections on port 6379’! May 06, 2021 docker run -d -name my-redis -p 6379:6379 redis. After launching it, you can access it via SSH with the following command: docker exec -it my-redis bash. You can access the Redis CLI running the following command in the SSH instance: redis-cli Creating a container with custom redis.conf. Dec 13, 2017 docker exec redis-1 redis-cli cluster nodes However, you cannot access this cluster from the outside network since we do not expose any ports to host. Therefore, you should run your application.

This tutorial will explain how to run Redis with Docker Compose. Compose is designed to run multi-container Docker applications using a YAML file for configuring the application’s services. Once configured, all the functions can then be created and started with a single command. This article will provide precise instructions on how to build a Docker container, run Redis with Docker Compose and how to deal with various errors.

Prerequisites

Docker must be installed on the host machine or server running the Redis container. Execute thedocker -v command to confirm the currently installed Docker engine version.

Use Docker pull to download the Redis image

The following docker pull command can be used to download the image before building the container:

docker pull bitnami/redis

Note that the official Redis and Bitnami images currently uses the Debian 9 “stretch” version of Linux.

Run Redis with Docker

The default command from the Docker hub profile for Bitnami Redis allows the use of an empty password, as shown in the following example:

docker run --name redis_cont
-eALLOW_EMPTY_PASSWORD=yes
bitnami/redis:latest

NOTE: The ALLOW_EMPTY_PASSWORD setting should only be used for development purposes.

Bind the Redis port when running Docker

The 6379 Redis port can also be bound to the host machine as follows:

As shown in the following representation, Docker will download the image and run the container in the foreground:

Bind a local volume for persistent Redis data

Following is another run example that bind mounts a local directory to the container’s volume:

docker run -p6379:6379-d
-v$PWD/redis-data:/bitnami/redis/data
--name redis_cont
bitnami/redis:latest # <-- Redis image

Note that the above command will allow for persistent data to remain even if the container is destroyed.

Start the Redis server

The redis-server command can be used to start the Redis server, or the redis-server -v command can be used to obtain the version number for the Redis server installed on the container. Execute the following ping command to get a response from the server:

Docker error – invalid reference format

Docker

If the previous docker run command returns an invalid reference format error, try removing the -name tag or use the redis:latest command to pull from the latest image, as shown here:

docker run -p6379:6379/tcp -d redis:latest

Stop the Docker container

To stop the container, execute the docker stop command followed by the container’s ID, as shown here:

NOTE: Be certain to execute the above command in a terminal window that isn’t running the container in the foreground.

Redis server cannot create server

If a “Could not create server TCP listening socket *:6379: bind: Address already in use” error appears, try stopping and then removing the container.

If ps is installed with the image’s Linux distro, use ps -ef grep redis command to search for all of the Redis processes and then execute the kill -9 command followed by the process ID to stop the operation.

Stop Redis on the host machine

In this scenario, there is typicaly a port conflict due to another instance of Redis running on the host machine. To fix this issue, exit out of the interactive execution of the container and stop the Redis server.

On a Homebrew installation of Redis on MacOS, stop the service with the following command:

brew services stop redis

If Redis is still running, try stopping the container with the docker stop command and then removing the container with the docker rm command. Now run the container again, changing the host machine’s port as follows:

If all else fails, connect to the interactive client for Redis with the redis-cli command and then use the shutdown command.

In Linux, the following command may also be effective:

/etc/init.d/redis-server stop

Run Redis with Compose

There aren’t any environment variables that can be set using the docker-compose.yml file. However, some functions can be modified with the redis.conf file.

Create a config file for Redis

Docker Redis Configuration

Redis Docker Image Configuration

To inject a custom configuration file for Redis when building the Docker container, create the redis.config file somewhere in the project directory and download a version of the config file that matches the version found on the Redis website.

The following configurations will specify a bound IP address and port:

Create a Docker-Compose file for Redis

Be certain to specify the relative path for the redis.conf file and for the Redis data. A password can be specified by invoking the redis-server --requirepass command under the command: field, as shown here:

version: '3.2'
services
:

redis
:
image
: 'redis:alpine'

command
: redis-server --requirepass sOmE_sEcUrE_pAsS

ports
:
- '6379:6379'

volumes
:
- $PWD/redis-data:/var/lib/redis
- $PWD/redis.conf:/usr/local/etc/redis/redis.conf

environment
:
- REDIS_REPLICATION_MODE=master

networks
:
node_net
:
ipv4_address
: 172.28.1.4
# networking for the Redis container
networks
:
node_net
:
ipam
:
driver
: default
config
:
- subnet
: 172.28.0.0/16

The above networks: fields will assign the container an IP address of 172.28.1.4. The 'redis-alpine' image will install Redis on a barebones “Alpine” distro of Linux.

Build the Redis with Docker Compose

Now save all of the settings and then execute the following docker-compose command to spin-up the container, after it is built from the YAML configuration:

The container should now be running in the foreground of the terminal window. Open another terminal instance, or a new tab, and use the docker ps command to have Docker return the container’s alpha-numeric ID.

Run commands in the Redis container

With the container ID obtained, use the ID to execute commands and connect to the Docker-Compose container interactively. Execute the following docker exec -it command to get access to its bash shell:

docker exec-it{CONTAINER_ID}sh

NOTE: When executing the above command, be absolutely certain to replace {CONTAINER_ID} with the actual ID of the container.

Redis - Official Image Docker Hub

Now type exit to disconnect from the container. To shutdown the container, execute the docker-compose down command in the same directory where the docker-compose.yml file is located.

Conclusion

This tutorial explained how to run Redis with Docker Compose. This article covered how to use Docker pull to download the Redis image, how to run Redis with Docker, how to bind the Redis port when running Docker, bind a local volume for persistent Redis data and start the Redis server. The tutorial also covered how to remedy errors, stop the Docker container, run Redis with Compose and how to create a config and various other files for Redis. Remember that it is absolutely critical to replace the {CONTAINER_ID} placeholder with the actual ID of the container when executing the docker exec -it command to get access to its bash shell.

In this short tutorial, I want to show you how to set up a simple Redis instance using Docker Compose. We will connect to it and test it using Python and the redis package.

Redis in docker container with custom file redis.conf doesn

Software

Since we will use Docker Compose to create the Redis instance, you must first install docker. Check out https://docs.docker.com/engine/installation for further information.
To run thy Python code, you have to first install Python and the pip package manager.
To download the PyPi package for Redis, open a terminal and type

Setting up Redis

Docker compose makes it very easy to set up and run a Redis instance. We will directly use the redis image from docker-hub.
To configure the Docker image, create a new directory for this project and add the following docker-compose.yml file to it:

As mentioned above, we use the redis image from docker hub directly. In the command section, we tell Docker to startup a redis server using the config file from /usr/local/etc/redis. To configure Redis, I added a volume that points to a custom redis.conf file which will then be used by redis instead of the default config.

Configuring Redis

You can get the default redis.conf file from http://download.redis.io/redis-stable/redis.conf
Before I could connect to the Redis instance from Python, I had to comment the following line in the config:

by adding a # in front of it. For further information about Redis configuration checkout its documentation.

Starting the Redis server

With both the docker-compose.yml and redis.conf files set up, we can now use Docker Compose to startup the Redis server:

If you run this command for the first time, docker has to pull the Redis image and will require some extra time to get started. If everything is set up correctly, you should see a couple of success messages from Redis, ending with something like Ready to accept connections.

Connecting to Redis from Python

Docker

What Is The Location Of Redis.conf In Official Docker Image ...

With Redis up and running, it’s time to send some commands to it. Since we will use a Python client, I added a redis-client.py file to the directory we created before:

Redis Docker Configuration File

Here we are simply adding and removing some keys to test the most basic Redis commands. Feel free to play around with this file and try out some more advanced tasks. After you are done testing, simply terminate the process that runs the Redis image. If you try to execute the Python code now, you should run into an error, since there is no Redis server listening on localhost on Port 6379.

That’s it for this quick introduction. While this example is very simplistic, you can easily build up from it by adjusting the redis.conf file or adding more services to the docker-compose.yml file. For example, you could set up a second service that starts up a web server which you can then, in turn, connect to the Redis instance.

Cached

If you have any questions, problems or feedback, please let me know.

Comments are closed.