Laradock is a community maintained set of Docker containers that we can use in our projects. Here I'm going to show what it takes to a couple of projects running usinng the same laradock setup.
I'm not gonna be building anything new here, so I'm going to create two new laravel applications:
laravel new project-a --dev
laravel new project-b --dev
Next, we need to clone the laradock repo:
git clone https://github.com/Laradock/laradock.git
We need to copy the default env configuration files for our containers:
cp laradock/env-example laradock/.env
After doing this, our project structure should be:
tree -L 1
.
├── laradock
├── project-a
└── project-b
3 directories, 0 files
We are going to be using nginx from Laradock, so we need to configure the sites:
cp laradock/nginx/sites/laravel.conf.example laradock/nginx/sites/project-{a,b}.dev.conf
This should copy the existing Laravel nginx site config into project-a.dev.conf
and project-b.dev.conf
. We now need to point the two hosts to the document root on nginx, so open both configuration files in your editor and change these two entries:
project-a:
server_name project-a.dev;
root /var/www/project-a/public;
project-b:
server_name project-b.dev;
root /var/www/project-b/public;
Ok, almost there. Now we need to change our hosts file and point your domains to your host machine local IP address, something like this:
192.168.0.7 project-a.dev project-b.dev
After that, we can can start the containers:
cd laradock
docker-compose up -d nginx mysql
The nginx container depends on the fpm container, so that one will automatically be started as well.
If you open http://project-a.dev
in your browser, you should see the Laravel welcome page. If you saw an error page, that probably means you don't have the correct permissions on your storage folder, so run sudo chmod -R 777 {project-a,project-b}/storage
..
Our laravel projects are not yet connected to the database. Since we are using the global laradock setup, we need to create the project databases before we can connect to them and run the migrations. To do so, we need to connect as root in the database.
You can either create using your database management UI tool of preference, or using the terminal. I'll go with the later but you can use the same credentials I'm going to use to connect via your UI of preference.
We don't know the credentials yet, you can those in the laradock/.env
file we copied before. To connect over the terminal, you can use:
mysql -uroot -proot -h127.0.0.1
Yes, you must use the 127.0.0.1
IP instead of defaulting to localhost
, or you could alternatively use your host IP address as I did before with the hosts in the nginx config.
Now, let's create the databases:
> create database projecta;
Query OK, 1 row affected (0.01 sec)
> create database projectb;
Query OK, 1 row affected (0.01 sec)
> grant all on `projecta`.* to 'default'@'%';
Query OK, 0 rows affected (0.01 sec)
> grant all on `projectb`.* to 'default'@'%';
Query OK, 0 rows affected (0.01 sec)
Done. We have created the databases and granted the default
user access to both.
Now we can change our .env
files in our projects, using these credentials:
File: project-a/.env
DB_CONNECTION=mysql
DB_HOST=192.168.0.7
DB_PORT=3306
DB_DATABASE=projecta
DB_USERNAME=default
DB_PASSWORD=secret
File: project-b/.env
DB_CONNECTION=mysql
DB_HOST=192.168.0.7
DB_PORT=3306
DB_DATABASE=projectb
DB_USERNAME=default
DB_PASSWORD=secret
Remember to change the DB_HOST
to use your own local IP address. Now we can the migrations:
project-a $ php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Laradock ships with a workspace container that you can use whenever you want to inspect your app or something. To use it, cd into the laradock folder and run:
docker-compose exec workspace bash
This will give you a shell inside the workspace container, something that can be compared with "SSH'ing into the container", but you have to keep in mind this is not SSH. Now, if you LS the folders, you should see a similar structure:
root@821e495dbc77:/var/www# ls
laradock project-a project-b
Great, CD into the project-a
folder, and now you can run the migrations, for example:
root@821e495dbc77:/var/www# cd project-a/
root@821e495dbc77:/var/www/project-a# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated: 2014_10_12_100000_create_password_resets_table
Now, get back to your host machine, inside the project-a
folder and run:
php artisan make:auth
After that, check out the sign-up page of your app. All should be working.
If, for example, your application needs Redis running as well, you can start a redis container. Before we do so, make sure you still have the other containers running:
$ cd laradock
$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------
laradock_applications_1 /true Exit 0
laradock_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
laradock_nginx_1 nginx Up 0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1 docker-php-entrypoint php-fpm Up 9000/tcp
laradock_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp
If you have rebooted or turned down the containers, you can run docker-compose up -d nginx mysql
again.
Now we are ready to add a new service, run:
docker-compose up -d redis
Creating laradock_redis_1 ...
Creating laradock_redis_1 ... done
Go to your project-a
folder and change the .env
file to use your local host machine IP address as the Redis host, like so:
REDIS_HOST=192.168.0.7
REDIS_PASSWORD=null
REDIS_PORT=6379
Now, let's tinker around with redis:
project-a php artisan tinker
Psy Shell v0.8.11 (PHP 7.1.8-2+ubuntu16.04.1+deb.sury.org+4 — cli) by Justin Hileman
>>> Redis::keys('*');
=> []
>>> Redis::incr('testing');
=> 1
>>> Redis::incr('testing');
=> 2
>>> Redis::incr('testing');
=> 3
>>> Redis::incr('testing');
=> 4
That's it. We have redis working in our local setup. Beaware that you are using the same mysql and the same redis for all your local projects. To avoid issues, make sure you set a different database number in your redis connection, like this:
At project-b/config/database.php
, change the redis config to the following
<?php
return [
// ...
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
];
Then, in your project-b/.env
make sure your Redis database is different than the project-a one:
REDIS_HOST=192.168.0.7
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_DATABASE=1
I didn't change the project-a
redis config, so it's using the database 0
and the project-b
is using the database 1
.