Lumen by Laravel is a PHP micro-framework for creating APIs and microservices. With Lumen you can enjoy a robust development environment which contains many core Laravel features like the artisan cli, routing, middleware, migrations, eloquent ORM and much more.
In this tutorial, we will install Lumen with Composer, containerize our Lumen app + MySQL with Docker and go over some examples of interacting with our dockerized Lumen project. In the first part of this tutorial, we will focus on installing our Lumen app and get it up and running with Docker.
Install Lumen with Composer
First, make sure you have Composer installed by running the following command in your terminal:
composer -v # or composer --versioncontent_copyTerminal
Install Lumen by running:
composer create-project --prefer-dist laravel/lumen lumen-appcontent_copyTerminal
Now, if all goes well your Lumen project was successfully created inside /path/to/your/lumen-app/ and you can cd to your project. Now, start your Lumen project by running the following PHP command in your project's root directory:
php -S localhost:8000 -t publiccontent_copyTerminal
Visit localhost:8000 or use something like Postman to verify you get a 200 response.
Docker setup
First, make sure you have Docker installed and running:
docker -v # or docker --versioncontent_copyTerminal
We will be creating a docker-compose.yml file so you need to make sure you have docker-compose enabled/installed. If you’re using Docker Desktop for Windows or Mac, you probably already have docker-compose up and running. In any case, check the availability of the docker-compose command with:
docker-compose -v # or docker-compose --versioncontent_copyTerminal
Now, let’s dockerize our app with a Dockerfile and a docker-compose.yml file.
Dockerfile
Create a file named Dockerfile in your project's root directory:
FROM php:7.3-fpm-alpine WORKDIR /var/www/html/ RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer COPY . . RUN composer installcontent_copyDockerfile
In our Dockerfile we set our container to run PHP 7.3. Then, install Composer, copy all our project’s files to the container and finish by running composer install to install Lumen's dependencies.
docker-compose.yml
Now, create a docker-compose.yml file in your project’s root directory:
version: '3.5' services: lumen: ports: - "8000:8000" volumes: - .:/var/www/html - /var/www/html/vendor/ build: . command: php -S lumen:8000 -t public restart: alwayscontent_copyYAML
Before building and running our Lumen project on Docker, it is useful to check that you have your machine’s 8000 port free for use. You can check that by running:
docker pscontent_copyTerminal
If one of your docker containers is listening on 8000, stop/kill that container or modify the port configs in your docker-compose.yml file.
You can now remove the /vendor/ folder from your project’s directory and run:
docker-compose up --buildcontent_copyTerminal
Now, you can visit localhost:8000 and start creating an amazing API/microservice!
In the next part of this tutorial, we will add MySQL as a service to our docker-compose.yml file and start interacting with our dockerized app.
Here is a link to the GitHub repository for this tutorial:
👉 https://github.com/yossi-abramov/lumen-mysql-docker