How to run Laravel in Docker
Last update: 04-25-2024
Welcome to this step-by-step guide on setting up a Laravel development environment using Docker. instead of the hassles of traditional software installations, Docker offers a convenient and efficient way to manage your development setup. By containerizing the environment, Docker ensures that you can get up and running with Laravel on any operating system where Docker is available. This approach not only simplifies the initial setup but also maintains consistency across different machines and team members.Initial Setup
To begin, create a new folder on your host machine. I'll refer to it as your-app
but you can use any name you want.
Place the following files in the 'your-app' folder:
- Dockerfile
- docker-compose.yml
Dockerfile:
FROM php:8.3.6 WORKDIR /laravel-app RUN apt update && apt install -y wget git unzip RUN wget https://getcomposer.org/download/2.7.3/composer.phar -O /usr/local/bin/composer && chmod +x /usr/local/bin/composer ENTRYPOINT ["tail", "-f", "/dev/null"]
Dockerfile Explanation:
- We start from a specific PHP image to ensure consistency across all environments.
- We set the WORKDIR (workding directory) to set the path for any operations following in the Dockerfile.
- The RUN commands update the package list and install wget, git, and unzip, which are necessary for downloading and managing Laravel and its dependencies. Composer is then downloaded directly into the system-wide binary path and made executable.
- The ENTRYPOINT command is a dummy command to keep the container running indefinitely
docker-compose.yml:
version: '3.8' services: php: build: . ports: - 8000:8000 volumes: - .:/laravel-app
docker-compose.yml Explanation:
- We define a single service called 'php'
- The build: . directive tells Docker to build the image using the Dockerfile located in the current directory.
- Mapping port 8000 from the host to the same port on the container allows access to Laravel's default port from the host machine.
- The volumes directive syncs the current folder to the folder in the container, in which the project's code will reside
Starting the Docker container
To create the project:
- Open your terminal
- Navigate to the main folder (the one that contains the Dockerfile, docker-compose.yml and the example-app folder)
docker compose up --build
This will start the container and set the ground for the creation of the Laravel application.
Once the container is running
Attaching to the container
I recommend using Visual Studio Code for Laravel development. Visual Studio Code offers excellent integration with Docker conatiners - just make sure you have the "Dev Containers" extension installed.
- Make sure that your container is running (you can always
docker compose up
if it isn't), then click on the blue icon in the bottom left corner of VSCode and choose "Attach to running container". - Select the container (which is named after the folder containing your Docker configuration).
- Once Visual Studio Code connects to the container, open the 'your-app' folder:
code ../laravel-app
This will open a new VSCode window with your Laravel project open. Close the old VSCode window.
Creating the application
Using VSCode's built in terminal, create the Laravel application by running the following:
composer create-project laravel/laravel temp mv temp/* ./ && mv temp/.* ./
Serving the application
To server your Laravel application and make it accessible to your host's browser, run:
php artisan serve --host=0.0.0.0This command starts Laravel's built-in development server, making it accessible through your local machine at localhost:8000.