Docker for Laravel Development

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 ContainerFirst 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).

DockerfileThen 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 homesteadcd /home/zmachmobile/dockerfilesdocker build -t homestead .

*) Notice the dot (.

) at the end of the command, dot represents current directoryWait 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 imagesInstall LaravelInstall Laravel with Composer, make sure you already installed Composer in your computer.

????./home/yourname/projectscd /home/yourname/projectscomposer create-project –prefer-dist laravel/laravel homesteadWait until Laravel finished installing.

Share Project Folder with ContainerOur 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 :Start homestead containerOpen port 5000 in our computer, connect to port 80 in our containerShare 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 homesteadOur container will run in background and now you can check http://localhost:5000.

Our container is liveStart Making ChangesYou can now edit routes/web.

php, changereturn view(‘welcome’); toreturn ‘Hello World’;Save changes and reload your web browserNice, now we are ready to develop !If this post is helpful, show your support by clicking ????.a few times as many as you like.

.

. More details

Leave a Reply