Skip to content

Creating laravel project without installing anything in host machine with the help of docker.

License

Notifications You must be signed in to change notification settings

actionanand/laravel_with_docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel with Docker (Section 6)

Creating laravel project without installing anything in host machine with the help of docker.

image

Application Containers

  • src: some folder, on our host machine, contains source code of this laravel php app.
  • php Interpreter (php service in our code) container: The src 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.

Utility Containers

  • Composer: Composer to php is like what is npm 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 uses npm for some frontend logics.

Creating A Laravel Project

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.

image

image

  • Change the DB_HOST value as mysql in side the .env, found inside src folder as for inter-docker communication, if all the containers are under same network, we can replcae localhost by container_name (here service_name as we use docker-compose). And change the username, password and database name too as shown below. These values we can find under env/mysql.env.

image

Bringing up the laravel project

docker-compose up -d service1 service2 service3 etc
docker-compose up -d server php mysql
  • If we added depends_on property under the service server with php and mysql in docker-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.

image image

Other userful info.

  • Running containers

image

  • Built/pulled Images

image

  • Generated networks

image

Output

  • Point your browser to localhost:8000

image

Bringing down the docker services

docker-compose down

image

Checking the db connection and running migrations

image

Fixing error if happens

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 the USER 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

Associated repos:

  1. Docker Basics
  2. Managing Data and working with volumes
  3. Docker Communication
  4. Docker Multi-container with docker-compose
  5. Docker Utility Containers & Executing Commands
  6. Laravel with Docker
  7. Angular with Docker

About

Creating laravel project without installing anything in host machine with the help of docker.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published