Write down Docker Compose file for Laravel and MySQL application

Write down Docker Compose file for Laravel and MySQL application

Here is an example Docker Compose file for a Laravel application with a MySQL database:

version: '3'
  # Laravel Application
    image: composer:latest
      - ./laravel-app:/var/www
    working_dir: /var/www
    command: bash -c "composer install && php artisan serve --host= --port=8000"
      - "8000:8000"
      - mysql
      DB_CONNECTION: mysql
      DB_HOST: mysql
      DB_PORT: 3306
      DB_DATABASE: laravel_db
      DB_USERNAME: laravel_user
      DB_PASSWORD: secret

  # MySQL Database
    image: mysql:latest
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: laravel_db
      MYSQL_USER: laravel_user
      MYSQL_PASSWORD: secret
      - "3306:3306"
      - ./mysql-data:/var/lib/mysql


  • The app service uses the official composer:latest image for Laravel dependencies and running the development server. It connects the laravel-app folder to the container and sets up environment variables for the MySQL database.

  • The mysql service uses the official mysql:latest image, with environment variables for the MySQL root password, database name, and user details. It also connects a volume (./mysql-data) to save MySQL data.

  • Both services are in the same network, so they can talk to each other using their service names (app and mysql).

To use this Docker Compose file:

  1. Create a directory for your Laravel application and place this docker-compose.yml file in the root of that directory.

  2. Run the following command to start the containers:

     docker-compose up
  3. Access your Laravel application at http://localhost:8000.

  4. To stop and remove the containers, use:

     docker-compose down

Be sure to change the settings to fit your Laravel app's needs. Also, think about using environment files for private information and setup.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!