Docker Compose for Database Containers: Best Practices
Last Updated :
23 Jul, 2025
Docker Compose is a nice way to easily handle any multi-container Docker application. Best practices of containers for databases are necessary to ensure reliability, security, and performance. This article covers key strategies for managing database containers using Docker Compose and therefore is a strong foundation for deploying and maintaining databases in an environment with containers.
Primary Terminologies
- Docker Compose: It is a tool that allows defining and running multi-container Docker applications. It provides users with an ability to manage the whole application lifecycle by just a single command, employing a YAML file for configuring services, networks, and volumes.
- Database Container: This is a database engine that is encapsulated in a Docker container instance, be it MySQL, PostgreSQL, or MongoDB, these containers execute the isolated instance of databases, making it easy to deploy, scale, and administrate.
- Volumes: Docker volumes are used to persist the data that is created with and consumed by Docker containers. For database containers, that helps in ensuring the data remains intact even when the container is restarted or removed.
- Networking: In Docker Compose, it is configuring the way communication is organized between containers. For database containers, this refers to the manner in which the database container communicates with other application services.
- Environment Variables: Key-value pairs defined in the Docker Compose file that configure container settings, for example, passwords for databases, user credentials, and connection details
Step-by-Step Process: Setting Up and Managing a Database Container with Docker Compose
Step 1: Install docker
Install docker
sudo yum -y install docker
Now start and enable docker daemon
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Step 2: Install docker compose
Install docker compose
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Step 3: Define Environment Variables Securely
Environment variables, such as database credentials, should be managed securely. Instead of hardcoding sensitive information directly in the docker-compose.yml file, consider using a .env file:
Create a .env file in the project directory:
touch .env
- Add the environment variables to the .env file:
MYSQL_ROOT_PASSWORD=example_root_password
MYSQL_DATABASE=example_db
MYSQL_USER=example_user
MYSQL_PASSWORD=example_password
Step 4: Create a docker-compose.yml File
- In the docker-compose.yml file, define the database service. This example sets up a MySQL database
version: '3.3'
services:
db:
image: mysql:latest
container_name: my_database
env_file:
- .env
volumes:
- db_data:/var/lib/mysql
networks:
- app_network
ports:
- "3306:3306"
restart: unless-stopped
healthcheck:
test: ["CMD-SHELL", "mysqladmin ping -h localhost"]
interval: 30s
timeout: 10s
retries: 5
volumes:
db_data:
driver: local
networks:
app_network:
driver: bridge
Step 5: Start the Services with Docker Compose
- With your docker-compose.yml configured, start the services using Docker Compose:
docker-compose up -d
Step 6: Verify the Database Container
To ensure that the database container is running correctly, you can use the following command
docker ps
Step 7: Connect to the Database
Before going to connect install MySQL by using following command
sudo yum install mysql -y
You can connect to the MySQL database container using the MySQL command-line client from another container in the same network or directly from your host machine.
Access MySQL inside the Docker container:
docker exec -it my_database mysql -u root -p
Show Databases
To list all databases:
Select a Database
- To switch to a specific database:
USE your_database_name;
Replace your_database_name with the name of the database you want to use
Insert Data
- To insert data into a table:
INSERT INTO example_table (name) VALUES ('Sample Data');
Show Tables
- To list all tables in the currently selected database:
SHOW TABLES;
Exit MySQL
- To exit the MySQL monitor:
EXIT;
Step 8: Stop the MySQL Container
docker-compose down
Best Practices for Managing Database Containers
- Data Persistence: Always use Docker volumes to persist your database data this will keep the data safe even if the containers are stopped or removed.
- Secure Configuration: Make use of environment variables, and .env files, for handling sensitive information like passwords and API keys, never hardcode these types of values directly in the docker-compose.yml file.
- Networking Configuration: Keep your database container on a private network, which is open only to trusted services. Make database ports public if, and only if, necessary.
- Backups: Regularly back up the data in your database with scheduled jobs that dump the content of your database and store it outside of your container environment.
- Monitoring and Logging: Monitor and log activities in and around your database containers for performance metrics, error logs, and access patterns
Conclusion
In this article, we have covered the best practices for managing MySQL databases with Docker Compose. We configured a docker-compose.yml file to set up a MySQL container with environment variables to manage credentials securely, map ports for external access, and health checks for reliability. We handled connection issues by showing how to use host IP and resolve issues about authentication plugin errors. We also connected to MySQL from within Docker and from outside systems, and we covered basic SQL commands used for database management. Some troubleshooting tips were also detailed in the document, which might come in handy in resolving common problems such as client upgrades or changing authentication methods, by doing this, you will now be able to handle your MySQL databases within Docker in a more secure, scalable, and usable way. The MySQL and Docker documentation will help you with more assistance
Similar Reads
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle, from planning and coding to testing, deploying, and monitoring.The main idea of DevOps is to i
9 min read
Introduction
What is DevOps ?DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read
DevOps LifecycleThe DevOps lifecycle is a structured approach that integrates development (Dev) and operations (Ops) teams to streamline software delivery. It focuses on collaboration, automation, and continuous feedback across key phases planning, coding, building, testing, releasing, deploying, operating, and mon
10 min read
The Evolution of DevOps - 3 Major Trends for FutureDevOps is a software engineering culture and practice that aims to unify software development and operations. It is an approach to software development that emphasizes collaboration, communication, and integration between software developers and IT operations. DevOps has come a long way since its in
7 min read
Version Control
Continuous Integration (CI) & Continuous Deployment (CD)
Containerization
Orchestration
Infrastructure as Code (IaC)
Monitoring and Logging
Microsoft Teams vs Slack Both Microsoft Teams and Slack are the communication channels used by organizations to communicate with their employees. Microsoft Teams was developed in 2017 whereas Slack was created in 2013. Microsoft Teams is mainly used in large organizations and is integrated with Office 365 enhancing the feat
4 min read
Security in DevOps