In the realm of web development, frameworks and tools that streamline the process are invaluable. Among them, Laravel has emerged as a leading choice for PHP developers due to its elegant syntax and robust capabilities. When paired with Docker Compose, setting up a Laravel development environment becomes not only efficient but also consistent across different platforms. In this comprehensive guide, we will delve into the steps required to install and set up Laravel using Docker Compose on Ubuntu 22.04. Whether you’re a seasoned developer or a novice, this tutorial has got you covered.
Why Use Laravel with Docker Compose?
Before we dive into the installation process, let's take a moment to understand why using Laravel with Docker Compose is a game-changer.
-
Portability: Docker containers encapsulate all dependencies, ensuring that your application runs the same way in any environment—be it development, testing, or production.
-
Isolation: Each application runs in its container, which avoids conflicts with other software installations and versions.
-
Scalability: With Docker Compose, you can easily scale services by simply adjusting the configuration files.
-
Efficient Development Workflow: Docker Compose allows for simultaneous management of multiple services, enhancing the development workflow.
Prerequisites
Before we begin, ensure that you have the following prerequisites in place:
- An Ubuntu 22.04 server or machine.
- Root or sudo access to install packages.
- Basic knowledge of command-line operations.
Step 1: Install Docker and Docker Compose
To get started with Laravel and Docker, the first step is to install Docker and Docker Compose on your Ubuntu 22.04 system.
1.1 Update Your System
Open your terminal and make sure your system is updated:
sudo apt update
sudo apt upgrade -y
1.2 Install Docker
Run the following command to install Docker:
sudo apt install docker.io -y
After installation, you need to start the Docker service and enable it to run at boot time:
sudo systemctl start docker
sudo systemctl enable docker
To verify that Docker is installed correctly, run:
sudo docker --version
You should see output showing the Docker version installed.
1.3 Install Docker Compose
To install Docker Compose, download the latest version using the following command:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
Next, set the permissions to make it executable:
sudo chmod +x /usr/local/bin/docker-compose
Finally, check the version to confirm the installation:
docker-compose --version
1.4 Add Your User to the Docker Group
To run Docker commands without sudo, add your user to the Docker group:
sudo usermod -aG docker $USER
You’ll need to log out and back in for this change to take effect.
Step 2: Create a New Laravel Project
Now that Docker and Docker Compose are installed, the next step is to create a new Laravel project.
2.1 Install Composer
Laravel requires Composer, a dependency manager for PHP. To install Composer, run the following commands:
sudo apt install php-cli unzip
cd ~
curl -sS https://getcomposer.org/installer -o composer-setup.php
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e1...') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"
Make sure to replace the hash
value in the code with the latest SHA384 from Composer's official website.
2.2 Create the Laravel Project
Create a new Laravel project by navigating to your desired directory and running:
composer create-project --prefer-dist laravel/laravel laravel-docker
This command will create a new directory called laravel-docker containing your Laravel application.
Step 3: Set Up Docker Compose
Now that we have our Laravel project set up, let's create a Docker environment for it.
3.1 Create a Dockerfile
Navigate into your project directory:
cd laravel-docker
Create a Dockerfile
in the root of your Laravel project:
# Dockerfile
FROM php:8.1-fpm
# Install dependencies
RUN apt-get update && apt-get install -y libpng-dev libjpeg-dev libfreetype6-dev unzip git && \
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install gd
# Set working directory
WORKDIR /var/www
# Copy existing application directory contents
COPY . .
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Install Laravel dependencies
RUN composer install
# Expose the port the app runs on
EXPOSE 9000
# Start PHP-FPM server
CMD ["php-fpm"]
3.2 Create a docker-compose.yml File
Next, create a docker-compose.yml
file in the same directory:
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
image: laravel-docker-app
container_name: laravel_app
volumes:
- .:/var/www
networks:
- laravel-network
web:
image: nginx:alpine
container_name: laravel_nginx
ports:
- "8080:80"
volumes:
- .:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
networks:
- laravel-network
networks:
laravel-network:
driver: bridge
3.3 Create an Nginx Configuration File
We also need to create an Nginx configuration file to serve our Laravel application. Create a directory named nginx and then create a file called default.conf within it:
mkdir nginx
touch nginx/default.conf
Insert the following configuration into default.conf:
server {
listen 80;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Step 4: Running the Application
With everything set up, it’s time to run our Laravel application.
4.1 Build and Run the Containers
In your terminal, run the following command to build the Docker containers:
docker-compose up -d --build
This command will build your app service based on the Dockerfile you created earlier and run the containers in detached mode.
4.2 Accessing the Application
Once the containers are up and running, you can access your Laravel application by navigating to http://localhost:8080
in your web browser.
Step 5: Verify Your Installation
To ensure that Laravel has been set up correctly, you should see the default Laravel welcome page. This means everything is functioning as intended.
5.1 Artisan Commands
To run Laravel Artisan commands, you can execute the following command:
docker-compose exec app php artisan migrate
This command allows you to interact with your Laravel application and execute any migrations you've set up.
Conclusion
In this tutorial, we have outlined the steps to install and set up Laravel with Docker Compose on Ubuntu 22.04. By following this guide, you should now have a fully operational Laravel application running within Docker containers. The beauty of this approach is that you can easily manage dependencies and environments, providing a scalable solution for development and production alike.
Using Docker Compose with Laravel not only accelerates your development process but also ensures that you maintain a high level of consistency across different environments. By leveraging these modern tools, you can focus more on writing quality code rather than getting caught up in setup hassles.
Feel free to dive deeper into the capabilities of Laravel and Docker as you continue your web development journey!
FAQs
1. What is Laravel?
Laravel is a PHP framework designed for developing web applications following the model-view-controller (MVC) architectural pattern. It provides a robust set of features for building modern web applications.
2. What is Docker Compose?
Docker Compose is a tool that allows users to define and run multi-container Docker applications. It uses a simple YAML file to configure the application’s services, making it easy to deploy and manage applications.
3. Can I use Docker Compose with other frameworks?
Yes, Docker Compose can be used with any application that requires multiple services, not just Laravel. It is widely used with Node.js, Ruby on Rails, and many other frameworks.
4. How can I stop the Docker containers?
To stop the running Docker containers, navigate to your project directory in the terminal and run:
docker-compose down
This command stops and removes all containers defined in your docker-compose.yml
file.
5. Is it necessary to have Docker installed on my host machine to use Docker Compose?
Yes, Docker Compose relies on Docker to manage and run containers. Therefore, Docker must be installed and running on your host machine for Docker Compose to function correctly.
By following these steps and understanding the foundational components, we hope you now feel confident in setting up Laravel with Docker Compose on Ubuntu 22.04! Happy coding!