yousefashrafi's avatar

Getting Started with Laravel and Docker

Hey everyone,

I wanted to share a quick guide on how to set up a Laravel application with Docker using docker-compose. This setup can help you create isolated environments, making your development process more efficient. Let's dive in!

Step 1: Install Docker

Make sure you have Docker and Docker Compose installed on your machine. You can download them from the official Docker website.

Step 2: Set Up Your Laravel Project

First, create a new Laravel project. If you already have an existing project, you can skip this step.

composer create-project --prefer-dist laravel/laravel laravel-docker
cd laravel-docker

Step 3: Create docker-compose.yml File

In your Laravel project root, create a docker-compose.yml file. This file will define the services required for your application.

version: '3.8'

services:
  app:
    image: php:8.1-fpm
    container_name: laravel_app
    working_dir: /var/www
    volumes:
      - ./:/var/www
    networks:
      - laravel

  webserver:
    image: nginx:alpine
    container_name: laravel_webserver
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./docker/nginx/conf.d:/etc/nginx/conf.d
    networks:
      - laravel

  db:
    image: mysql:5.7
    container_name: laravel_db
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: root
      MYSQL_PASSWORD: root
      MYSQL_USER: laravel
    ports:
      - "3306:3306"
    volumes:
      - dbdata:/var/lib/mysql
    networks:
      - laravel

networks:
  laravel:

volumes:
  dbdata:

Step 4: Configure Nginx

Create the docker/nginx/conf.d directory structure and a configuration file for Nginx:

mkdir -p docker/nginx/conf.d

Create a file named default.conf inside the docker/nginx/conf.d directory with the following content:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root /var/www/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ /\.ht {
        deny all;
    }
}

Step 5: Update Laravel Environment Configuration

Open the .env file in your Laravel project and update the database configuration to match the Docker setup:

DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=root

Step 6: Build and Run Docker Containers

Now, let's build and run the containers. In the root of your project, run:

docker-compose up -d

This command will build the Docker images (if they aren't already built) and start the containers in detached mode.

Step 7: Access Your Laravel Application

Once the containers are up and running, you can access your Laravel application in your web browser at localhost.

Step 8: Running Artisan Commands

To run Artisan commands inside the Docker container, you can use the following command format:

docker-compose exec app php artisan migrate

Conclusion

And that's it! You've successfully set up a Laravel application using Docker. This setup allows you to easily manage and isolate your development environment, making your workflow more efficient and less prone to conflicts. If you have any questions or run into issues, feel free to ask in the comments below!

Happy coding!

0 likes
0 replies

Please or to participate in this conversation.