Or copy link
Copy link
Laravel is a PHP based web framework known for its clean and easy to use architecture which makes web development easier and more efficient. Setting up a Laravel application in Docker creates a consistent and isolated development environment. This setup not only facilitates development but also simplifies deployment and scaling.
In this tutorial, we will walk you through a step-by-step procedure for setting up Laravel in Docker.
Docker is a powerful tool that allows the packaging of single or multiple applications into containers. In Docker, setting up Laravel creates an environment that prevents conflicts and streamlines the development of applications.
To set up Laravel in Docker, consider the below instructions with practical demonstration:
Docker is responsible for running individual containers, while Docker Compose lets us define and run multi-container Docker applications using a single YAML file. Therefore, to set up Laravel in Docker, it’s essential to install Docker on Ubuntu system.
After the installation, you can set up the new Laravel project or navigate to the existing project directory. For instance, download the sample project from GitHub named travellist-laravel-demo:
curl -L https://github.com/do-community/travellist-laravel-demo/archive/tutorial-1.0.1.zip -o travellist.zip
After downloading, extract the sample project with the unzip utility:
unzip travellist.zip
Now, move all extracted files to the new project folder named travellist-demo:
travellist-demo
mv travellist-laravel-demo-tutorial-1.0.1 travellist-demo
Let’s access the travellist-demo folder using the following command:
cd travellist-demo
Read also How to Install Docker on Windows
Laravel’s environment configurations are stored in the .env file. In this file, you can modify the database settings to your Docker service names and credentials. To configure the development environment, let’s copy the existing template:
.env
cp .env.example .env
Now create a new .env file based on the existing template:
nano .env
Let’s copy the code below and paste it into the .env file:
APP_NAME=Travellist APP_ENV=dev APP_KEY= APP_DEBUG=true APP_URL=http://localhost:8000 LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=travellist DB_USERNAME=travellist_user DB_PASSWORD=password
This step is crucial for connecting your Laravel application to the correct database container:
After that, save and close the file.
For Laravel, the Dockerfile sets up the PHP environment, installs dependencies, and sets up any necessary extensions. Let’s create it in the working directory:
nano Dockerfile
Now, copy the code below and paste it into the Dockerfile file:
Dockerfile
FROM php:8.2-fpm ARG user ARG uid RUN apt-get update && apt-get install -y \ git \ curl \ libpng-dev \ libonig-dev \ libxml2-dev RUN apt clean && rm -rf /var/lib/apt/lists/* RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd COPY --from=composer:latest /usr/bin/composer /usr/bin/composer RUN useradd -G www-data,root -u $uid -d /home/$user $user RUN mkdir -p /home/$user/.composer && \ chown -R $user:$user /home/$user WORKDIR /var/www USER $user
Then, save and close the file:
Now, create a docker-compose/nginx folder in the project directory to store configuration files:
docker-compose/nginx
mkdir -p docker-compose/nginx
In this folder, create a .conf file to perform Nginx configuration. Let’s create the file named travellist.conf, which sets up Nginx to serve our application:
.conf
travellist.conf
nano docker-compose/nginx/travellist.conf
Now, copy the Nginx configuration below and save it as a file named travellist.conf in the current directory:
server { listen 80; index index.php index.html; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /var/www/public; 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 / { try_files $uri $uri/ /index.php?$query_string; gzip_static on; } }
After pasting the above code in the travellist.conf file, save, and close the file:
Now, add a new folder named mysql for MySQL initialization files in the docker-compose directory:
mysql
docker-compose
mkdir docker-compose/mysql
Let’s open the init_db.sql file located in the docker-compose/mysql/ directory using the nano text editor for editing:
init_db.sql
docker-compose/mysql/
nano docker-compose/mysql/init_db.sql
Now, add the following MySQL commands to the init_db.sql file. You can also add customized SQL statements to display on the webpage:
SELECT 'Ultahost Setting up Laravel in Docker' AS message;
After that, save and close the .sql file in the nano editor.
Connect Your Laravel Apps with our PHP Hosting!
Ultahost provides optimized PHP hosting solutions, ensuring a smooth and secure environment for running your Laravel projects seamlessly.
The docker-compose.yml file specifies the Docker networks, services, volumes, and many more. For a Laravel application, you typically need services like PHP, Nginx or Apache, MySQL, or other services depending on your application’s needs. Let’s create the .yml file:
docker-compose.yml
.yml
sudo nano docker-compose.yml
Now, add the definition of services, db, nginx, and networks to the docker-compose.yml file under the corresponding sections:
version: "3.7" services: app: build: args: user: anees uid: 1000 context: ./ dockerfile: Dockerfile image: travellist container_name: travellist-app restart: unless-stopped working_dir: /var/www/ volumes: - ./:/var/www networks: - travellist db: image: mysql:8.0 container_name: travellist-db restart: unless-stopped environment: MYSQL_DATABASE: ${DB_DATABASE} MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} MYSQL_PASSWORD: ${DB_PASSWORD} MYSQL_USER: ${DB_USERNAME} SERVICE_TAGS: dev SERVICE_NAME: mysql volumes: - ./docker-compose/mysql:/docker-entrypoint-initdb.d networks: - travellist nginx: image: nginx:alpine container_name: travellist-nginx restart: unless-stopped ports: - 8000:80 volumes: - ./:/var/www - ./docker-compose/nginx:/etc/nginx/conf.d/ networks: - travellist networks: travellist: driver: bridge
In the end, save and close the file.
With the Dockerfile and docker-compose.yml in place, you can build your application’s Docker image and run the containers. This is done using the docker-compose up command, which will start and run your entire app:
sudo docker-compose up -d
After everything is set up, you can access your Laravel application via the web browser at the specified port 8000 (configured in Step 3). Now, open the web browser and navigate to http://localhost:8000:
The output confirms that we have successfully set up a Laravel application in Docker.
Setting up Laravel in Docker involves several key steps to ensure a smooth development environment. First, you need to create a Dockerfile that defines the PHP environment suitable for Laravel, including necessary extensions and Composer. Next, a docker-compose.yml file is essential to define services such as Nginx, MySQL, and PHPMyAdmin, which are common in Laravel applications.
Once the configuration files are ready, you can build and run your containers using Docker Compose, which will set up the entire environment. Afterward, you can access your Laravel application through the specified ports and manage your database.
We hope this guide has helped you easily set up Laravel on Docker. You can integrate Docker with Ultahost’s best Linux VPS server for an optimized Laravel environment. Enjoy super-fast SSD NVMe speeds with no delays, making sure your Laravel app runs smoothly on Docker.
Setup Laravel Docker creates isolated development environments, preventing conflicts. It makes deployment easier by packaging the application with its dependencies. Also, streamlines development by providing a readily available environment.
Docker and Docker Compose must be installed on your system before setting up Laraverl on Docker.
Update database settings to match Docker service names and credentials (host, port, username, password).
A Dockerfile defines the instructions for building a Docker image containing the PHP environment, dependencies, and extensions needed for your Laravel application.
The Nginx configuration defines how Nginx serves your Laravel application. It specifies the document root, handles PHP requests, and sets up error logging.
This file defines the services needed for your Laravel application (like PHP, Nginx, MySQL) and how they interact with each other. It includes configurations for volumes, networks, and environment variables.
Use the docker-compose up -d command in your project directory to build the Docker image and start the containers in detached mode.
Docker is a powerful containerization platform that all...
Integrating a MySQL database with a PHP website in web ...
Docker is a popular containerization platform that allo...
Laravel is a popular open-source PHP framework designed...
Starting an online store can be complex due to the time...
Docker is an open-source platform that enables develope...
Save my name, email, and website in this browser for the next time I comment.
Δ