Containerize Valkey Using Docker and Docker Compose
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.
Prerequisites
Docker: Ensure Docker is installed and running on your machine.
docker --version
This command should output the installed Docker version.
Docker Compose: Ensure Docker Compose is installed and running on your machine.
docker-compose --version
This command should output the installed Docker Compose version.
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
Navigate to the project directory:
cd path/to/valkey-container
Build the container using Docker Compose:
docker-compose build
This command will build the Valkey image based on the
Dockerfile
.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
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.
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.
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
Exit Valkey CLI: To exit the Valkey CLI, type
exit
or pressCtrl + 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.
Subscribe to my newsletter
Read articles from anj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by