Laravel Docker

broken image


Prerequisites

Larapacket is an open-source GitHub repository that has a docker ready configuration to setup all the components mentioned above and getting your environment ready for laravel application. Larapacket is a drop-in files for Laravel applications to start using Docker in development and production. Docker and Laravel setup: Here we will handle all the setup requirements of Docker and Laravel. After the setup, we can run Docker for the first time. We need to create a new.env file for Docker to run. A default file is supplied with Laradock, it's named ‘env-example'. We will make a copy of this file and name it.env.

Before you start, you need to have some certain prerequisites in place:

Docker Composer Php

Laravel: Docker + Cron Jobs When we are using Docker for running our Laravel project, we must take into account that just one php-fpm process is running and we don't have access to some crontab and we definetely don't want to make use of the host for running a PHP process. Laravel Sail is a light-weight command-line interface for interacting with Laravel's default Docker configuration. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience. Already a Docker expert?

  • PHP 7.2+
  • Local machine running the latest version of Docker.

Get Started

Let's create a Laravel project and then see how we can dockerize it using Docker.

Laravel Requirements

The Laravel framework has a few system requirements:

  • PHP >= 5.5.9
  • PHP Extensions:
    • OpenSSLup
    • PDO
    • Mbstring
    • Tokenizer

Step 1 - Install Laravel

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

Install Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. To a install it quickly, just open up a terminal and run this command:

Composer is multi-platform and it run equally well on Windows, Linux and macOS. Here you will get more information regarding how to install composer in each platform:

Step 2 - Create a Laravel Project

Via Laravel Installer

First, download the Laravel installer using Composer:

Once installed, you can use the laravel new command to create a fresh laravel project.

Via Composer Create-Project

Alternatively, you may also install Laravel by issuing the Composer create-project command in your terminal:

Here is a brief breakdown of the directories in a common Laravel application:

  • app/: This is the source folder where our application code lives. All controllers, policies, and models are inside this folder
  • bootstrap/: Holds the application's startup script and a few class map files
  • config/: Holds the app's configuration files. These are usually not modified directly but instead, rely on the values set up in the .env (environment) file at the root of the app
  • database/: Houses the database files including migrations, seeds and test factories
  • public/: Publicly accessible folder holding compiled assets and of course an index.php file
  • resources/: Contains front-end assets such as javascript files, language files, CSS/SASS files and all templates used in the application (called blade templates)
  • routes/: All routes in the application are inside here. There are a few different 'scopes' of routes but the one we will be focusing on is the web.php file
  • storage/: All temporary cache files used by the application, session files, compiled view scripts and log files
  • tests/: Contains test files for the application such as unit tests and functional tests.
  • vendor/: All dependency packages installed with composer

Then, to install all dependencies and run the project, you need to run the following commads:

You will see your Laravel server running on: http://127.0.0.1:8000

Congratulations, you've created a Laravel project!

Note: For more extensive instructions on setting up Laravel, refer to the Laravel documentation here.

Step 3 - Dockerize the Project

Setup Docker

Docker Run Image

Before creating a container for the Laravel application and shipping it off, you need to install Docker on your local machine. For learning purpose, you will install Docker Community Edition. Select your OS from the list below and follow the setup instructions:

Make the docker App image

Laravel docker-compose

The next stage to improve your docker php development workflow is adding a Dockerfile to your project. The structure of a Dockerfile can be considered a series of instructions on how to build your container/image.

Start the Dockerfile by creating an empty file named Dockerfile in the root of your project. Then, complete each instruction according to the following example:

Building and Running the Container

Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile and leave you with a container that is ready to start.

To build your php laravel docker container, you will use the docker build command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.

The final step is to run the container you have just built using Docker:

The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. After you run this command, you should be able to visit http://localhost:8000 in your browser.

You can see the Docker containers that are currently running on your system (along with their Container IDs) with:

To turn off your Docker container, run:

Push to cloud

1. Create your app

In order to install your docker php example, just create a new app via cli or admin panel and set a port to 8000.

2. Push your docker container

Laravel

3. Set up resources

4. Logs and app status

5. Release your app

After to click on the release button, your php docker tutorial will be deployed, Just click on the generated URL and you will get your app running.

Now you can deploy your php laravel app without a massive build time.

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.


Image
Laravel Docker

The next stage to improve your docker php development workflow is adding a Dockerfile to your project. The structure of a Dockerfile can be considered a series of instructions on how to build your container/image.

Start the Dockerfile by creating an empty file named Dockerfile in the root of your project. Then, complete each instruction according to the following example:

Building and Running the Container

Building the container is very straight forward once you have Docker and Docker Machine on your system. The following command will look for your Dockerfile and download all the necessary layers required to get your container image running. Afterwards, it will run the instructions in the Dockerfile and leave you with a container that is ready to start.

To build your php laravel docker container, you will use the docker build command and provide a tag or a name for the container, so you can reference it later when you want to run it. The final part of the command tells Docker which directory to build from.

The final step is to run the container you have just built using Docker:

The command tells Docker to run the container and forward the exposed port 8000 to port 8000 on your local machine. After you run this command, you should be able to visit http://localhost:8000 in your browser.

You can see the Docker containers that are currently running on your system (along with their Container IDs) with:

To turn off your Docker container, run:

Push to cloud

1. Create your app

In order to install your docker php example, just create a new app via cli or admin panel and set a port to 8000.

2. Push your docker container

3. Set up resources

4. Logs and app status

5. Release your app

After to click on the release button, your php docker tutorial will be deployed, Just click on the generated URL and you will get your app running.

Now you can deploy your php laravel app without a massive build time.

Bonus 1: SSL certificate for HTTPS

It's already done for you. If you need to connect your custom domain, SSL certificate will be provided for it.


Bonus 2: Autoscaling

With autoscaling the app will be scaled up when CPU and RAM load goes up and scaled down when it goes down.

Now you can deploy your php laravel app without a massive build time.





broken image