How to Set Up a Laravel 11 and PHP 8 Environment with Docker
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
関連動画
Related Video
This is a video where we actually tried out the content from the article! If anything is unclear in the article, please check out the video.
The video provides further explanations and demonstrations, so it should be helpful.
Subscribe to Our Channel
If you found this video helpful, please consider subscribing to our channel or giving it a thumbs up! It really motivates us to create more content.
Questions and Feedback
If you have any questions or feedback regarding this article or the video, feel free to leave them in the comment section of the video. Your input is greatly appreciated and will help us improve our content in the future!
How to Set Up a Laravel 11 and PHP 8 Environment with Docker
We’ve summarized how to set up a Laravel 11 and PHP 8 environment using Docker.
At the end of the article, there is a video that demonstrates the actual environment setup.
If you run into any difficulties, refer to the video for guidance.
Tools You’ll Use
- Docker Desktop
- VScode
- Mac (Intel chip)
Final Development Environment
- Laravel 11
- PHP 8.3
- Node.js 20
- MySQL 8.4
Configuring Docker Desktop
First, create an “empty folder” in a suitable location for your project.
Download and Install Docker Desktop
Download and install the version suitable for your PC from the link above.
Add Folder to File Sharing
Note: This may not be necessary for Windows.
- Click the gear icon in the top right
- Go to Resources
- Select File Sharing
- Click the Add button at the bottom and select the empty folder you created
- Click Apply
Folder and File Structure
This will be your final structure. There are many folders and files, so be careful as mistakes can prevent it from working properly.
docker/
├── db/
│ ├── data/
│ ├── Dockerfile
│ └── my.conf
├── nginx/
│ ├── default.conf
│ └── Dockerfile
├── php/
│ ├── Dockerfile
│ └── php.ini
src/
docker-compose.yml
Creating the Web Container
Create docker-compose.yml
and Add the Following Code
services:
web:
container_name: "web"
build:
dockerfile: "./docker/nginx/Dockerfile"
ports: - "8900:80"
volumes: - "./src:/var/www/html"
Create docker/nginx/Dockerfile
and Add the Following Code
# Lightweight stable version
FROM nginx:stable-alpine
# Copy the configuration file
# COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf
### Create `docker/nginx/default.conf` and Add the Following Code
server { listen 80; listen [::]:80; # Specify the root directory root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Run the Command in the Project Folder (Where docker-compose.yml
Is)
Execute the following command in your terminal:
docker compose up -d
Make sure to read the display carefully as Docker errors may not appear in red. Check if there are any errors.
Check if the container is running:
docker compose ps
If the web container is running, you’re good to go.
Also, visit:
http://localhost:8900/
You should see an nginx error screen. This error is expected since Laravel is not yet installed.
Creating the App Container
Add the Following to docker-compose.yml
app:
container_name: "app"
mem_limit: 6g
build:
dockerfile: "./docker/php/Dockerfile"
volumes: - "./src:/var/www/html"
environment: - DB_CONNECTION=${DB_CONNECTION:-mysql}
- DB_HOST=${DB_HOST:-db} - DB_PORT=${DB_PORT:-3306}
- DB_DATABASE=${DB_DATABASE:-laravel-docker} - DB_USERNAME=${DB_USERNAME:-user}
- DB_PASSWORD=${DB_PASSWORD:-password}
Create docker/php/Dockerfile
and Add the Following Code
FROM php:8.3-fpm
# Copy the configuration file
COPY ./docker/php/php.ini /usr/local/etc/php/php.ini
# Install packages
RUN apt-get update \
&& apt-get -y install git zip unzip vim \
wget xz-utils
RUN docker-php-ext-install pdo_mysql mysqli
RUN apt-get -y install libzip-dev \
&& docker-php-ext-install zip
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get install -y nodejs
# Install composer
RUN cd /usr/bin && curl -s http://getcomposer.org/installer | php && ln -s /usr/bin/composer.phar /usr/bin/composer
# Specify the Node.js version to use
ENV NODE_VERSION=20.9.0
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version
# Set the working directory inside the container
# WORKDIR /var/www/html
### Create `docker/php/php.ini` and Add the Following Code
[Date] date.timezone = "Asia/Tokyo" [mbstring] mbstring.internal_encoding = "UTF-8" mbstring.language = "Japanese"
### Check if Everything Is Working Properly
Execute the following command in your terminal:
docker compose up -d
Check if the container is running properly:
docker compose ps
If the app container is running, everything is good.
## Creating the Database Container
### Add the Following to `docker-compose.yml`
db: container_name: db build: dockerfile: "./docker/db/Dockerfile" ports: - 3306:3306 environment: - MYSQL_DATABASE=${DB_NAME:-laravel-docker} - MYSQL_USER=${DB_USER:-user} - MYSQL_PASSWORD=${DB_PASS:-password} - MYSQL_ROOT_PASSWORD=${DB_PASS:-password} volumes: - "./docker/db/data:/var/lib/mysql"
### Create `docker/db/Dockerfile` and Add the Following Code
Base image
FROM mysql:8.4.0
Copy the configuration file
COPY ./docker/db/my.conf /etc/my.conf
Create docker/db/my.conf
and Add the Following Code
[mysqld]
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
Create an Empty Folder docker/db/data/
Create the folder as per the previous structure.
Run the Command
Execute the following command in your terminal:
docker compose up -d
Check if the container is running properly:
docker compose ps
If the db container is running, everything is good.
Installing the Laravel Project
If all goes well, it’s just two commands.
docker compose exec app bash
This command allows you to enter the app container.
Once inside, run the following command to install Laravel:
composer create-project laravel/laravel:^11.0 .
If no errors occur, it’s complete. Access the following URL in your browser:
http://localhost:8900/
If you see the Laravel screen, the environment setup is complete.
Congratulations!
When You Want to Check Database Contents
During development, it can be cumbersome to verify database contents by typing SQL commands each time.
There are various ways to check the contents of a database depending on the language, but for PHP, using phpMyAdmin is convenient.
It’s very simple as you only need to add it to your docker-compose.yml.
How to Add phpMyAdmin to a Dockerized Laravel Project
Conclusion
This concludes the setup of the Laravel 11 environment with Docker.