Unable to run laravel command on docker with composer - laravel

I'm having some issues with running the laravel command on my docker container.
I use the php docker image and use the copy command for getting composer from the composer image. After that I've added composer to my $PATH variable and run the composer global require laravel/installer command.
After building the docker compose file and running it I open up the command line for my php image. In here I try to run the "laravel" command but get the following error: /bin/sh: laravel:not found.
Looking in the $HOME/.config/composer/vendor folder I see laravel here, so I think the installation is correct.
I might be completely wrong here or have made a dumb rookie mistake, so any help is greatly appreciated
Below here is my dockerfile:
FROM php:8.0.14-apache
RUN docker-php-ext-install pdo pdo_mysql
#apache
RUN a2enmod rewrite
#composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
#add composer to path
ENV PATH="$PATH:$HOME/usr/local/bin/composer"
RUN export PATH="$PATH:$HOME/.composer/vendor/bin"
#update
RUN apt-get update
RUN apt-get -y install nano
#add nodejs
RUN apt-get -y install nodejs npm
COPY . /var/www/html/
RUN npm install
#install laravel
RUN composer global require laravel/installer

You copy composer to /usr/local/bin/composer, but you add $HOME/usr/local/bin/composer to the path.
Also, RUN export ... doesn't do anything, because each RUN statement is run in a separate shell. So when the RUN command is done, the shell exits and the exported variable is lost.
Try this
FROM php:8.0.14-apache
RUN docker-php-ext-install pdo pdo_mysql
#apache
RUN a2enmod rewrite
#composer
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
#add composer to path
ENV PATH="$PATH:/usr/local/bin/composer"
#update
RUN apt-get update
RUN apt-get -y install nano
#add nodejs
RUN apt-get -y install nodejs npm
COPY . /var/www/html/
RUN npm install
#install laravel
RUN composer global require laravel/installer

I've added the "changed path" from the command composer global about to my ENV path and added /vendor/bin. I'm not sure if its bad practise to add something from the root folder to the $PATH variable.
So the complete line looks like this:
ENV PATH="$PATH:/root/.config/composer/vendor/bin"
By adding this line i'm able to run the laravel command

Related

Laravel Squash Migrate Error pg_dump not found

i have 3 container (php-fpm, nginx, postgresql) for my laravel app, i execute command php artisan schema:dump on php-fpm container and I found error pg_dump not found
I have had similar issue but with mysql. What solved it for me was an installation of mysql-client inside the container in which I was trying to execute php artisan schema:dump command (I have added RUN apt-get install -y mysql-client inside my Dockerfile, then I have rebuilt all the containers). I think that you need to do similar thing, e.g. RUN apt-get install -y postgresql-client inside your Dockerfile.

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

Docker and laravel not clearing the cache

I have an app running with docker and laravel but there are some instructions that apparently are executed but not functioning.
These instructions are php artisan clear:cache and php artisan optimize.
Sometimes I need to get into the container and run these instructions in order to have my changes reflected in the browsers. Apart from the failures that kubernetes detects due to the unreflected changes
FROM php:7.2.0-fpm as php
RUN apt-get update && apt-get install -y openssl openssh-client \
rsync sshpass zip unzip git vim libmcrypt-dev mysql-client \
libssh2-1-dev libmagickwand-dev \n
gnupg2 --no-install-recommends zlib1g-dev sass \
&& pecl install mcrypt-1.0.1 \
&& docker-php-ext-enable mcrypt \
&& pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install gd \
&& docker-php-ext-install zip \
&& pecl install ssh2-1.0 \
&& docker-php-ext-enable ssh2
RUN curl --silent --location https://deb.nodesource.com/setup_11.x | bash -
RUN apt-get install -y nodejs
RUN npm install --global bower
WORKDIR /var/www/html
COPY install-composer.sh install-composer.sh
RUN bash ./install-composer.sh
COPY composer.json ./
COPY composer.lock ./
RUN composer install --no-scripts --no-autoloader
COPY . ./
RUN composer update
RUN composer install
COPY zz-docker.conf /usr/local/etc/php-fpm.d/zz-docker.conf
RUN mkdir -p /var/run/php/sock/
RUN sass resources/sass/style.scss:public/assets/stylesheets/style.css
RUN bower install --allow-root
RUN composer dump-autoload
RUN php artisan vendor:publish --provider="Sentry\SentryLaravel\SentryLaravelServiceProvider"
ONBUILD chown -R www-data:www-data .
RUN php artisan optimize && php artisan config:cache \
&& php artisan view:cache && php artisan view:clear
Most of the time I need to get into the container and run the php artisan optimize, view:cache and view:clear again even though they are in the Dockerfile so it shouldn't be necessary to do it all over again
Any commands that are being executed using RUN in the Dockerfile will be executed only during the build phase. If you wish to run them while starting a container so you don't have to run them manually then you need to use a script e.g. bash script that can be used as an ENTRYPOINT for your Dockerfile and then make this script execute the command that you should run to start the application. So your entrypoint.sh can look like this:
# entrypoint.sh
#!/usr/bin/env sh
...
php artisan optimize
php artisan config:cache
php artisan view:cache
php artisan view:clear
...
php artisan serve # or use exec "$#" and pass php artisan serve through CMD in Dockerfile
This will make every container that you start execute all of these commands before start serving your application.
Other links that you might want to check:
What is the difference between CMD and ENTRYPOINT in a Dockerfile?
What does set -e and exec “$#” do for docker entrypoint scripts?
You can also use the cmd:
sudo docker-compose exec php php /var/www/html/path_to_laravel_project_dir/artisan config:cache
it will execute the artisan cmd in the container.
Just ran into a problem of cache:clear with Laravel too (no Docker involved in my case)
Fact is : if Artisan has no rights to clear caches, it just silently fails, proudly telling you cache have been successfully cleared (sigh).
See https://github.com/laravel/framework/issues/1179 for more details
*In my case, adding simple "sudo" before my command did the trick... but I lost half an hour understanding the cause (what an introduction to Laravel/Artisan :p)
*I have a Laravel v6.5

composer install - define directory

I'm running a shell script to execute composer install command.
my composer.json file is in /var/www/html
Is there a way I can tell composer not to run under the current directory and to run the composer.json in /var/www/html ?
This is what i've done - but it feels wrong:
cd /var/www/html && composer install
You can change directory to /var/www/html and then do the composer install like so:
cd /var/www/html && composer install
UPDATE
Actually a better way to do it would be using --working-dir or -d option.
composer install -d=/var/www/html
or
composer install --working-dir=/var/www/html

Remove composer

I installed composer while trying to install cakePhp, but the installation was not successful and I want to uninstall composer.
I am not finding any way to do this.
For the installation I used the command curl -s https://getcomposer.org/installer | php
I am working in linux
During the installation you got a message
Composer successfully installed to: ... this indicates where Composer was installed. But you might also search for the file composer.phar on your system.
Then simply:
Delete the file composer.phar.
Delete the Cache Folder:
Linux: /home/<user>/.composer
Windows: C:\Users\<username>\AppData\Roaming\Composer
That's it.
Uninstall composer
To remove just composer package itself from Ubuntu 16.04 (Xenial Xerus) execute on terminal:
sudo apt-get remove composer
Uninstall composer and it's dependent packages
To remove the composer package and any other dependant package which are no longer needed from Ubuntu Xenial.
sudo apt-get remove --auto-remove composer
Purging composer
If you also want to delete configuration and/or data files of composer from Ubuntu Xenial then this will work:
sudo apt-get purge composer
To delete configuration and/or data files of composer and it's dependencies from Ubuntu Xenial then execute:
sudo apt-get purge --auto-remove composer
https://www.howtoinstall.co/en/ubuntu/xenial/composer?action=remove
Additional information about removing/uninstalling composer
Answers above did not help me, but what did help me is removing:
~/.cache/composer
~/.local/share/composer
~/.config/composer
Hope this helps.
If you install the composer as global on Ubuntu, you just need to find the composer location.
Use command
type composer
or
where composer
For Mac users, use command:
which composer
and then just remove the folder using rm command.
curl -sS https://getcomposer.org/installer | sudo php
sudo mv composer.phar /usr/local/bin/composer
export PATH="$HOME/.composer/vendor/bin:$PATH"
If you have installed by this way simply
Delete composer.phar from where you've putted it.
In this case path will be /usr/local/bin/composer
Run this command
sudo rm /usr/local/bin/composer/composer.phar
Note: There is no need to delete the exported path.
Find the Location of the Composer by typing this command
whereis composer
Then You will get the output like this
composer: /usr/local/bin/composer
then cd /usr/local/bin/
then remove composer by this command
sudo rm -r composer

Resources