Philipp Wrann
Philipp Wrann

Reputation: 1849

docker and php: getting dependencies (composer) into the container

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

Upvotes: 7

Views: 5474

Answers (2)

Philipp Wrann
Philipp Wrann

Reputation: 1849

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

Upvotes: 5

Aggarat .J
Aggarat .J

Reputation: 509

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 ^, *).

Upvotes: -1

Related Questions