Setting up MySQL and Redis with Docker Compose
Docker has transformed the way developers deploy applications, offering an efficient method to package and distribute software. One common use case involves setting up a MySQL database alongside a Redis service. MySQL serves as a reliable relational database management system, while Redis acts as a high-performance in-memory key-value store. With Docker and Docker Compose, you can get both of these services up and connected in just a few steps. This article will guide you through creating a Docker environment that includes MySQL and Redis, giving you a smooth and reliable setup.
Prerequisites
In this article, we will show you how to create a functional Docker environment. However, for a more in-depth understanding and detailed insights, we highly recommend referring to the following articles:
Installing Docker: Installing Docker Engine and Docker Compose
Docker images basics: Your first Docker Hub image from scratch
Docker containers basics: Docker compose introduction
Docker commands: Comprehensive cheatsheet of Docker commands
Setting up your project structure
Before getting into the Docker setup, start by organizing your project files. First, create a dedicated directory on your machine for the project; you might call it docker-db
. This folder will hold your docker-compose.yml
file, where you’ll define services, networks, and volumes. Keeping everything in one place makes the project easy to manage and update as needed. This simple organization step will help keep your setup clean and manageable from the start.
We also suggest creating individual folders for each service's configuration files. This approach not only helps keep things organized but also allows you to customize each service more easily without introducing unnecessary complexity.
Writing the Docker Compose file
The heart of this setup lies in the docker-compose.yml
file. This Docker Compose file sets up MySQL and Redis services, each with a specific version, container name, hostname, and mapped ports. Environment variables configure MySQL's root password, and named volumes provide persistent storage for both MySQL and Redis data.
services:
mysql:
image: mysql:8.4.3
container_name: mysql
hostname: mysql.hibit
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root_password
volumes:
- ./mysql8/conf.d:/etc/mysql/conf.d
- mysql_data:/var/lib/mysql # Named volume for persistent storage
redis:
image: redis:7.4.1
container_name: redis
hostname: redis.hibit
ports:
- "6379:6379"
volumes:
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
- redis_data:/data # Optional: named volume for Redis data persistence
volumes:
mysql_data:
name: mysql_data
redis_data:
name: redis_data
This configuration provides a convenient, isolated environment for MySQL and Redis, with custom settings and data persistence enabled for each service.
Building and running Docker Containers
With your docker-compose.yml
file ready, the next step is to build and run your containers. Open your terminal, navigate to the project directory, and execute the following command:
docker-compose up -d
The -d
flag runs the containers in detached mode, allowing them to operate in the background. After executing this command, Docker will pull the necessary images if they aren’t already on your machine and start the services. You can check the status of your containers using:
docker-compose ps
This command provides a list of running containers, ensuring both MySQL and Redis are active and ready for connections.
Connecting MySQL and Redis
Now that both services are running, you might want to connect them. While MySQL handles relational data, Redis can be used for caching or session storage, enhancing performance. To connect the two, you can use a programming language of your choice, such as Python, Node.js, or PHP. In your application, you would configure the connection details for both MySQL and Redis, ensuring they can communicate as needed.
You may need to update your local hosts file to ensure that your services can be accessed using the specified hostnames. To do this, open your hosts file and add the following lines:
127.0.0.1 mysql.hibit # replace with hostname chosen in Docker Compose configuration
127.0.0.1 redis.hibit # replace with hostname chosen in Docker Compose configuration
This step maps the hostnames mysql.hibit
and redis.hibit
to your local machine, allowing you to connect to the MySQL and Redis services using these friendly names instead of IP addresses. Updating the hosts file simplifies access and improves the overall usability of your Docker environment.
Conclusion
Creating a Docker image with MySQL and Redis is a practical way to streamline your application development. By following the steps outlined, you can efficiently set up both services using Docker Compose, making your workflow much smoother. As you continue to work with Docker, you'll find it offers a flexible solution for managing complex applications, ensuring you can focus more on building great software rather than worrying about deployment. With a bit of practice, you'll be well on your way to mastering Docker for your projects.
Credits
Official GitHub: https://github.com/hibit-dev/db-containerization
0 Comments