Using Laravel Sail on an existing Laravel application - laravel

Sail Version: 1.16.2
Laravel Version: 9.37
PHP Version: 8.1
Host operating system: macOS 12.3
Description:
I want to use Sail on my existing Laravel application.
As following to the documentation, i need to execute this command to install sail & other composer dependencies:
docker run --rm \
-u "$(id -u):$(id -g)" \
-v $(pwd):/var/www/html \
-w /var/www/html \
laravelsail/php81-composer:latest \
composer install --ignore-platform-reqs
When i run it, everything is going well until composer install crashes with this error:
Install of google/apiclient-services failed
In Filesystem.php line 314: Could not delete /var/www/html/vendor/composer/cc83f9bf/googleapis-google-api-php-client-services-52b4942/src/OrgPolicyAPI:
Steps To Reproduce:
Install new laravel application
Delete vendor folder if you have one
Add "google/apiclient": "^2.12.6" to your composer.json
Execute the docker run command above
Error occurs
Can someone help me solving this?

Related

Install Imagick in Laravel Sail (Docker)

I am trying to install Imagick PHP Extension in Laravel Sail but I am failing to do so.
Details:
Laravel 9
PHP 8.2
In the Dockerfile (in directory docker/8.2) I added this snippet to the script:
RUN apt-get update; \ # Imagick extension apt-get install -y libmagickwand-dev; \ pecl install imagick; \ docker-php-ext-enable imagick; \ # Success true
Did run sail build and it runs the script without errors but it does not detect Imagick
Solved. I forgot to point to the published Dockerfile in docker-composer.yml. :/

Cant set up php enviroment in Docker

When I run
docker run --rm -v $(pwd):/opt -w /opt laravelsail/php81-composer:latest composer install
to install all the composer dependencies,
these popped up
~/Crowdstage$ docker run --rm -v $(pwd):/opt -w /opt laravelsail/php81-composer:latest composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Your lock file does not contain a compatible set of packages. Please run composer update.
Problem 1
- spatie/image is locked to version 2.2.2 and an update of this package was not requested.
- spatie/image 2.2.2 requires ext-exif * -> it is missing from your system. Install or enable PHP's exif extension.
Problem 2
- spatie/laravel-medialibrary is locked to version 10.3.4 and an update of this package was not requested.
- spatie/laravel-medialibrary 10.3.4 requires ext-exif * -> it is missing from your system. Install or enable PHP's exif extension.
To enable extensions, verify that they are enabled in your .ini files:
-
- /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini
- /usr/local/etc/php/conf.d/docker-php-ext-pcntl.ini
- /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
- /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
What is the proper way to set up PHP environment in docker for laravel ?
If I understand correctly, you want to run composer install inside the php container. You can do this by running
docker-compose exec laravel.test "composer install"
if laravel.test is the container name
This is how you can install composer dependencies in a cloned project
docker run --rm -v $(pwd):/opt -w /opt laravelsail/php81-composer:latest composer install --ignore-platform-reqs

How do I setup Laravel on Docker for local development?

I have been trying for the last few days to set up Laravael for Docker on my WSL2 enabled machine. After digging through various bloated stacks - I've tried to build my own stack for local development. My issue is I cannot both mount the uncompiled Laravel folder to the container, and also install dependencies via composer. Below are my current set of files. I cannot access the default application because autoload.php has not been created by composer. If I copy the files to the container via dockerfile and then proceed to run composer dependencies, I end up with a static application that will not reflect changes as I make them in VSCode.
For clarification, my goal is simply to be able to edit my Laravel application without needing to re-build the image everytime.
dockerfile
FROM php:7.4.14-apache
RUN apt-get update -y && apt-get install -y zip unzip
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install Laravel
# VOLUME ./laravel /var/www/html
# WORKDIR /var/www/html
# RUN composer install
docker-compose.yml
version: '3'
services:
web:
build:
context: .
dockerfile: dockerfile.httpd
ports:
- '80:80'
volumes:
- './laravel/public:/var/www/html'
db:
image: mariadb
restart: always
environment:
- MYSQL_ROOT_PASSWORD=my_secure_pwd
- MARIADB_USER=mydbuser
- MARIADB_DATABASE=laravel
- MARIADB_PASSWORD=mydbuserpwd

E: Package 'mysql-client' has no installation candidate in php-fpm image build using docker compose

Im fairly new to docker and so im trying to learn more about it using a laravel project, im following this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose
Ive adjusted the Dockerfile a bit from what the tutorial has but even the tutorial file causes the same result.
FROM php:7.3-fpm
# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/
# Install dependencies
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - && \
apt-get update && apt-get install -y mysql-client \
RUN npm install -g npm
# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
# Install extensions
RUN docker-php-ext-install pdo pdo_mysql
# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www
# Copy existing application directory contents
COPY . /var/www
# Copy existing application directory permissions
COPY --chown=www:www . /var/www
# Change current user to www
USER www
# Set working directory
WORKDIR /var/www
# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]
But i keep getting the following error when i run docker-compose up -d:
E: Package 'mysql-client' has no installation candidate
ERROR: Service 'app' failed to build: The command '/bin/sh -c curl -sL https://deb.nodesource.com/setup_10.x | bash - && apt-get update && apt-get install -y mysql-client nodejs build-essential vim git curl' returned a non-zero code: 100
Am i missing something?
I expected this to work since i am running apt-get update before installing mysql-client.
Thanks.
php:7.3-fpm now use Debian 10 (Buster) as its base image and Buster ships with MariaDB, so just replace mysql-client with mariadb-client should fix it.
If you still want to use the mysql client, it's called default-mysql-client now.
php:7.2-apache triggers the error as well, but I resolve it using php:7.2.18-apache
it worked for me: sudo apt-get update && apt-get install -y git curl libmcrypt-dev default-mysql-client
or alternatively apt-cache search mysql-server
find out your servers then sudo apt-get install default-mysql-server default-mysql-server-core mariadb-server-10.6 mariadb-server-core-10.6
in my case it was the above codes

How do I install composer in container of docker?

I am new at docker and docker-compose and I am developing a Laravel-project on docker and docker-compose with Laradock as following a tutorial(not sure whether It is a correct way or not to refer this situation though).
I want to install the composer in this environment to be able to use the composer command.
As a matter of fact, I wanted to do seeding to put data into DB that I made by php artisan make:migrate but this error appeared.
include(/var/www/laravel_practice/vendor/composer/../../database/seeds/AdminsTableSeeder.php): failed to open stream: No such file or directory
So I googled this script to find a solution that will solve the error then I found it.
It says, "Do composer dump-autoload and try seeding again", so I followed it then this error appeared.
bash: composer: command not found
Because I have not installed composer into docker-container.
My docker's condition is like this now.
・workspace
・mysql
・apache
・php-fpm
Since I have not installed the composer, I have to install it into docker-container to solve the problem, BUT I have no idea how to install it into docker-container.
So could anyone tell me how to install composer into docker-container?
Thank you.
here is the laradock/mysql/Dockerfile and laravelProject/docker-compose.yml.
ARG MYSQL_VERSION=5.7
FROM mysql:${MYSQL_VERSION}
LABEL maintainer="Mahmoud Zalt <mahmoud#zalt.me>"
#####################################
# Set Timezone
#####################################
ARG TZ=UTC
ENV TZ ${TZ}
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && chown -R mysql:root /var/lib/mysql/
COPY my.cnf /etc/mysql/conf.d/my.cnf
CMD ["mysqld"]
EXPOSE 3306
version: '2'
services:
db:
image: mysql:5.7
ports:
- "6603:3306"
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=true
- MYSQL_DATABASE=laravelProject
- LANG=C.UTF-8
volumes:
- db:/var/lib/mysql
command: mysqld --sql-mode=NO_ENGINE_SUBSTITUTION --character-set-server=utf8 --collation-server=utf8_unicode_ci
web:
image: arbiedev/php-nginx:7.1.8
ports:
- "8080:80"
volumes:
- ./www:/var/www
- ./nginx.conf:/etc/nginx/sites-enabled/default
volumes:
db:
You can build your own image and use it in your Docker compose file.
FROM php:7.2-alpine3.8
RUN apk update
RUN apk add bash
RUN apk add curl
# INSTALL COMPOSER
RUN curl -s https://getcomposer.org/installer | php
RUN alias composer='php composer.phar'
# INSTALL NGINX
RUN apk add nginx
I used the PHP alpine image as my base image because it's lightweight, so you might have to install other dependencies yourself. In your docker-compose file
web:
build: path/to/your/Dockerfile/directory
image: your-image-tag
ports:
- "8080:80"
volumes:
- ./www:/var/www
- ./nginx.conf:/etc/nginx/sites-enabled/default
You could do something like this:
FROM php:8.0.2-apache
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y mariadb-client libxml2-dev
RUN apt-get autoremove -y && apt-get autoclean
RUN docker-php-ext-install mysqli pdo pdo_mysql xml
COPY --from=composer /usr/bin/composer /usr/bin/composer
the argument COPY --from= should solve your problem.
FROM php:7.3-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN alias composer='php /usr/bin/composer'

Resources