Introduction
Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It is known for its high performance, scalability, and versatility. Redis stands for Remote Dictionary Server.
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define an entire application stack, including services, networks, and volumes, in a single file called docker-compose.yml. With a single command, you can then spin up your entire application stack, simplifying the process of managing and deploying multi-container Docker applications.
Prerequisites
- Up and running ubuntu 22.04 LTS machine.
- Basic knowledge in linux commands.
- Internet connectivity.
- Docker should installed on your machine.
To install Docker on ubuntu 22.04 LTS, We can use the given link.
In this post, We will deploy the Redis database docker container with docker compose on Ubuntu 22.04 LTS machine.
Step 1: Creating docker-compose.yml
To deploy a Redis container using Docker Compose, you’ll need to create a docker-compose.yml file that defines the service. Here’s a simple example:
To create a docker-compose.yml
sudo nano docker-compose.yml
Copy and paste the followings configuration.
version: '3'
services:
redis:
image: "redis:latest"
container_name: "my-redis-container"
ports:
- "6379:6379"
volumes:
- "redis_data:/data"
volumes:
redis_data:
Save and exit from the nano text editor.
Step 2: Deploy Redis Container
We are good to deploy the Redis container with persistence volume, We need to execute the given command.
sudo docker-compose up -d
The -d flag runs the containers in the background, It will take few sec or mins to pull the image and deploy the Redis.
Step 3: Validate the Container
To check the running containers launched by Docker Compose, you can use the following command:
sudo docker-compose ps
This command provides a summary of the status of each service defined in your docker-compose.yml
file. It shows information such as the service name, container ID, status, ports, and names.
Here’s an example of what the output might look like:
Name Command State Ports
----------------------------------------------------------------------------------------
redis docker-entrypoint.sh redis ... Up 0.0.0.0:6379->6379/tcp
Step 4: Logging
Some time we need to also check the real time logs, So we can use the given commands.
sudo docker-compose logs -f redis
Step 5: Redis connection PHP
To connect to a Redis database using PHP, you can use the redis extension. Here’s a simple example:
Install redis’s PHP module by using given command.
pecl install redis
Here is the refence to test PHP based connection with Redis server.
<?php
$redis = new Redis();
// Replace '127.0.0.1' and '6379' with your Redis server's host and port
$redis->connect('127.0.0.1', 6379);
echo "Connection to server successful!\n";
// Simple set and get example
$key = 'example_key';
$value = 'Hello, Redis!';
$redis->set($key, $value);
$result = $redis->get($key);
echo "Value for key '{$key}': {$result}\n";
// Close the connection
$redis->close();
?>
Make sure replace your Redis’s creds before saving your PHP code :).
Step 6: Destroy Redis Container
To destroy (stop and remove) the Redis container launched with Docker Compose, you can use the following command in the directory where your docker-compose.yml file is located:
sudo docker-compose down
This command stops and removes all the containers, networks, and volumes defined in your docker-compose.yml
file.
The down command stops and removes the containers but retains the data volumes by default. If you want to remove the volumes as well, you can use the -v
option:
sudo docker-compose down -v
Make sure you are in the correct directory containing your docker-compose.yml
file when running these commands. This ensures that Docker Compose identifies the correct configuration file.
After running the docker-compose down command, you can use the docker-compose ps command to verify that the containers are no longer running. The output should be empty, indicating that no containers are currently running.
Remember that this command will stop and remove all the services defined in your docker-compose.yml
file, not just the Redis service. If you only want to remove a specific service, you can specify the service name:
docker-compose down -v <service_name>
Replace <service_name>
with the actual name of the service you want to remove.
Conclusion
We have successfully deployed Redis database server on docker’s container using docker compose on ubuntu 22.04 LTS machine, If you still have questions, please post them in the comments section below.