Connecting A Dockerized Laravel App To Internal Mysql Service

Running Laravel inside Docker is a modern approach to building and deploying apps consistently across various environments. If you're containerizing your Laravel app and want to connect it to a MySQL database without installing MySQL on your server, Docker Compose makes this process smooth and efficient.
In this guide, we’ll walk through how to spin up a Laravel container and link it to a MySQL container using Docker Compose. However, if you prefer using an external MySQL service rather than a containerized one, I have written a quick guide How To Connect A Dockerized Laravel App To A Host Mysql Server On A Linux Vps
Prerequisites
Make sure the following are ready:
- A pre-built Docker image for your Laravel app (e.g., published on Docker Hub)
- Docker and Docker Compose are installed on your VPS
Step 1: Create Docker Compose File
Create a docker-compose.yml
file in your preferred directory on your VPS.
Here is a sample content:
services:
app:
image: <username>/<your-image-name>
container_name: laravel-app
ports:
- "8090:80"
- "5173:5173"
environment:
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_DATABASE: ${DB_DATABASE}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
env_file:
- .env
volumes:
- ./.env.production:/var/www/html/.env:ro
networks:
- laravel-app-network
depends_on:
- db
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost || exit 1"]
interval: 30s
timeout: 10s
retries: 5
db:
image: mysql:8.0
container_name: laravel-app-db
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USERNAME}
MYSQL_PASSWORD: ${DB_PASSWORD}
ports:
- "3307:3306"
volumes:
- db_data:/var/lib/mysql
networks:
- laravel-app-network
volumes:
db_data:
networks:
laravel-app-network:
driver: bridge
Note: We expose MySQL to the host on port 3307 to avoid conflicts, but internally it runs on 3306.
Step 2: Create the .env
File
In the same directory as your docker-compose.yml
, create a .env
file with the following content:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=laravel_pass
MYSQL_ROOT_PASSWORD=secret_root_pass
Also, create .env.production
if your Laravel image expects it to be mounted at /var/www/html/.env
.
💡 Why
DB_HOST=db
?
In Docker Compose, services can reach each other by their service names.
Since our MySQL container is nameddb
, Laravel usesDB_HOST=db
to connect to it internally — notlocalhost
.
Step 3: Start Everything Up
From the directory containing your docker-compose.yml
, run:
docker-compose up -d
Step 4: Verify MySQL Connection
Access the app container through:
docker exec -it laravel-app bash
Install MySQL client (optional, for manual testing):apt update && apt install -y default-mysql-client
mysql -h db -u laravel_user -p
When prompted, enter laravel_pass
. You should land in the MySQL prompt, implying you are connected.
Assumption
This guide assumes:
- Laravel app image is pulled from Docker Hub (e.g.,
username/your-image-name
). - The MySQL service runs as a container, not on the host system.

Jkm96Dev
Staff writer at Mblog. Full stack web developer and author.
Login
No comments yet.