Docker for Laravel Development

Zamahsyari
Feb 22 · 3 min read

If you are PHP developer, chance you know Laravel are high. Laravel is one of the most popular PHP Framework today. Getting started with Laravel is easy, make sure you have meet all requirements, and composer create-project --prefer-dist laravel/laravel something .

But to meet all requirements is something else, if you are using Windows and XAMPP, it is hard to fulfil all the requirements, you have to manually install missing PHP extensions, or you are Mac person or Linux version, having installed different version of PHP, and the brand new Laravel version require you to upgrade your PHP into PHP 7.2, but you don’t want to mess with your old project that still running on PHP 5.4. What should you do?

I know, install Laravel Homestead, sounds perfect, right? Laravel Homestead powered by Vagrant, or virtual machine, so you just have to download Vagrant, download Laravel Homestead Vagrant box, run it, the VM is alive, there you go!

Yes, it will run, but I’d rather say it is overkill! You don’t have to emulate the whole machine to run just one project. You can actually share your computer resource to run your project, rather than you have to allocate some amount of your computer resource. Use Docker instead !

What will we do?

If you haven’t already know Docker, think Docker like a smart virtual machine. Docker will create our development image (container) and run it like a virtual machine. Our container will contains OS, PHP, and Nginx.

Building the Container

First of all, we will make a script to provision our container, create a new file called Dockerfile in one of your folder e.g. /home/yourname/dockerfiles. Copy-paste the following script to your Dockerfile file. We will use Alpine Linux as our base image because of its small size (~5 MB, compared with ~80 MB as in ubuntu base image).

Dockerfile

Then create a new file called default.conf in the same folder as our previous Dockerfile (/home/yourname/dockerfiles), which it is the Nginx configuration for our Nginx server. Copy-paste the following config to your default.conf file. We will replace default Nginx configuration in our container with our Laravel Nginx configuration.

Make sure Docker is installed and running, then hit following command to create our container image, we named it homestead

cd /home/zmachmobile/dockerfiles
docker build -t homestead .

*) Notice the dot (.) at the end of the command, dot represents current directory

Wait for a moment until the image creation completed. You can check the already created images with following command, you will see homestead images within the list.

docker images

Install Laravel

Install Laravel with Composer, make sure you already installed Composer in your computer.

🎉 /home/yourname/projects
cd /home/yourname/projects
composer create-project --prefer-dist laravel/laravel homestead

Wait until Laravel finished installing.

Share Project Folder with Container

Our container is finished, our environment is ready, yeaaay 🎉 . Now we are ready to code, but connect our newly created Laravel project to our container first. What we will do is as follow :

  1. Start homestead container
  2. Open port 5000 in our computer, connect to port 80 in our container
  3. Share our project folder (/home/yourname/projects/homestead) with our container’s default nginx project folder (/var/www/html)

Hit your terminal with this command :

docker run -d \
-p 5000:80 \
-v /home/yourname/projects/homestead:/var/www/html \
homestead

Our container will run in background and now you can check http://localhost:5000.

Our container is live

Start Making Changes

You can now edit routes/web.php, change

return view(‘welcome’); 

to

return ‘Hello World’;

Save changes and reload your web browser

Nice, now we are ready to develop !

If this post is helpful, show your support by clicking 👏 a few times as many as you like.

Zamahsyari

Written by

Software Engineer, Web Developer, App Developer, Tech Enthusiast

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade