Setting Up Multiple Database Connections in Laravel Using Docker
In this guide, we’ll walk through the process of setting up multiple database connections in a Laravel project using Docker. By the end of this tutorial, you’ll be able to organize your Docker and database configuration files efficiently and ensure smooth database connections.
Step 1: Create a New Folder in Your Laravel Project
First, navigate to your Laravel project directory. To keep things organized, create a new folder named myFolder
to store Docker and database configuration files.
mkdir myFolder
Step 2: Create New Environment and Configuration Files
Inside the myFolder
folder, create a new .env
file to manage environment variables such as DB_PORT
, DB_USERNAME
, and DB_HOST
. This file will help you define the necessary database credentials and connection settings.
Additionally, create a .yml
file, for example, docker-compose.yml
, to define Docker services, networks, and volumes.
Step 3: Docker Compose Configuration
Open the docker-compose.yml
file and configure the services according to your requirements. Here, you'll specify the database services using appropriate images (e.g., MySQL, PostgreSQL). Make sure to set the environment variables in the Docker service definition to match those in the .env
file.
Example docker-compose.yml
:
version: '3.8'
services:
app:
image: laravel:latest
container_name: laravel_app
ports:
- "8000:8000"
volumes:
- .:/var/www/html
depends_on:
- mysql
- postgres
mysql:
image: mysql:5.7
container_name: mysql_db
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: laravel_db1
MYSQL_USER: user
MYSQL_PASSWORD: password
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
postgres:
image: postgres:13
container_name: postgres_db
environment:
POSTGRES_DB: laravel_db2
POSTGRES_USER: user
POSTGRES_PASSWORD: password
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
mysql_data:
postgres_data:
Step 4: Starting Docker Containers
Navigate to the directory where your docker-compose.yml
file is located and run the following command to start the containers in detached mode:
docker-compose up -d
To ensure everything is running smoothly, check the status of the containers with:
docker ps
Step 5: Connect the Database to Dbvear or Any Other Database Tool
Now, connect your database tool (e.g., Dbvear) using the username, port, and other credentials you specified in your .env
file. This will allow you to interact with your databases through an intuitive interface.
Step 6: Test the Connection
Finally, verify that you have successfully connected to the databases. You should be able to run queries and manage your database without any issues.
Conclusion
By following these steps, you’ve set up multiple database connections in your Laravel project using Docker. This setup allows for a more organized and manageable development environment, making it easier to work with different database systems within a single application.
Feel free to reach out if you have any questions or need further assistance. Happy coding!