How to create a dockerized Laravel app
In this article we will see how to download and configure Docker and Docker-compose on your Linux PC, and then quickly have a local server for the development of our Laravel applications.
Laravel from version 8 makes use of containers, for this we will have to equip ourselves with Docker. In the case of Linux, it is necessary to refer to the different versions and architectures supported, each version has a series of tricks that differ from the others. From this article, follow the path relative to your version. Below we will install Docker on Ubuntu.
We remove previous versions, update the packages, and install the necessary ones, after which we give the daemon root permissions.
$ sudo apt-get remove docker docker-engine docker.io containerd runc
$ sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
$ sudo apt-get install docker-compose
To create a new Laravel application in a directory named “timesheet-app”, run the following command in the terminal:
curl -s https://laravel.build/timesheet-app | bash
cd example-app/vendor/bin
./sail up
When the terminal finally tells us “PHP 8.0.0 Development Server (http://0.0.0.0:80) started”, we will be able to visit the root page of our Laravel project, available at http: // localhost.
At this point, if we open another terminal, we can use Docker’s own commands to interact with containers or with the Laravel environment. For example:
//shut down container
./sail down
//start container in detached mode
./sail up -d
//create app key
./sail artisan key:generate
//migrate database
./sail artisan migrate
If the last command fails, we must connect to port 3306 of our PC with any SQL client (eg Adminer or MySql Workbench) and create the database.
The database name is visible in the .env file “DB_DATABASE = example_app”, the login credentials with the client are DB_USERNAME = root B_PASSWORD = (generally the latter is empty).
Installation of multiple environments with Sail Laravel
If you have already previously started a Laravel application with the method described above, starting a second environment could create some problems, in particular you will see that the MYSQL container is terminated immediately after launch due to a plugin error. in this case we will have to go to the new project folder and modify the docker-compose.yml file in the two lines where the value of the sailmysql volume occurs, giving it a different name.
volumes:
- 'example-sailmysql:/var/lib/mysql'
.
.
.
networks:
sail:
driver: bridge
volumes:
timesheet-sailmysql:
driver: local
sailredis:
driver: local