11 Connecting Containers - dchantzis/sfh-docker-in-development-part-1 GitHub Wiki

Source: https://serversforhackers.com/c/div-connecting-containers

We get a Laravel application running.

Stop the running Containers of the application shippingdocker/app:latest

docker ps ls

docker stop <CONTAINER_ID>

We will remove the application directory:

rm -rf application

We will "share" the root directory to be mapped in the container, as we'll install laravel in a new application directory in the root folder:

docker run -it --rm -v %cd%:/var/www/html -w /var/www/html shippingdocker/app:latest composer create-project laravel/laravel application 
  • -w Working directory inside the Container. We specify a working directory so that the Container knows where to install laravael.

Now that laravel is installed in the application directory, we will map application. Please note that by default laravel has a public directory:

docker run --name=app --network=appnet -d --rm -p 80:80 -v %cd%/application:/var/www/html shippingdocker/app:latest
  • -p We set the port to 80:80 (<local_machine_port>:<container_port) instead of 8080 as it doesn't matter for this test, as long as the local port 80 is free.
docker exec -it -w /var/www/html app php -v

docker exec -it -w /var/www/html app php artisan -v

As of Laravel 6.x (and 7.x) the authentication scaffolding is no longer available with php artisan make:auth We will need to install the laravel/ui package via composer:

So instead of (usable up to laravel 5.8):

docker exec -it -w /var/www/html app php artisan make:auth

we need to do the following:

docker exec -it -w /var/www/html app composer require laravel/ui

docker exec -it -w /var/www/html app php artisan ui:auth

and then run the migrations (the auth scaffolding requires it):

docker exec -it -w /var/www/html app php artisan migrate

if the migrations fail, that means we need to update the projects' .env file (inside the application local project root folder):

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=root
DB_PASSWORD=root

Notice that for DB_HOST we use the hostname for the container named mysql that we created earlier.

Now we can try again:

docker exec -it -w /var/www/html app php artisan migrate

Go to localhost:80 check your installed Laravel project, then register, sign-out and log in.

Now we can also check our network:

docker network inspect appnet