Let's start using a
docker-compose.yml file to get started with Docker quickly.
In this video, we use the Shipping Docker Github repository to get up and running quickly. This repository includes a
docker-compose.yml file and a build process for 2 images.
We basically follow the instructions in the Github repository to create an environment that we can use to start hacking on a PHP application.
1. Get the files and spin up containers
# Get shipping-docker files git clone email@example.com:shipping-docker/php-app.git cd php-app # Start the app, run containers # in the background # This will download and build the images # the first time you run this docker-compose up -d
At this point, we've created containers and have them up and running. However, we didn't create a Laravel application to serve yet. We waited because we wanted a PHP image to get created so we can re-use it and run composer commands.
2. Create a new Laravel application
# From directory "php-app" # Create a Laravel application docker run -it --rm \ -v $(pwd):/opt \ -w /opt \ --network=phpapp_appnet \ shippingdocker/php \ composer create-project laravel/laravel application docker run -it --rm \ -v $(pwd)/application:/opt \ -w /opt \ --network=phpapp_appnet \ shippingdocker/php \ composer require predis/predis
-vflag is sharing the current directory (the
$(pwd)command expands out to the current directory) with the
/optdirectory inside the container. We know a
/optdirectory exists in the container because it's a directory common to all flavors of Linux.
-wflag sets the "working-directory" to
/opt. In other words, when the container spins up, all commands run will be relative to
Since we shared our host machine's current directory (which includes our Laravel code files) with the container's
/optdirectory, and we set the working directory to
/opt, we know any commands we run, such as
composer requirewill be run from the directory containing our code.
Windows users may need to define their current directory manually. This means replacing
$(pwd)with a file path such as
C:/Path/To/Code/Files. Note the use of back-slashes instead of forward-slashes. Read more about that here.
Edit the application/.env file to have correct settings for our containers. Adjust the following as necessary:
DB_CONNECTION=mysql DB_HOST=db DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret BROADCAST_DRIVER=log CACHE_DRIVER=redis SESSION_DRIVER=redis QUEUE_DRIVER=sync REDIS_HOST=redis REDIS_PASSWORD=null REDIS_PORT=6379
If you already have an application, you can move it to the application directory here. Else, you can adjust the shared volume file paths within the
If you edit
docker-compose down; docker-compose up -dto suck in the new Volume settings.
3. Add Auth Scaffolding:
We can add Laravel's Auth scaffolding as well. To do that, we need to run some Artisan commands:
# Scaffold authentication views/routes docker run -it --rm \ -v $(pwd)/application:/opt \ -w /opt \ --network=phpapp_appnet \ shippingdocker/php \ php artisan make:auth # Run migrations for auth scaffolding docker run -it --rm \ -v $(pwd)/application:/opt \ -w /opt \ --network=phpapp_appnet \ shippingdocker/php \ php artisan migrate
Now we can start using our application! Head to http://localhost/register to see your Laravel application with auth scaffolding in place.