redis-python-docker-compose

Learn how to set up and run a Redis instance using Docker Compose. This guide provides a simple docker-compose.yml file for Redis, making it easy to manage your Redis container.

Redis Docker Compose Setup

This document outlines how to set up a Redis instance using Docker Compose. Docker Compose is a tool for defining and running multi-container Docker applications. By using a docker-compose.yml file, you can easily configure and manage your Redis service.

Redis Docker Compose Configuration

Below is a sample docker-compose.yml file that defines a single Redis service. This configuration specifies the Redis image to use, sets a container name, configures environment variables, defines network settings, and sets up port mapping and logging options.

version: "3.7"

services:
  redis-server:
    image: redis:7.0
    container_name: redis-server
    environment:
      - DOCKERHUB_DOCS=https://hub.docker.com/_/redis
    networks:
      - app
    ports:
      - 6379:6379
    logging:
      driver: "json-file"
      options:
        max-size: "1m"

networks:
  app:
    name: app

Understanding the Docker Compose File

Service Definition

The services section defines the containers that make up your application. Here, redis-server is our service name.

  • image: redis:7.0: Specifies the Docker image to use for the Redis service. We are using the official Redis image, version 7.0.
  • container_name: redis-server: Assigns a specific name to the container for easier identification.
  • environment:: Sets environment variables within the container. DOCKERHUB_DOCS is used here to link to the official Redis Docker Hub documentation.
  • networks: - app: Connects the Redis service to a custom network named app.
  • ports: - 6379:6379: Maps port 6379 on the host machine to port 6379 inside the container, allowing external access to Redis.
  • logging:: Configures logging for the container. This example sets the driver to json-file and limits log file size to 1MB.

Network Definition

The networks section defines the networks for your services. Here, we define a network named app, which the redis-server service is attached to.

Running Redis with Docker Compose

To run this Redis setup, save the content above as docker-compose.yml in a directory. Then, navigate to that directory in your terminal and run the following command:

docker-compose up -d

This command will download the Redis image if it's not already present and start the Redis container in detached mode (in the background).

Stopping Redis

To stop the Redis container, navigate to the same directory and run:

docker-compose down

External Resources