Creating laravel project without installing anything in host machine with the help of docker.
src
: some folder, on our host machine, contains source code of this laravel php app.- php Interpreter (
php
service in our code) container: Thesrc
folder will be exposed to this. This will interpret the php code (src) code and generate response to the incoming request. nginx
: This web server will take all incoming request. And go to php Interpreter & let php interpreter generate response.And send back to the client.sql
: For storing DB. php Interpreter, in the end, needs to communicate with sql.- Application Conatiners need to be up always.
Composer
: Composer to php is like what isnpm
to node. This is a package manager which we can use to install third party packages. We'll use composer to create laravel application. And laravel will use composer to install third party dependencies.Laravel Artisan
: Laravel ships its own tool,Artisan
along with php. This is for run migration against the DB and to write initial data to db.npm
: Laravel usesnpm
for some frontend logics.
Creating A Laravel Project - Guide
docker-compose run --rm composer create-project laravel/laravel .
.
referes to the current folder. So files will be generated in current folder.- The above command will generate laravel code in
src
folder of our host machine.
- Change the
DB_HOST
value asmysql
in side the.env
, found insidesrc
folder as for inter-docker communication, if all the containers are under same network, we can replcaelocalhost
bycontainer_name
(hereservice_name
as we usedocker-compose
). And change the username, password and database name too as shown below. These values we can find underenv/mysql.env
.
docker-compose up -d service1 service2 service3 etc
docker-compose up -d server php mysql
- If we added
depends_on
property under the serviceserver
withphp
andmysql
indocker-compose.yaml
, the command to bring up the services is as follow:
docker-compose up -d --build server
--build
is to rebuild everytime whenever we run the above command, or cached image only will be taken.
- Running containers
- Built/pulled Images
- Generated networks
- Point your browser to
localhost:8000
docker-compose down
When using Docker on Linux, you might face permission errors when adding a bind mount. If you happens, try these steps:
- Change the
php.dockerfile
so that it looks like that:
FROM php:8.3.1-fpm-alpine
WORKDIR /var/www/html
COPY src .
RUN docker-php-ext-install pdo pdo_mysql
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
USER laravel
-
Please note that the
RUN chown
instruction was removed here, instead we now create a user laravel which we use (with theUSER
instruction for commands executed inside of this image / container). -
Also edit the
composer.dockerfile
to look like this:
FROM composer:2.6.6
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
USER laravel
WORKDIR /var/www/html
ENTRYPOINT [ "composer", "--ignore-platform-reqs" ]
-
Here, we add that same laravel user and use it for creating the project therefore.
-
These steps should ensure that all files which are created by the Composer container are assigned to a user named laravel which exists in all containers which have to work on the files.
-
Resolving permision issue
RUN chown -R www-data:www-data /var/www
RUN chmod 755 /var/www