# How to use Redis in Docker Containers

Redis is among the most popular Docker images and is often used as a lightning-fast in-memory database, functioning as a cache, session store, or message broker. In this article, we show you step-by-step how to use Redis with Docker and connect it with other [Docker containers](https://www.ionos.co.uk/digitalguide/server/know-how/docker-container/) or external applications.

## Advantages of running Redis in Docker

- **Fast deployment** thanks to pre-configured images
- **Portability** across different environments
- **Easy scaling** and automation with [Docker Compose](https://www.ionos.co.uk/digitalguide/server/configuration/docker-compose-tutorial/) or [Kubernetes](https://www.ionos.co.uk/digitalguide/server/know-how/what-is-kubernetes/)
- **Good isolation** for development, testing, and production environments
- **Easy integration** into microservice architectures

## Prerequisites

To use [Redis](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/what-is-redis/) in Docker, you need:

- A [Linux server with Docker installation](https://www.ionos.co.uk/digitalguide/server/know-how/installing-and-running-docker-on-a-linux-server/) (e.g., Ubuntu 24.04, Debian 12, or AlmaLinux 9)
- Basic knowledge of using the command line
- Optional: Access without `sudo`, by membership in the Docker group

## Step 1: Start Redis Docker Container

Use the following command to start a basic **Redis Docker container** that stores its data persistently:

```bash
sudo docker run --name my-redis-container -d redis
```

The official [Redis image](https://hub.docker.com/_/redis/ "Redis Image website") from Docker Hub uses port 6379 by default and is ready to use immediately.

## Step 2: Connect Redis Docker instance container to container

Use a custom network to connect your **Docker Redis server** instance with other containers:

```bash
docker network create redis-net
docker run --name my-redis-container --network redis-net -d redis
docker run --name my-redis-client --network redis-net -it redis redis-cli -h my-redis-container
```

This allows Redis to be seamlessly integrated with backend services, microservices, or admin tools without using the outdated --link option.

## Step 3: Allow external access to Redis Docker Container

If you want to use Redis not only internally but also externally (e.g., via a remote server), enable port forwarding:

```bash
docker run --name my-redis-container -p 7001:6379 -d redis
```

Access from the client:

```bash
redis-cli -h [host-IP or domain] -p 7001
```

Note Open the port in your firewall and secure your instance with a password in the `redis.conf`.

## Step 4: Use custom `redis.conf` in the container

You can provide your own configuration for the **Docker Redis server**:

```bash
docker run --name my-redis-container \
    -v /data/myredis/redis.conf:/usr/local/etc/redis/redis.conf \
    redis redis-server /usr/local/etc/redis/redis.conf
```

This allows for custom settings such as authentication (`requirepass`), memory limits, or replication.

## Redis Docker setup with Docker Compose

For larger projects, it is recommended to use Docker Compose:

```bash
version: '3'
services:
    redis:
        image: redis
        ports:
            - "6379:6379"
        volumes:
            - redis-data:/data
volumes:
    redis-data:
```

Start your environment with:

```bash
docker compose up -d
```

## Best practices for Redis Docker server

- Enable requirepass to secure your Redis instance
- Use TLS/SSL for encrypted communication
- Store data in Docker volumes for persistent storage
- Monitor containers with docker logs, Redis CLI, or monitoring tools
- Keep Redis and Docker images regularly updated

## Conclusion

A Redis Docker container can be set up in a matter of minutes and is ideal for local development and production infrastructures. Thanks to the official Redis image, clear networking concepts, and easy configuration, Redis can quickly connect with other Docker containers and operate securely. With Docker Compose, your own `redis.conf`, and best practices, you can get the most out of your setup.


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/server/know-how/redis-in-docker-containers/](https://www.ionos.co.uk/digitalguide/server/know-how/redis-in-docker-containers/) for AI/LLM consumption.