Tutorial Laravel 6 with Docker and Docker-Compose

Tutorial Laravel 6 with Docker and Docker-Compose

In this tutorial, we'll learn how we can use Docker and Compose to develop and run Laravel 6 applications locally.

Following this tutorial you'll learn:

Docker is a container tool that allows developers and organizations to build, run and deploy applications using containers.

Containers are standalone images that allow developers to package applications with their dependencies. They share the kernel and resources of the system where they are running and they provide isolated environments for applications.

Docker makes use of <a href="https://morioh.com/p/544a7cbf0be5" target="_blank">Dockerfile</a> files to define and describe the parts and dependencies used by an application and also commands to run in order to install the dependences and start the application.

Installing Docker

start by installing Docker for your type of operating system by following one of these official guides:

Docker-Compose

The official documentation describes docker-compose as:

A tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see the list of features.
Using docker-compose involves three easy steps:

  1. First, you need to define/describe your app environment with a ```Dockerfile``` so you can reproduce it anywhere.
    
  2. Second, you need to create ```docker-compose.yml``` file and define the services required for running your application so you can run them in multi containers.
    
  3. Finally you just need to run ```docker-compose up``` and Compose will start all the services for your application
    
Creating a Laravel 6 Project

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

We have different ways to create Laravel projects: Using Composer (PHP package manager), cloning from GitHub or downloading using cURL.

Using Composer

If you have Composer installed on your system then you can simply use the following command to create a Laravel project:

composer create-project --prefer-dist laravel/laravel laravelproject

Using GitHub Repository

Alternatively if you don't want to install Composer in your system, you can also clone Laravel from GitHub using:

git clone https://github.com/laravel/laravel.git laravelproject

Installing the Project Dependencies

Now, let's install the project's dependencies. If you have Composer installed, simply run the following command from within your project's root folder:

composer install 

If you don't have Composer installed on your system. You can also use Docker with the official composer image to install dependencies by running the following command from the project's root folder:

docker run --rm -v $(pwd):/app composer/composer install

The --rm switch tells Docker to automatically clean up and remove the container after doing the task and exiting.

This is what the docs says about that:

By default a container’s file system persists even after the container exits. This makes debugging a lot easier (since you can inspect the final state) and you retain all your data by default. But if you are running short-term foreground processes, these container file systems can really pile up. If instead you’d like Docker to automatically clean up the container and remove the file system when the container exits, you can add the <em>--rm</em> flag:
The command: -v $(pwd):/app instructs Docker to mount the current directory on the host system retrieved using $(pwd) to /app in the container . What app? Simply because the composer tool in the container will look for a composer.json file in this folder.

After finishing the installation, either way, you'll find a vendor folder with all dependencies inside your Laravel project.

Creating a Dockerfile

Now, after installing Docker and Docker-compose and creating the laravel project let's create a Dockerfile to define our environmenet. Create a new Dockerfile file in the application directory and start by adding the first command:

FROM php:7.2.2-fpm

This tells Docker to download and use the php:7.2.2-fpm image.

Next add:

RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN docker-php-ext-install pdo mcrypt mbstring

This run apt-get to install the dependencies and extensions required by Laravel.

If you want to install Composer you can also add this command:

RUN apt-get -y curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

Now, add:

WORKDIR /app
COPY . /app

This tells Docker to set the working directory in the container to /app and copy the files (Laravel project) in our current folder (in our system host) to the /app folder in the container.

If you didn't install the dependencies using the previous methods you can install them using:

RUN composer install

Now, let's run the artisan serve command using the CMD command:

CMD php artisan serve --host=0.0.0.0 --port=8000

This will serve the Laravel project from the port 8000.

Finally, you need to expose the port 8000 from the container using:

EXPOSE 8000

This is the complete Dockerfile:

FROM php:7.2.2-fpm
RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN docker-php-ext-install pdo mcrypt mbstring
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo mcrypt mbstring
WORKDIR /app
COPY . /app
RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000

Building the Docker Image

After creating the Dockerfile, you are ready to create the actual Docker image. From your terminal, navigate to the root folder of your project and run:

docker build -t my-laravel-image .

Using the -t switch we can specify the tag/name of the Docker image. For the dot means the current folder will be used as the context for the image to be built. Remember the COPY command in the Dockerfile? its source folder is this folder so we can copy the files from our project to the container file-system.

Running the Docker Image Using Docker

After building the Docker image (my-laravel-image) you can now run it using:

docker run -p 8000:8000 my-laravel-image

You can now access your application from your browser at <a href="http://localhost:8000/" target="_blank">http://localhost:8000</a>.

Running the Docker Image Using Docker-compose

Docker-compose is more useful to build multi containers for the same application but it can also used to run a single Docker image. So first, create a docker-compose.yml file in the root folder of your project and add the following content:

web:
    image: my-laravel-image
    ports:
        - 8000:8000
    volumes:
        - ./:/app

We are defining a volume using ./:/var/www which simply mount our current directory ./ in the host system to /app in the container. This will allow you to work on the source code in your project's folder and changes will be reflected in the container file-system (in our case /app) without executing any other commands.

Conclusion

In this tutorial, we've seen how to use Docker to create a development environment with Laravel 6. We've seen how to use both Docker and docker-compose to run our Laravel 6 application in an isolated container.

=========================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

How To Set Up Laravel, Nginx, and MySQL with Docker Compose

Deploying a Laravel app in Kubernetes on Google Cloud

Docker Basics: Docker Compose

Laravel 6 CRUD Application Tutorial

Laravel 6 Image Upload Tutorial

How to use form validation in Laravel 6

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Top Laravel Development Companies Reviews

Top Laravel Development Companies Reviews

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here...

Among the list of Best Laravel development companies, TopDevelopers has chosen the most acclaimed laravel developers for the visitors so as to make them avail the extremely wonderful service ever offered. The web development companies listed here have passed the stringent assessments carried out by TopDevelopers in order to get approved as top Laravel service providers. Get in touch with the most sought-after Laravel developers in the market for your assistance.

Here is the List of Best Laravel Development Companies.