Run Redis with Docker Compose

Introduction

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    \
    -e ALLOW_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:

docker run -p 6379:6379 –name redis_cont -d redis

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

Screenshot of Redis running in a Docker container standalone detached

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 -p 6379: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:

redis-cli ping

Docker error – invalid reference format

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 -p 6379: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:

docker stop {CONTAINER_ID}

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:

docker run -p 6380:6380/tcp -d redis:latest

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

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:

bind 172.28.1.4
protected-mode yes
port 6379

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.

Screenshot of Sublime text editor editing docker-compose file for Redis

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:

docker-compose up --build

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.

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.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.