Dockerize your Laravel App or any PHP projects

I have spent dozens of hours understanding how it actually works by experimenting with different commands it has.

Face the same issue as other developers do, I want to run different PHP versions in my computer without installing multiple software like (Xampp, Wamp, easyPHP etc.)

So the solution is Dockerizing my PHP projects.

In this article we’ll cover/learn how to install:

  1. PHP (7.3) — This can be any version you want
  2. MySQL (5.7) — Again you can modify as per your project needs
  3. Nginx Server
  4. Composer
  5. GIT
  6. PHPMyAdmin — for development purpose

Before we get started, make sure you have Docker installed.

I have done all the work for you, just clone this repository and follow the steps mentioned below:

Cone the repository

git clone php-app

Build the services & start the server. This will take a while to download all the images for the first time.

Make sure all the services are running

docker ps

If you see everything is “Up” under the status column then the next step is to test your application.

Head over to the browser and type http://localhost:8080 you should see the following output.

For PHPMyAdmin type http://localhost:8081

MySQL credentials:

Username : homestead
Password : secret

If you want to change this credential you can do it from docker-compose.yml file.

Some useful commands:

  • To SSH to your container use docker exec -it <container_name> /bin/bash
  • Execute commands without login to ssh (For example Laravel app) docker exec -it php-app_app1 php artisan migrate
  • To build the app docker-compose build
  • To restart all the services docker-compose restart
  • To stop all the services docker-compose stop

Once you SHH to your container you can install Laravel using composer. For example:

composer create-project --prefer-dist laravel/laravel lara-app

If you are interested in Apacheserver then instead of Nginx you can clone the following setup:

git clone

You need to follow the same steps as mentioned above to run this setup — no change.

That’s it! Hope you have enjoyed learned something. If you have any suggestions or improvements please leave the comment below and I’ll update my post.

Thanks for reading!

Popular Posts