https://github.com/actionanand/laravel_with_docker
Creating laravel project without installing anything in host machine with the help of docker.
https://github.com/actionanand/laravel_with_docker
artisan composer docker docker-compose laravel mysql php
Last synced: 3 months ago
JSON representation
Creating laravel project without installing anything in host machine with the help of docker.
- Host: GitHub
- URL: https://github.com/actionanand/laravel_with_docker
- Owner: actionanand
- License: mit
- Created: 2023-12-28T21:10:54.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-31T17:42:02.000Z (over 2 years ago)
- Last Synced: 2025-04-02T03:28:29.639Z (over 1 year ago)
- Topics: artisan, composer, docker, docker-compose, laravel, mysql, php
- Language: Dockerfile
- Homepage:
- Size: 95.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel with Docker (Section 6)
Creating laravel project without installing anything in host machine with the help of docker.

### 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](https://laravel.com/docs/10.x#creating-a-laravel-project)
```bash
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 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`.

## Bringing up the laravel project
```shell
docker-compose up -d service1 service2 service3 etc
```
```bash
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:
```bash
docker-compose up -d --build server
```
* `--build` is to rebuild everytime whenever we run the above command, or cached image only will be taken.


### Other userful info.
* Running containers

* Built/pulled Images

* Generated networks

### Output
* Point your browser to `localhost:8000`

### Bringing down the docker services
```shell
docker-compose down
```

### Checking the db connection and running migrations

### 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:
```dockerfile
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:
```dockerfile
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
```dockerfile
RUN chown -R www-data:www-data /var/www
RUN chmod 755 /var/www
```
## Associated repos:
1. [Docker Basics](https://github.com/actionanand/docker_playground)
2. [Managing Data and working with volumes](https://github.com/actionanand/docker_data_volume)
3. [Docker Communication](https://github.com/actionanand/docker_communication)
4. [Docker Multi-container with docker-compose](https://github.com/actionanand/docker_multi-container)
5. [Docker Utility Containers & Executing Commands](https://github.com/actionanand/node-util)
6. [Laravel with Docker](https://github.com/actionanand/laravel_with_docker)
7. [Angular with Docker](https://github.com/actionanand/angular_with_docker)