ChristophAust's avatar

Sail with existing DB host?

Hi folks,

I am fiddling around with sail in order to get my laravel app running in a docker environment. But it seems I cannot find a way to not install a mysql DB in a container.

I actually would like to have only the laravel app to run in the container as the redis and mysql are external services.

Any hints?

0 likes
3 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, you can use an existing MySQL database with Laravel Sail. You just need to update the .env file in your Laravel project to use the correct database credentials.

Here's an example .env file that uses an external MySQL database:

DB_CONNECTION=mysql
DB_HOST=your-db-host
DB_PORT=3306
DB_DATABASE=your-db-name
DB_USERNAME=your-db-username
DB_PASSWORD=your-db-password

Make sure to replace your-db-host, your-db-name, your-db-username, and your-db-password with your actual database credentials.

Once you've updated your .env file, you can start Sail with the --no-orchestration flag to prevent it from starting a MySQL container:

sail up --no-orchestration

This will start your Laravel app in a container, but it won't start a MySQL container.

Note that if you're using other services like Redis, you'll need to update your .env file to use the correct Redis credentials as well.

Theresanowak's avatar

Hello, If you want to use Laravel Sail with an existing MySQL database host instead of running MySQL within a Docker container, you can make some modifications to your Sail configuration. https://www.tellpopeyes.ltd/

Here's a step-by-step guide on how to achieve this:

Open your Laravel project and navigate to the root directory.

Find the .env file and open it for editing.

Locate the following lines in the .env file that define the database connection settings: DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=your_database DB_USERNAME=your_username DB_PASSWORD=your_password Modify the values of these variables to match your existing MySQL database host. For example: DB_CONNECTION=mysql DB_HOST=your_existing_host DB_PORT=3306 DB_DATABASE=your_database DB_USERNAME=your_username DB_PASSWORD=your_password Replace your_existing_host with the hostname or IP address of your external MySQL database.

Save the changes to the .env file.

Next, open the docker-compose.yml file located in the root directory of your Laravel project.

Look for the mysql service definition in the docker-compose.yml file. It should resemble something like this: mysql: image: 'mysql:8.0' ports: - '${FORWARD_DB_PORT:-3306}:3306' environment: MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}' MYSQL_DATABASE: '${DB_DATABASE}' MYSQL_USER: '${DB_USERNAME}' MYSQL_PASSWORD: '${DB_PASSWORD}' volumes: - 'sailmysql:/var/lib/mysql' networks: - sail Comment out or remove the mysql service definition entirely. This step ensures that MySQL container won't be started.

Save the changes to the docker-compose.yml file.

Now you can start Sail using the existing MySQL database host by running the following command in your Laravel project's root directory: ./vendor/bin/sail up Laravel Sail will now use the MySQL database host you specified in the .env file instead of starting a new MySQL container. Make sure that your existing MySQL database host is accessible from within the Docker network where Laravel Sail runs.

ChristophAust's avatar

@Theresanowak thanks, I ended up, installing sail chosing the service mysql and removing it from the dockerfile manually later.

Please or to participate in this conversation.