How To Install and Set Up Laravel 8 with Docker Compose on Ubuntu 20.04
To get started, you’ll need to create a containerized environment. Then, you’ll be able to bootstrap the new Laravel application from scratch, without the need to have a local PHP environment installed on your local machine or development server. In any site and moment in few second you’ll have available your enviroment.
For any bug or fail, please highlight. It’s enabled
Environment
- Ubuntu 20.04
- Docker compose version 1.28.5, build c4eb3a1f
- Docker version 20.10.5
- Sublime text
- Google Chrome
Step 1: Install dependencies (Docker and Docker-Compose)
First, update your packages:
sudo apt update
Next, install a few prerequisite packages:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Then add the GPG key for the official Docker repository to our system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to APT sources:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | apt-get update
And finally install docker:
sudo apt-get install docker-ce
Check that it’s running:
docker.service — Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021–03–12 20:03:09 CET; 41min ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 1321 (dockerd)
Tasks: 34
Memory: 166.9M
CGroup: /system.slice/docker.service
├─1321 /usr/bin/dockerd -H fd:// — containerd=/run/containerd/containerd.sock
└─2200 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8000 -container-ip 172>
Now we are going to install last docker-compose version:
DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}mkdir -p $DOCKER_CONFIG/cli-pluginscurl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o $DOCKER_CONFIG/cli-plugins/docker-compose
Next, set the execution permissions, if you don’t do it you’ll get error like “Segmentation fault (core dumped)”:
chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
Verify if your installation was successful
maruan@maruan:~/Documentos$ docker compose version
Docker Compose version v2.5.0
Finall check:
maruan@maruan:~/Documentos/laravel_docker$ sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
Step 2: Setting up a docker-compose.yml file
Okay, now it’s the big moment my boy. Let’s start. First you need create a new directory “workspace” for your laravel project:
maruan@maruan:~/Documentos$ mkdir laravel_docker
maruan@maruan:~/Documentos$ cd laravel_docker/
Next, you’ll need create the docker-compose.yml file that will define the containerized environment. In this file, you’ll set up a service named app to be used by laravel. You need know your user and uid arguments cause will be used in the Dockerfile at build time because you’ll need permisions in directory without a root user (good practices and more safety).
Get your uid for current user:
maruan@maruan:~/Documentos/laravel_docker$ echo $UID
1000
Now create docker-compose.yml:
maruan@maruan:~/Documentos/laravel_docker$ nano docker-compose.yml
Step 2.1: Laravel app service
Now, define the laravel project to build docker image for the Laravel project using the Dockerfile(remember change to your own user and uid):
- The laravel container service it’s named app. And the new image will be named as laravel-app.
- We want to mount the laravel project directory to the /var/www/html inside the container service.
- We’re using the custom docker network for our setup, the network will be named as mynetwork.
Step 2.2: Nginx service
Now we will define the nginx container service:
- Create a new container named nginx based on the docker image nginx:alpine.
- The nginx container will mount two different volumes. The laravel project will to be directory to the /var/www/html directory, the nginx virtual host configuration nginx/conf.d/ to the /etc/nginx/conf.d
- We’re using the custom docker network for our setup, the network will be named as mynetwork.
Step 2.3: MySQL service
And the last, we define the MySQL database service.
- The MySQL container service will be named as db, based on the mysql:5.7 docker image.
- The db service will open the default MySQL port 3306.
- The Laravel project will be using the database, user, and the password based on the environment variable of the db service located in env file.
- The MySQL db service will mount the volume named ‘mysqldata’, and have the same network mynetwork.
- And we define the custom network mynetwork with the bridge driver, and the mysqldata volume will be using the local drive
Complete file:
Step 3: DockerFile
Next, create a new Dockerfile for your Laravel project.
maruan@maruan:~/Documentos/laravel_docker$ nano Dockerfile
Save and close the file. Next, bring your environment up:
maruan@maruan:~/Documentos/laravel_docker$ sudo docker-compose up -d Creating network “mynetwork_landing” with driver “bridge”
Building app
Step 1/11 : FROM php:7.4-fpm
— -> fa37b52db33g
…
Step 10/11 : WORKDIR /var/www
— -> Using cache
— -> 769af65sf4d8
Step 11/11 : USER $user
— -> Using cache
— -> 841eb5852b69Successfully built 841eb5852b69
Successfully tagged laravel_docker:latest
WARNING: Image for service app was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up — build`.
Creating laravel_docker_app_1 … done
You can verify:
Name Command State Ports
--------------------------------------------------------------------
laravel_docker_app_1 docker-entrypoint.s Up 9000/tcp
Now app service is up, in this momemnt you can run composer, with this you can deploy laravel app (create laravel project):
maruan@maruan:~/Documentos/laravel_docker$ docker-compose exec app composer create-project laravel/laravel --prefer-dist applicationCreating a “laravel/laravel” project at “./application”
Installing laravel/laravel (v8.5.13)
— Downloading laravel/laravel (v8.5.13)
— Installing laravel/laravel (v8.5.13): Extracting archive
Created project in /var/www/application
> @php -r “file_exists(‘.env’) || copy(‘.env.example’, ‘.env’);”
Loading composer repositories with package information
Updating dependencies
Lock file operations: 105 installs, 0 updates, 0 removals
Package manifest generated successfully.
74 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan key:generate — ansi
Application key set successfully.
This installation creates a new .env file based on the default .env.example originally comes with Laravel. The .env file contains database credentials and other sensitive application settings.
Now down docker:
maruan@maruan:~/Documentos/laravel_docker$ sudo docker-compose down
[sudo] contraseña para maruan:
WARNING: The DB_DATABASE variable is not set. Defaulting to a blank string.
WARNING: The DB_PASSWORD variable is not set. Defaulting to a blank string.
WARNING: The DB_USERNAME variable is not set. Defaulting to a blank string.
Stopping laravel_docker_app_1 … done
Removing laravel_docker_app_1 … done
Step 3.1: Create Nginx Virtual Host for Laravel
Within the laravel_docker project directory, create a new directory named nginx that will contain other directorie conf.d. Then create a new nginx virtual host configuration laravel.conf inside the conf.d directory.
First you’ll need directory so create it:
maruan@maruan:~/Documentos/laravel_docker$ mkdir -p nginx/{conf.d}
Create laravel.conf:
maruan@maruan:~/Documentos/laravel_docker/nginx/conf.d$ nano laravel.conf
Step 4: Edit .env file
If you has good eyes, you can see we don’t have configured $db_password and other parameters, so let’s do it:
maruan@maruan:~/Documentos/laravel_docker$ nano .env
Change password, username and database accordingly db container:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:VxpSASCd2i+PQIkA0heyFeJTM44a63Qd1DdkuFs+I5Q=
APP_DEBUG=true
APP_URL=http://localhost:8000LOG_CHANNEL=stack
LOG_LEVEL=debugDB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=laravel
DB_PASSWORD=laravelpassworddb
Now the only step… bring the updated environment up
sudo docker-compose start
Starting app ... done
Starting nginx ... done
Starting db ... done
Now, you can point your browser to localhost or your remote server’s IP address, on port 8000http://localhost:8000/
If you make all good, you’ll see a page like this:
Errors:
All errors it’s because you’re probably using wrong php version.
- PHP Fatal error: Declaration of App\Exceptions\Handler::report(Exception $exception) — Change your version to PHP 7.4 here
- This package requires php >=7.4 but your PHP version (8.0.3) does not satisfy that requirement — edit composer.json
"platform": {
"php": "7.4"
}
- Docker & Laravel : configure: error: Package requirements (oniguruma) were not met — Change your version to PHP 7.4 here
- Thanks for learn with me. Don’t forget follow me in my little social network and in medium.