Containerize Valkey Using Docker and Docker Compose

anjanj
3 min read

This documentation provides a step-by-step guide to containerize Valkey using Docker and Docker Compose. We will define a Dockerfile for building the Valkey container and a docker-compose.yml file to manage the container configuration, volumes, networks, and health checks.

πŸ’‘
The configurations are for development environment only. Do not use in production.

Prerequisites

  1. Docker: Ensure Docker is installed and running on your machine.

     docker --version
    

    This command should output the installed Docker version.

  2. Docker Compose: Ensure Docker Compose is installed and running on your machine.

     docker-compose --version
    

    This command should output the installed Docker Compose version.

  3. Docker Desktop: If you are using a Windows or macOS system, install Docker Desktop which includes Docker Engine, Docker CLI, Docker Compose, and other Docker tools.

Directory Structure

valkey-container/
β”œβ”€β”€ Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ conf/
β”‚   └── valkey.conf
└── data/

Create a Dockerfile

The Dockerfile is used to build the Valkey image. Since we're using an existing Valkey base image and mounting the configuration file via a volume, the Dockerfile is straightforward.

# Use the official Valkey base image
FROM valkey/valkey:latest

# Run the Valkey server with your custom configuration file
CMD ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]

Create a Configuration File (valkey.conf)

Place your custom configuration file in the conf directory. Here is a sample valkey.conf:

# By default, Valkey uses the following default for binding to interfaces on the server: bind 127.0.0.1 -::1
# bind 127.0.0.1 -::1

# Development configuration - comment out in production
bind 0.0.0.0 -::1
protected-mode no

Create a Docker Compose File (docker-compose.yml)

Create a docker-compose.yml file to define the Valkey service and its configuration.

version: '3.8'

services:
  valkey:
    container_name: valkey
    hostname: valkey
    image: valkey/valkey:latest
    build: .
    volumes:
      - ./conf/valkey.conf:/usr/local/etc/valkey/valkey.conf
      - ./data:/data
    command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
    healthcheck:
      test: ["CMD-SHELL", "valkey-cli ping | grep PONG"]
      interval: 1s
      timeout: 3s
      retries: 5
    ports:
      - 6379:6379
    networks:
      - valkey-net

networks:
  valkey-net:
    driver: bridge

Running the Valkey Container

Running the Valkey Container

  1. Navigate to the project directory:

     cd path/to/valkey-container
    
  2. Build the container using Docker Compose:

     docker-compose build
    

    This command will build the Valkey image based on the Dockerfile.

  3. Start the container using Docker Compose:

docker-compose up

This command will start the Valkey container based on the configuration specified in docker-compose.yml.

Verifying the Setup

  • Ensure the container is running correctly:

      docker ps
    

    You should see the Valkey container listed.

  • Check the logs for any errors:

      docker-compose logs valkey
    
  • Test the health of the Valkey service:

      docker inspect --format "{{json .State.Health }}" valkey
    

Test the connection

  1. Connect to Valkey Server: Open a terminal and use the following command to connect to Valkey server:

     valkey-cli -h localhost -p 6379
    

    This command will open a Valkey CLI prompt connected to Valkey server.

  2. Test Valkey Commands: Once connected, you can test various Valkey commands. For example, you can ping the server:

     ping
    

    You should receive a response of "PONG", indicating that the server is running and responsive.

  3. Interact with Valkey: You can execute other Valkey commands as needed. For example:

    • Set a key:

        set mykey "Hello Valkey"
      
    • Retrieve the value:

        get mykey
      
  4. Exit Valkey CLI: To exit the Valkey CLI, type exit or press Ctrl + D.


I trust we all learned something from this blog. If you found it helpful too, give back and show your support by clicking heart or like, share this article as a conversation starter and join my newsletter so that we can continue learning together and you won’t miss any future posts.

Thanks for reading until the end! If you have any questions or feedback, feel free to leave a comment.

πŸ’‘
πŸ‘‹ Kindness is contagious. Please leave your appreciation by commenting on this post! It takesΒ one minuteΒ and is worth it for your career.
0
Subscribe to my newsletter

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

Written by

anj
anj