Connected Python with Redis Using Docker Compose

Project Introduction: Connect Python with Redis Using Docker Compose

This project demonstrates how to connect a Python application with a Redis database using Docker Compose, a powerful tool for running multi-container Docker applications. Redis is a popular in-memory key-value store, often used for caching, real-time analytics, or session management. In this setup:

  • The Python app acts as a client that sends data to and retrieves data from Redis.

  • Redis runs in a separate container.

  • Both containers are orchestrated using a single docker-compose.yml file.

  • No manual networking setup is needed—Docker Compose takes care of connecting both services automatically.

This is a great beginner-friendly example to understand container linking, service communication, and micro-service deployment using Docker.

Connecting Python to Redis

Importing Dependencies:

import redis import time

  • redis: This is the Redis client library for Python, which lets you connect to a Redis server and perform operations like get, set, etc.

  • time: This is used to introduce a delay, ensuring the Redis service is up before the Python script attempts a connection.


2. Delay to Allow Redis to Start:

time.sleep(3)

  • This 3-second sleep gives the Redis container enough time to start up before the Python script tries to connect to it.

  • It's crucial when both services are started simultaneously using Docker Compose.


3. Connect to Redis Container:

r = redis.Redis(host='redis', port=6379)

  • This line connects the Python app to the Redis server.

  • host=’redis’ : This is not an IP—it’s the name of the Redis service defined in your docker-compose.yml. Docker Compose sets up DNS for service discovery.

  • port=6379: The default port on which Redis listens.


4. Store and Ret0rieve Data:

r.set('name', 'Yash')

data value = r.get('name')

  • Sets a key-value pair in Redis where name is the key and 'Yash‘ is the value.

  • Immediately fetches that same key from Redis.


5. Display the Value:

print("Stored value from Redis:", value.decode())

  • Redis returns bytes, so decode() is used to convert it to a human-readable string.

  • This prints:
    Stored value from Redis: Yash

Dockerfile

FROM python: 3.10-alpine

  • Uses the official lightweight Python 3.10 image (Alpine-based) as the base for the container.

  • Alpine is great for small, fast images.


WORKDIR /app

  • Sets the working directory inside the container to /app.

  • All subsequent commands will run from this directory.


COPY main.py .

  • Copies the main.py file from your local project directory into the container’s /app directory.

RUN pip install redis

  • Installs the redis Python package, which allows Python code to communicate with a Redis server.

CMD ["python", "main.py"]

  • Specifies the command that will run when the container starts.

  • It runs your script using Python.

version: ‘3’

  • Specifies the version of Docker Compose syntax being used (v3 is compatible with modern Docker engines).

services

This section defines all the containers (services) that will be launched.


redis

  • image: redis-alpine
    Pulls the official lightweight Redis image based on Alpine Linux.

  • container_name: redis_server
    Names the container for easier reference and logs.


app

  • build: ./app
    Builds the Python container using the Dockerfile located in the app directory.

  • container_name: python_client
    Gives the Python container a custom name.

  • depends_on: - redis
    Ensures the Redis service starts before the Python app, helping prevent connection errors during startup.

What’s happening?

redis_server

  • This service starts the Redis container.

  • You can see it loading modules like:

    • search, timseries, and ReJSON
  • It registers its internal configurations and finally logs:

    Ready to accept connections tcp

    This means the Redis server is up and running.


python_client

  • The Python container connects to the Redis server, stores the key-value pair name:Yash, and retrieves it.

    It prints:

    Stored value from Redis: Yash

  • Then, the container exits cleanly with:

    exited with code 0

    This indicates successful execution without errors.

0
Subscribe to my newsletter

Read articles from Yash Shankhwalkar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yash Shankhwalkar
Yash Shankhwalkar