Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run composer as non root user on official docker php image

I am using the official php:alpine https://github.com/docker-library/php/blob/master/7.2/alpine3.7/fpm/Dockerfile as my base image. My projects are basically composer based project. So I installed composer on it like below.

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- --filename=composer

When I install any packages using composer install it runs as root as the main php process runs as root. So how can I run main php process as root and composer as another non root user deploy?

Update-1 My dockerfile is like below. So as you can see I am not installing composer packages inside dockerfile rather I install those packages on container like docker exec -it php composer install.

FROM php:7.2-fpm-alpine
..........................
..........................
..........................
RUN  set ex \
  # Install Composer( Requires git )
  && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin -- --filename=composer \
  ENTRYPOINT ["/entrypoint.sh"]
  CMD ["php-fpm"]

I was trying to achieve it like https://unix.stackexchange.com/questions/476155/how-to-pass-multiple-parameters-to-su-user-c-command but in vain.

like image 603
SkyRar Avatar asked Oct 18 '18 03:10

SkyRar


People also ask

Should I run composer as Sudo?

Best practice is not to use sudo for composer commands at all. If you need sudo for composer, it usually points at your project's file permissions not being setup correctly. E.g. you should have a non-root user owning the projects directory, and you should run the needed commands as that user, without requiring sudo .

What is Docker composer?

Composer is a tool for dependency management in PHP, written in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.


1 Answers

Incase anyone wants to achieve the same follow instructions below

I added a USER in dockerfile so that composer will be run as user deploy in future. But php-fpm needs to be run as root. So I set a setuid bit on the php-fpm binary. And now everything got resolved.

RUN chmod u+s /usr/sbin/php-fpm7

USER deploy
ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm7","-F"]
like image 195
SkyRar Avatar answered Oct 07 '22 09:10

SkyRar