A little context: I am new to docker and dont know of any best-practices yet. My task is to create a webservice based on php and i decided to use docker-compose for provisioning.
Because we are all fancy devs, we know - there is no way to build a php application without using composer these days.
My question:
Should i install dependencies in the build? So by adding this to my Dockerfile:
RUN cd /app && composer install --no-interaction --prefer-source --optimize-autoloader
Or should i install dependencies during development and build the container image with included dependencies?
I only know of one way NOT to do it: install dependencies locally on the dev machine and build the container afterwards. But how would the "best practice" look like?
And - because i am a newbee in this field - how would i run a "composer require some/package" for my app service container?
By the way
I also noticed a message "Do not run Composer as root/super user!" when building the container. I added COMPOSER_ALLOW_SUPERUSER=1 to my Env file (as seen here), but this message still appears.
Is it possible to NOT execute composer install as root in a docker container? Can i ignore that message?
Thanks in advance,
Philipp
For the moment i am working towards the following solutuion:
Development environment:
Add the whole src as volume:
volumes:
- .:/app
initial composer install command
docker-compose exec app composer install
install new composer package:
docker-compose exec app composer require some/package
The package will be installed via the container in your composer package directory (/vendor by default of course), the source code can be inspected in your ide, etc.
Production environment:
Only app state defined as volume, for example:
volumes:
- public/uploads:/app/public/uploads
add /vendor to .dockerignore
and run composer install during build
RUN cd /app && composer install --no-dev --no-interaction --optimize-autoloader
So you will have a pre-built image to deploy, but still be able to develop in a more agile way.
There are still two downsides in this solution, maybe someone has input for me:
1) build becomes kind of slow - it seems composer caching does not work properly
A solution (i dont know if this is possible in composer) could be a volume for the composer cache directory.
2) all packages created by docker are owned by root, so you can only work on those files/folders with sudo/root session.
I have no idea how to fix this properly. Maybe i could create a user with the name of the dev user for the development container and have this user run commands and php-fpm, etc... For production root would be okay i guess.
I am still very new to docker and open for better solutions, so i wont accept this answer.
UPDATED
To add new user in docker user in Dockerfile. as follows
RUN useradd -ms /bin/bash newuser
USER newuser
if you want to create home user too
WORKDIR /home/newuser
or
mkdir /home/newuser
if you want to back to root user, you can do like
USER root
two-sides-of-a-coin -
It is a good practice to do on building images period which I also do the same as you.
we will get latest update from public repositories but another side of coin is that we cannot ensure they are the same version as we develop.
you need to handle this by specifying exact version in composer.json (do not use ^, *).
Related
I'm using Buddy Works to deploy my project to a server.
When creating a pipeline to deploy my Laravel project, I'm asked to enter deployment actions; this is where I'm stuck.
So far, I have set it to run the following composer commands:
composer validate
composer install
Because this is usually what I do when running my project locally. But I understand that I also need to run Apache and such (I use WAMP so it sorts all that for me).
I've searched on Google and all I got was composer install. Is this all I need to do or is there more?
Prior to using deployment tools, I used to upload files directly to the server, so CI/CD or other DevOps duties are very new to me.
It's good idea to reload php as well as migrate new tables :)
composer install --no-interaction --prefer-dist --optimize-autoloader
echo "" | sudo -S service php7.3-fpm reload
php artisan migrate --force
If you're using DigitalOcean and you have $12 to spare you could use ready tools such as Laravel Forge :)
I am currently learning about Docker, and using it for 2 weeks. Now i have a very simple task, installing PHP Libraries via Composer. That is usually, when working without Docker:
composer install
Now since i am using Docker, i found there is a Docker Container, that is holding composer for me:
docker run --rm -v $(pwd):/app composer/composer install
This is working pretty good, but there is some libraries out there, that require specific php libraries to be installed, like bcmath, so i add this to my Dockerfile
FROM php:7.0-apache
RUN docker-php-ext-install bcmath <-- added this line
COPY . /var/www/html
WORKDIR /var/www/html
EXPOSE 80
When i rebuild my container, this code returns true
var_dump(extension_loaded('bcmath'))
Hooray! BCMath is installed correctly, but composer does not recognize it, because the library is not installed in the composer container!
Now i could ignore that by using
docker run --rm -v $(pwd):/app composer/composer install --ignore-platform-reqs
but this is, in my opinion, a dirty workound, and composer can not validate my platform. Is there any clean solution, besides downloading composer in my Dockerfile and not reusing an existing container?
You may use platform settings to mimic your PHP container configuration. This will work similar to --ignore-platform-reqs switch (it will use PHP and extensions configured in composer.json instead of real info from current PHP installation), but it gives you more granular control. Instead of "ignore all platform requirements checks" you may say "I really have bcmath installed, trust me". All other requirements will be checked, so you still be warned if new requirement will pop-up.
"config": {
"platform": {
"php": "7.1",
"ext-bcmath": "*"
}
},
You need PHP + PHP Extensions + Composer in the same(!) container = DevContainer.
Simply install Composer using the command provided here.
I realize that this is what composer is used for, but I don't particularly like it. It makes sense, but it annoys me that laravel5's github doesn't work out of the box because it's vendor directory is somewhat large and isn't necessarily laravel5, although laravel does require it.
It also puzzles me why composer doesn't get it itself.
I would expect to be able to
git clone the_laravel5_github_url
composer install
and be able to run as it seemed like you were able to do at one time, but for some reason now, the vendor directory isn't there. I feel like this makes laravel harder to set up as it isn't so obvious.
Is there a place where someone can get an recent version of this? I find it disappointing that there is no mention of it on their readme and that you were able to do it before and still can't despite using composer install.
Install Composer on your OS using this command
curl -sS https://getcomposer.org/installer | php.
Move the composer.phar file to /usr/local/bin/ with this command
mv composer.phar /usr/local/bin/composer. This will enable you to access composer globally.
git clone your project.
Make a vendors folder in the root of your project.
cd to root of your project and run composer update. This command will look for vendors folder in the root and will install all the packages required by your project in it.
Happy Coding
Thank you :)
Use composer install in the same folder of your app. Try composer update. And install composer globally in your OS.
I am trying to run unit tests in a new laravel 5 application, using the phpunit framework. In the root path of my laravel application I ru the following command:
./vendor/bin/phpunit /tests/ExampleTest.php
And then I get the following message:
You need to set up the project dependencies using the following commands:
wget http://getcomposer.org/composer,phar
php composer.phar install
I already have composer installed in may system and I install Laravel 5 using composer. Isn't phpunit installed when I install a new laravel 5 application? If not, how can I install it in a existent laravel 5 application?
I known that I can also install phpunit globaly and resolve the problem. But maybe it will be a duplication since I have all the phpunit code already in may laravel application.
I had the same problem and it had a specific solution that may apply to other people. I store my files in Dropbox, and the vendor/bin/phpunit file should be an symlink like this
$ ls -lo vendor/bin/phpunit
lrwxr-xr-x vendor/bin/phpunit -> ../phpunit/phpunit/phpunit
However, Dropbox will occasionally replace symlinks with the original file, and this will cause the error above. The behaviour is inconsistent, and with relative symlinks seems to break when 2 machines are accessing Dropbox at the same time. Fixing the symlink worked for me or you could make a new symlink directly to the vendor/phpunit/phpunit/phpunit outside of Dropbox and run that.
Edit: Nowadays I exclude Vendor and node_modules from Dropbox - and simply run composer install when necessary. This works really well, and also deals with the hassle of syncing so many files on Dropbox. What you can do is go into the folder and delete all the files. Wait for Dropbox to sync. Then mark the folder as excluded. Finally, run composer install and you get the contents as you need. (Delete + composer install often solves other issues too).
Running composer install did nothing in my case. However, removing vendor folder and then calling composer install fixed it.
You need to have Composer installed and run composer install or composer update in your application to install the different packages listed in your composer.json.
When you install your Laravel application it doesn't install the packages right away.
You can verify the packages are installed by looking in the vendor directory of your application and check that phpunit is in there.
did you install phpunit globally? I recommend you do it.
just type in your laravel's root directory (e.g. /var/www)
cd /var/www
phpunit
if you what to test just one file, you can do something like this:
phpunit tests/ExampleTest.php
Unit Test:
D:\xampp\htdocs\Samplemed\vendor\bin>
phpunit ../../tests/Unit/Company/CompanyUnitTest
Feature Test:
D:\xampp\htdocs\Samplemed\vendor\bin>phpunit
../../tests/Feature/Company/CompanyFeatureTest
Please try this. its working fine.
I'm using Homestead to serve my Laravel application. I'm trying to run PHPUnit. According to the docs:
An example test file is provided in the app/tests directory. After
installing a new Laravel application, simply run phpunit on the
command line to run your tests.
Well, when I'm "simply running" phpunit in my project root (inside the Homestead environment) I get this:
The program 'phpunit' is currently not installed.
Do I need to install PHPUnit separately then? The documentation does not mention it. What am I doing wrong?
You can install it globally on the system using.
composer global require phpunit/phpunit
However, if you need different versions for different projects this can cause issues.
The alternative option is to use the version installed as part of your dependencies by referencing the path to your vendor directory.
./vendor/bin/phpunit
You could even add an alias to your aliases file in your ~/Homestead directory. That way you're always using the phpunit version that is installed with your project dependencies.
alias phpunit=./vendor/bin/phpunit
You'll need to restart the homestead box to make use of the alias.
You can install it globally with:
$ composer global require "phpunit/phpunit=4.4.*"
# then use
$ phpunit
or you can use it with your local composer:
$ composer require "phpunit/phpunit=4.4.*"
# then
$ vendor/bin/phpunit
Since it's a package required for development, Laravel provide PHPunit(require-dev section in composer), you should find it in vendor's folder :
$ your_app/vendor/bin/
You can run the command from the root of your app folder by typing :
$ vendor/bin/phpunit
I hope it will help !