Database Integration
This section outlines the use of Docker Compose to manage different database technologies.
Setting Up and Configuring Databases
Docker Compose simplifies the setup and configuration of databases, making them readily available for development and testing purposes.
Example:
version: '3.7'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: my_database
MYSQL_USER: my_user
MYSQL_PASSWORD: my_password
ports:
- "3306:3306"
This example demonstrates setting up a MySQL database using the mysql:5.7
image. It defines the root password, database name, user, and password, along with the port mapping. This ensures the database is accessible from the host machine on port 3306.
Creating Databases and Tables
Docker Compose provides flexibility for creating databases and tables through environment variables and commands.
Example:
version: '3.7'
services:
db:
image: postgres:14
restart: always
environment:
POSTGRES_USER: my_user
POSTGRES_PASSWORD: my_password
POSTGRES_DB: my_database
command: bash -c '
psql -U my_user -d my_database -c "CREATE TABLE my_table (id SERIAL PRIMARY KEY, name VARCHAR(255));"
&& psql -U my_user -d my_database -c "INSERT INTO my_table (name) VALUES ('Example Data');"
'
In this example, the command
section executes a series of psql
commands to create the my_table
table within the my_database
and insert some initial data.
Connecting Applications to Databases
Docker Compose facilitates the connection of applications to databases by defining network dependencies and environment variables.
Example:
version: '3.7'
services:
db:
image: mongo:4.4
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: my_user
MONGO_INITDB_ROOT_PASSWORD: my_password
app:
image: my_app:latest
restart: always
depends_on:
- db
environment:
MONGO_URI: mongodb://db:27017/my_database
This example sets up a MongoDB database and an application named app
. depends_on
ensures the application starts after the database is ready. The MONGO_URI
environment variable provides the connection string for the application to connect to the database.
Managing Database Backups and Migrations
Docker Compose can be combined with other tools and scripts for managing database backups and migrations. This often involves utilizing volumes for persistent storage and scripting for backup and migration tasks.
Example:
version: '3.7'
services:
db:
image: postgres:14
restart: always
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: my_user
POSTGRES_PASSWORD: my_password
POSTGRES_DB: my_database
backup:
image: backup-script:latest
restart: on-failure
volumes:
- db-data:/var/lib/postgresql/data
command: backup_script.sh
This example utilizes a volume db-data
to store the database data persistently. The backup
service uses this volume to perform backup operations as defined by the backup_script.sh
script.
Note: The examples provided are for demonstration purposes. They may need adjustments based on your specific database technology, version, and configuration needs.