- Laradock – Docker Containers for Your PHP and Laravel Development – Introduction
- Laradock, installation and configuration
- Connecting Laravel Databases to Docker Laradock
How to install and configure Laradock, which is, as presented in introductory article, a project for quick and simple configuration of PHP systems development environments for the web, using Docker.
In this tutorial article, we will see in more detail how to install and configure a Laradock environment to work per PHP project, i.e. one installation for each project. Or how to use one installation for multiple projects.
It is also shown how to configure installations to run different versions of PHP for multiple projects.
Laradock Tutorials:
– Laradock intro
– Laradock, installation and configuration – This article
– Connecting Databases in Laravel Project with Laradock
Articles with error resolution:
– How to solve Laradock and Docker error with Python modules
– LaraDock error with Mac Silicon computer (MacBook Pro M1) – Docker Desktop Apache2_1 exits with code 2
requirements
The applications and requirements for installing, configuring and running a Laradock environment are:
- GIT
- Docker: v >= 17.12
Installing Laradock for multiple projects
This installation is intended to configure a Laradock installation so that multiple projects can use the same configuration of containers.
Clone Laradoc repository
- Clone the Laradock repository anywhere on your machine - I advise you to clone to the folder where you keep your projects, on the same level as them:
$ cd my-workspace $ git clone https://github.com/laradock/laradock.git
Your folder structure should look something like this:
- my-workspace - laradock - project-a - project-b ... - other-projects
Edit environment file
2. edit the configuration file of your Docker Laradock environment. copy the file .env.example
for .env
$ cd laradock $ cp .env.example .env
3. Open it the file .env
and make sure that the variable APP_CODE_PATH_HOST
, at the beginning of the file, is pointing to the path of path previous:
APP_CODE_PATH_HOST=../
This setting defines the path of your applications on the local machine, this means that they are in an earlier folder level than the Laradock folder. So you must clone as explained in step 1.
4. configure the file .env
with the other settings you want for this installation. As web server to use and php-fpm and workspace interpreter extensions.
Configure site/project paths
5. browse in the folders of your Laradock installation, where each subdirectory corresponds contains configuration files or application Dockerfiles corresponding to the folder name.
Go to the web server you chose to use: Apache or NGinx, and create configuration files to point to different project directories on your machine that you want to serve with the Laradock installation:
For Nginx go to nginx/sites
, for Apache2 apache2/sites
.
Laradock by default includes some sample files for you to copy app.conf.example
, laravel.conf.example
and symfony.conf.example
.
6. change the default names *.conf
:
You can rename the configuration files, project folders and domains as you wish, just make sure the root in the configuration files is pointing to the correct project folder name.
6.1. For example: you will use Apache2 and the name of one of the applications is clients and it is being developed in Laravel:
6.1.1. access the folder apache2/sites
of the Laradock installation
6.1.2. copy the file sample.conf.example
for customers.conf
6.1.3. change the contents of the configuration in the new file:
ServerName cliente.test DocumentRoot /var/www/clientes/public/ Options Indexes FollowSymLinks AllowOverride All <IfVersion Allow from all = 2.4> Require all granted
See how we define the DocumentRoot and Directory based on the configuration of rais files of our Apache for the Laradock installation, and we point to the public subdirectory because it is a Laravel application that must have its access point in this directory and not in the application root.
7. add domains to files hosts
.
In the file hosts
from your machine, configure the ip of localhost to point to each project configured in your files *.conf
from the web server in the Laradock installation.
127.0.0.1 clients.test 127.0.0.1 other_project.localhost ...
Note that you can use extensions for the local domain like: .localhost
, .invalid
, .test
, or .example
. But no you can use the extension .dev
.
read Domain extensions to use for local development to understand why.
Continue in section Using Laradock.
Using Laradock
Before you start using your recent installation of Laradock, be aware of the following:
- If you are using the Docker Toolbox (VM), I advise using Laradock v.4, for that, upgrade to Docker native for Mac/Windows (recommended). If necessary, check the documentation as update Laradock.
- I recommend using a version of Docker newer than 1.13.
- If you used an older version of Laradock, we highly recommend rebuilding the containers you need. see how rebuild a container in the documentation.
Configure the .env environment file
After the installation and basic configuration you should understand how to configure your environment and the steps to upload Docker containers on a daily basis.
- If you haven't already, copy the file
.env.example
for.env
and configure the installations you want for your Docker services, as well as the PHP version.
$ cd laradock $ cp .env.example .env
edit the file .env
to choose which software you want to install in your environment. You can always refer to the file docker-compose.yml
to see how these variables are being used.
Depending on the host operating system, you may need to change the value provided for COMPOSE_FILE. When you are running Laradock on Mac OS, the correct file separator to use is :
. When running Laradock from a Windows environment, multiple files must be separated with ;
.
By default, the containers that will be created have the name of the current directory as a suffix (for example, laradock_workspace_1
).
This can cause “data mixing” within container volumes if you use laradock in multiple projects. To avoid this change the variable COMPOSE_PROJECT_NAME
to something unique, like the name of your project.
I keep the value laradock
same for all my projects on a shared installation of Laradock.
# Defines the prefix of container names. This is useful if you have multiple projects that use laradock to have separate containers per project. COMPOSE_PROJECT_NAME=laradock
This is why I don't use overlapping paths or database names in the databases. Another reason to use the same name in this case is to share service containers like mysql between different versions of php-fpm.
Launch Docker Containers
2. create the environment and run it using docker-compose
In this example, we will see how to run NGINX (web server), MySQL (database engine) to host PHP web scripts and REDIS (database in memory for cache):
$ docker-compose up -d nginx mysql redis
Do you want to work with APACHE (web server), MONGODB (non-relational database engine) and REDIS?
$ docker-compose up -d apache mongodb redis
Note: All web server containers nginx
, apache
..etc depend on php-fpm
, which means if you run any of them they will automatically start the container php-fpm
. This way it is not necessary to call explicitly in the command up
O php-fpm
.
If you have to do this, you may need to run them like this:
$ docker-compose up -d nginx php-fpm mysql
You can select your own combination of existing containers, listed on here.
Heads up: this list is not always up to date, so check the file docker-compose.yml
to see the complete listing of all available containers.
Running composer and php artisan
3. In between in the Workspace container, where you can run commands such as: Artisan, Composer, PHPUnit, Gulp, Node, Npm, etc.
$ docker-compose exec workspace bash
Alternatively, for Windows PowerShell users: Run the following command to access the terminal of any running container:
$ docker exec -it {workspace-container-id} bash
I guide you to access the containers with the default user, laradock, so that the files created have the proper permission level. If accessing without defining the user, root it is used.
Of course, if you need to access a container with privileged access, don't inform the user.
To access as laradock, add --user = laradock
. Example:
$ docker-compose exec --user=laradock workspace bash
Configure your project
4. Update configuring your project to use the database server, the Redis service, and others that you installed and uploaded.
open the file .env
from your PHP project or whatever configuration file your project uses, and set the database host DB_HOST
like mysql.
You need to use Laradock's default database credentials, which can be found in the .env file (eg MYSQL_USER =). Or you can change them and rebuild the container.
an example of .env
from a Laravel project configured to access Mysql from a Laradock container:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=mysql DB_USERNAME=default DB_PASSWORD=secret
Access your project
5. access your project in your browser, visit your localhost address according to the website configuration you set.
In the example in this article the project path in the browser would be http://clientes.test
.
Here's how to configure some of the options .env
file settings .env
Some settings worth mentioning and should be verified by you, adjusted if necessary, in the environment file.
Note that specific settings to enable or disable the use of each application must be performed so that you can use its containers.
For example, to enable the use of XDEBUG in Workspace, you must go to the WORKSPACE section and change the variable WORKSPACE_INSTALL_XDEBUG
for true
.
The same goes for extensions that must be available to the PHP-CLI in the WORKSPACE.
To enable extensions in PHP-FPM, navigate to its section and enable the extensions you want for the PHP-FPM container.
APP_CODE_PATH_HOST
It defines the code path of your application(s) on your host (your machine and not the container).
You will have to change it according to the Laradock installation strategy, see the previous sections to understand what values to use.
The list is arranged in alphabetical order, but the variables are arranged in a different order in the file. .env
.
DATA_PATH_HOST
Sets the storage path on your machine.
For all storage systems. This means that database files, caches, and other files used by applications installed in their containers will be stored in this path.
Applications will use their subdirectories from this setting, by default it is ~/.laradock/data
. That is, from your folder /home/yourusername/
hidden directory for settings and data is created.
If you are using the strategy of one Laradock installation per PHP version, or even one installation per project, you can keep the same data path configuration for all installations. In this way you will be able to share database files and others.
COMPOSE_PROJECT_NAME
Defines the prefix of container names. This is useful if you have multiple projects that use Laradock to have separate containers per project. Or even if you use an installation per PHP version.
PHP_INTERPRETER
The PHP interpreter you want to use, either hhvm or php-fm. I confess that I only used php-fpm.
PHP_VERSION
Sets the PHP version of the workspace and PHP-FPM containers (does not apply to HHVM).
For each Laradock installation, when using one per version, you can configure a different version for PHP, but keep the same version for, for example, the MySQL database management system.
This strategy allows uploading containers in different PHP versions that share the same containers for other applications. This is the beauty of Docker.
keep up to date
Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.
Pingback: How to resolve "COMPOSER_AUTH" does not match the expected JSON schema in Laradock - MAZER.DEV
Pingback: LaraDock error with Mac Silicon computer (MacBook Pro M1) - Docker Desktop Apache2_1 exits with code 2 - MAZER.DEV
Pingback: Connecting Laravel Databases to Laradock - MAZER.DEV
Pingback: Laradock - Docker Containers for your PHP and Laravel development - Introduction - MAZER.DEV