Create Laravel project on Docker

Tsubasa Kondo
3 min readMay 25, 2019

--

0. Preparation

You need to have “git” and “docker” installed before this steps.

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

https://www.docker.com/get-started

1. Setting Laradock

$ cd [YOUR PROJECT PATH]$ git clone https://github.com/LaraDock/laradock.git laradock$ cd laradock$ cp env-example .env
//Edit ".env" as follows.
APP_CODE_PATH_HOST=../
DATA_PATH_HOST=~/.laradock/data
MYSQL_VERSION=5.7
MYSQL_ROOT_PASSWORD=root
$ mv nginx/sites/default.conf nginx/sites/default.conf.bak$ cp nginx/sites/laravel.conf.example nginx/sites/default.conf$ docker-compose up -d nginx mysqlYou can stop and delete docker containers with “$ docker-compose down”.

2A. Clone an existing Laravel project

$ cd [YOUR PROJECT PATH]$ git clone [URL] laravel$ cd laravel$ cp .env.example .env
//Edit ".env" as follows.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_DATABASE=[DBNAME]
DB_USERNAME=root
DB_PASSWORD=root

$ touch .env.testing
//Edit ".env.testing" as follows.DB_HOST=mysql
DB_DATABASE=[DBNAME]_testing
DB_USERNAME=root
DB_PASSWORD=root
*Create the following databases to the "127.0.0.1" mysql server.
CREATE DATABASE `mypj` DEFAULT CHARACTER SET utf8;
CREATE DATABASE `mypj_testing` DEFAULT CHARACTER SET utf8;
$ cd [YOUR PROJECT PATH]/laradock$ docker-compose exec --user=laradock workspace bash
(for Windows $ winpty docker-compose exec --user=laradock workspace bash)
$ cd laravelThis "/var/www/laravel" is the same directory as "[YOUR PROJECT PATH]/laravel". The data on the Docker container disappears when you stop the container. So you need to mount source codes and DB data on your PC. This "/var/www/laravel" is the source codes on the docker container and the "[YOUR PROJECT PATH]/laravel" is the source codes mounted on your PC. In addition, the DB data is stored in "~/.laradock/data/" on your PC.$ composer install$ php artisan key:generate$ php artisan migrate --seed
or (php artisan migrate && php artisan db:seed)
$ php artisan migrate --seed --env=testing$ ./vendor/bin/phpunit$ exit

2B. Create a new Laravel Project

$ cd [YOUR PROJECT PATH]$ docker-compose exec --user=laradock workspace bash
(for Windows $ winpty docker-compose exec --user=laradock workspace bash)
$ composer create-project --prefer-dist laravel/laravel laravel$ cd laravel$ cp .env.example .env
//Edit ".env" as follows.
DB_CONNECTION=mysql
DB_HOST=mysql
DB_DATABASE=[DBNAME]
DB_USERNAME=root
DB_PASSWORD=root

You must know why you use Composer.

The source code released on GitHub does not contain the code of the external library. The code of the framework itself is also an external library. Running “composer install” will install those codes into “vendor” directory on your environment.

In general, the “vendor” directory is ignored by git. This means delegating source control of the library to the vendor. For example, if a vulnerability is found in a library, you can fix the vulnerability simply by running “composer update” in your development environment.

There is a library that can be installed with composer in a repository called Packagist. This is the concept of a “package management system”, which is used in many other languages and operating systems. For example, JavaScript uses a package management system called “npm”. Getting used to a package management system is very important for every developers.

--

--

Tsubasa Kondo

I am a Japanese software developer living in Mandalay (Myanmar).