Skip to content
Start » Laravel » Connecting Laravel Databases to Docker Laradock

Connecting Laravel Databases to Docker Laradock

The connection is very straightforward and intuitive to MySQL or Mariadb database servers in a PHP Laravel project when using Docker containers from the Laradock project.

However, some tips and reminders are important to avoid wasting time when preparing your development environment for a Laravel application in Laradock that uses DBMS.

In this article I will exemplify, in addition to configuration and connection, tips for importing sql dumps from Mysql or MariaDB, from your host to the desired Docker container.

I also show how to connect a database client, the DBaver with the Mysql or MariaDB server running in the Docker Laradock container.

In addition to solving some connection issues with database containers using Docker Laradock on Mac OS (M1 and Pro).

Laradock Tutorials:

Laradock intro
Laradock, installation and configuration
– Connection of databases in Laravel project with Laradock – This article
– Articles with error resolution:
How to solve Laradock and Docker error with Python modules
LaraDock error with Mac Silicon computer (MacBook Pro M1) – Docker Desktop Apache2_1 exits with code 2

Creating and uploading the database container with Laradock

Laradock comes with an interesting array of ready-to-go database server containers. See the list in the article Laradock – Docker Containers for Your PHP and Laravel Development – Introduction.

Among the available servers are MySQL and MariaDB.

In order for a container with one of these servers to go up and become available, you must run the command docker-compose up.

To climb – and build for the first time it runs, the MySQL, run:

docker-compose up -d mysql

To climb – and build for the first time it runs, the MariaDB, run:

docker-compose up -d mysql

You can upload multiple containers at the same time, including having both servers running at the same time, along with other services like Apache2, Workspace and Redi:

docker-compose up -d apache2 mysql mariadb workspace redis # result Starting laradock_redis_1 ... done Starting laradock_docker-in-docker_1 ... done Creating laradock_mariadb_1 ... done Starting laradock_workspace_1 ... done Starting laradock_php-fpm_1 ... done Starting laradock_apache2_1 ... done

This way you will be able to access the different database servers according to the connection configuration of your PHP Laravel project, or pure PHP.

Connecting PHP Laravel Project with Database Server in Docker

Remember that containers go up with specific names, look again at the example from the previous section for the result.

In our example the MySQL server is called laradock_mysql_1, while the MariaDB server is called laradock_mariadb_1.

Connecting PHP Laravel with MySQL

To connect your PHP Laravel project to the MySQL server running in the Laradock configuration Docker container, configure the file .env with the following variables:

DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=my_db_name DB_USERNAME=root DB_PASSWORD=root

Note that the user's password root is defined in the file .env from Laradock, not your Laravel project.

Connecting PHP Laravel with MariaDB

To connect your PHP Laravel project to the MariaDB server running in the Laradock configuration Docker container, configure the file .env with the following variables:

DB_CONNECTION=mysql DB_HOST=mariadb DB_PORT=3306 DB_DATABASE=my_db_name DB_USERNAME=root DB_PASSWORD=root

Note that the root user password is defined in the file .env from Laradock, not your Laravel project.

How to connect DBeaver with MySQL or MariaDB in Laradock

The DBeaver database client's connection to the database servers in a Docker Laradock container differs only in the identification of the host.

To connect to DBeaver, you must use the localhost ip path: 127.0.0.1, as shown in the following image:

DBeaver conexão MYSQL e MariDB no Docker Laradock

How to Connect Database in Laradock with Mac M1 and Pro

A common mistake when using Mac OS computers is the inability to connect using the container name directly, as shown above for Linux Ubuntu, Debian and Mint.

To solve just add the ip 127.0.0.1 pointing to the desired host, for example mysql, in the file /etc/host from Mac.

An example file with the insert for mysql and mariadb:

## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost # Added by Docker Desktop # To allow the same kube context to work on the host and the container: 127.0.0.1 kubernetes.docker.internal ## Add the lines below 127.0.0.1 mysql 127.0.0.1 mariadb # End of section

keep up to date

Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.

1 thought on “Conexão de bancos de dados do Laravel com Docker Laradock”

  1. Pingback: Laradock - Docker Containers for your PHP and Laravel development - Introduction - MAZER.DEV

Leave a Reply

Your email address will not be published.

en_USEnglish
%d bloggers like this: