How to Use Multiple Databases with docker-compose?


Introduction

Docker is a popular platform for deploying and running applications in a containerized environment. It provides an efficient way to package, distribute and run applications with all their dependencies. One of main advantages of using Docker is that it enables use of multiple databases in an efficient way using docker-compose.

Docker-compose is a tool for defining and running multi-container Docker applications. It allows you to define configuration of different containers in a single YAML file, which can be used to start, stop and manage containers. In this article, we will discuss how to use multiple databases with Docker-compose.

Prerequisites

Before we start, you should have following installed on your machine −

  • Docker

  • Docker-compose

You should also have a basic understanding of Docker and Docker-compose.

Using Multiple Databases

When building applications, it is common to use multiple databases to store different types of data. For example, you might have one database for user authentication and another for storing product information. In a Dockerized environment, you can use multiple databases by creating separate containers for each database and linking them together.

To use multiple databases with Docker-compose, you need to define different database containers in YAML file. Let’s take a look at an example −

version: '3'
services: 
db1: 
image: 
mysql environment: 
MYSQL_ROOT_PASSWORD: mypassword 
db2: 
image: 
postgres 
environment: 
POSTGRES_PASSWORD: mypassword

In this example, we are defining two database containers: db1 and db2. We are using mysql image for db1 and postgres image for db2. We are also setting root password for db1 and password for postgres for db2.

Linking Containers

To link containers together, we need to use links option in Docker-compose. links option allows you to define which containers should be linked to which. Here is an example −

version: '3'
services: 
app: 
build: . 
links: 
- db1 
- db2 
db1: 
image: mysql 
environment: 
MYSQL_ROOT_PASSWORD: mypassword 
db2: 
image: postgres 
environment: 
POSTGRES_PASSWORD: mypassword

In this example, we are defining an app container, which is linking to both db1 and db2. This means that app container can access both databases. You can also specify alias for each container using links option. For example −

version: '3'
services: 
app: 
build: . 
links: 
- db1:mysql
- db2:postgres 
db1: 
image: mysql 
environment: 
MYSQL_ROOT_PASSWORD: mypassword 
db2: image: postgres 
environment: 
POSTGRES_PASSWORD: mypassword

In this example, we are specifying alias for each container. db1 container is aliased as mysql and db2 container is aliased as postgres.

Using Multiple Networks

Another way to use multiple databases is to create separate networks for each database. This is useful if you want to isolate different databases from each other. Here is an example −

version: '3'
services: 
app: 
build: . 
networks: 
- db1 
- db2 
db1: 
image: mysql 
environment: 
MYSQL_ROOT_PASSWORD: mypassword 
networks: 
db1: 
db2: 
image: postgres 
environment: 
POSTGRES_PASSWORD: mypassword 
networks: 
db2:
networks: 
db1: 
db2:

In this example, we are defining two networks: db1 and db2. We are also defining an app container, which is connected to both networks. db1 container is connected to db1 network and db2 container is connected to db2 network. This means that app container can access both databases through respective networks.

Using Multiple Volumes

When using multiple databases, it is important to keep data for each database separate. One way to do this is to use separate volumes for each database. Here is an example −

version: '3'
services: 
app: 
build: . 
volumes: 
- db1_data:/var/lib/mysql 
- db2_data:/var/lib/postgresql/data 
db1: 
image: mysql 
environment: 
MYSQL_ROOT_PASSWORD: mypassword 
volumes: 
- db1_data:/var/lib/mysql 
db2: 
image: postgres 
environment: 
POSTGRES_PASSWORD: mypassword 
volumes: 
- db2_data:/var/lib/postgresql/data
volumes: 
db1_data: 
db2_data:

In this example, we are defining two volumes − db1_data and db2_data. We are also defining an app container, which is using both volumes to store data for each database. db1 container is using db1_data volume to store its data and db2 container is using db2_data volume to store its data.

Best Practices for Using Multiple Databases with Docker-compose

When using multiple databases with Docker-compose, there are some best practices you should follow to ensure your application runs smoothly −

  • Use version control − Always use version control for your Docker-compose files. This will allow you to track changes and roll back to previous versions if necessary.

  • Use environment variables − Use environment variables to store sensitive information like passwords and API keys. This will make it easier to update your passwords without having to change your Docker-compose file.

  • Use containers for each component − When using multiple databases, it's best to use separate containers for each database component. This will allow you to easily scale your application and make updates without affecting other components.

  • Use separate networks for each database − Using separate networks for each database will allow you to isolate different databases and prevent conflicts.

  • Use volumes for persistent data − When using multiple databases, it's important to use separate volumes for each database to keep data for each database separate.

  • Use health checks − Use health checks to ensure that your containers are running and healthy. This will allow you to quickly identify and fix any issues with your application.

Examples of Using Multiple Databases with Docker-compose

Let's take a look at some examples of using multiple databases with Docker-compose −

WordPress with MySQL and Redis

version: '3'
services: 
db: 
image: mysql:5.7 
volumes: 
- db_data:/var/lib/mysql 
restart: always 
environment: 
MYSQL_ROOT_PASSWORD: mypassword 
MYSQL_DATABASE: wordpress 
MYSQL_USER: wordpress 
MYSQL_PASSWORD: wordpress
redis: 
image: redis:alpine
restart: always
wordpress: depends_on: - db - redis image: wordpress:latest ports: - "8000:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: wordpress WORDPRESS_CACHE_HOST: redis
volumes: db_data:

In this example, we are defining a WordPress application with two databases: MySQL and Redis. db container is using MySQL image and redis container is using Redis image. WordPress container is linking to both db and redis containers and is using environment variables to connect to databases.

Django with Postgres and Redis

version: '3'
services: 
web: 
build: . 
command: python manage.py runserver 0.0.0.0:8000 
volumes: 
- .:/code 
ports: 
- "8000:8000" 
depends_on: 
- db 
- redis
db: 
image: postgres 
restart: always 
environment: 
POSTGRES_USER: myuser 
POSTGRES_PASSWORD: mypassword 
POSTGRES_DB: mydb
redis: 
image: redis 
restart: always

In this example, we are defining a Django application with two databases − Postgres and Redis. web container is using a custom image and is linked to both db and redis containers. db container is using Postgres image and redis container is using Redis image.

Conclusion

In this article, we discussed how to use multiple databases with Docker-compose. We covered how to link containers together, how to use multiple networks, and how to use multiple volumes. By using these techniques, you can efficiently use multiple databases in a Dockerized environment.

Updated on: 23-Mar-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements