Npm permission denied on docker php:8.1.1-fpm-alpine3.15 - php

I get the following error message when I try to spin up my service via docker-compose:
service_frontend | npm ERR! code 128
service_frontend | npm ERR! An unknown git error occurred
service_frontend | npm ERR! command git --no-replace-objects clone -b feature/WHITELABEL-212-skeletons-während-der-lad https://bjoernme:***#bitbucket.org/faaren/faaren-ui.git /root/.npm/_cacache/tmp/git-cloneBmjHnf --recurse-submodules --depth=1
service_frontend | npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-cloneBmjHnf': Permission denied
service_frontend |
service_frontend | npm ERR! A complete log of this run can be found in:
service_frontend | npm ERR! /root/.npm/_logs/2022-06-24T13_42_41_376Z-debug.log
service_frontend exited with code 128
I have tried multiple constellations with in my docker-compose.yml for the user property, starting from root, root:root, node:node, 1000:1000, UID:GID (variables are set to inject my local user id and group id.
The relevant part from my docker-compose.yml:
service_frontend:
build:
context: /workspace/faaren-services/frontend
dockerfile: Dockerfile
args:
dev: "true"
command: bash -c "npm install --save-dev chokidar#3.5.2 && composer install && php artisan octane:start --server=swoole --host=0.0.0.0 --port=8080 --watch"
user: root
volumes:
- /workspace/faaren-services/frontend:/var/www/html
- ./docker-conf/supervisor/supervisord.conf:/etc/supervisor/conf.d/supervisord.conf
- ./docker-conf/php/debugger.ini:/usr/local/etc/php/conf.d/debugger.ini
This is my local docker image:
FROM eu.gcr.io/faaren-prod/frontend-base-image:latest
COPY . /var/www/html
ARG dev=false
RUN if [ ${dev} = "true" ] ; then \
set -ex \
&& apk add --no-cache npm \
&& mkdir -p /.npm \
&& mkdir -p /root/.npm/_cacache/tmp/ \
&& chmod 777 -R "/root/.npm/_cacache/tmp/" \
&& chmod 777 -R "/.npm" \
fi ;
And this is our internal base image (which is based on the php:8.1.1-fpm-alpine3.15 image:
FROM php:8.1.1-fpm-alpine3.15
WORKDIR /var/www/html/
RUN apk add --no-cache --update git \
npm
RUN mkdir /.npm
RUN mkdir /.cache
RUN chown -R 1000:1001 "/.npm"
RUN chown -R 1000:1001 "/.cache"

After digging deeper into this issue I found out, that in Node 16.15.1 all commands within a npm task are run as the user who owns the current working dir. So even when running npm i as root subcommands as git clone are run as the user the current working dir belongs to.
In my case, /var/www/html/ belongs to user:group 33333:33333. npm i was run as root. Coming to some git clone commands, those were run as user 33333. Therefor the user 33333 was not allowed to access the default cache npm folder under /root/.npm as this folder belongs to user root.
I fixed the problem the following way:
create a cache dir outside of root mkdir /var/www/.npm/cache
change ownership to 333333: chown -R 33333:33333 /var/www/.npm/cache
set new directory as npm cache dir: npm config set cache /var/www/.npm/cache --global

Related

Php-fpm alpine can't resolve gitlab on my domain

I wrote myself a dockerfile that puts an application in Symfony. Unfortunately, the problem is that using the php:7.4.9-fpm-buster image everything works, but when php:7.4-fpm-alpine works it is a problem with the host service.
On alpine I get an error when installing composer:
#0 0.284 ssh: Could not resolve hostname gitlab.pawel.com: Name does not resolve
#0 0.284 fatal: Could not read from remote repository.
#0 0.284 Please make sure you have the correct access rights
#0 0.284 and the repository exists.
I already tried adding to /etc/hosts
193.33.111.185 gitlab.pawel.com
I also tried this /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4
I need a working image for alpine, but I have no idea what the problem is. This i my all code
FROM php:7.4-fpm-alpine
ARG ABSOLUTE_PATH=./app
ARG CONFIG_PATH=./config/web_adapter
RUN apk update && apk add openssh-client
#COPY CONFIG
RUN ln -s $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
COPY $CONFIG_PATH/conf.d/api-platform.ini $PHP_INI_DIR/conf.d/api-platform.ini
COPY $CONFIG_PATH/www.conf /usr/local/etc/php-fpm.d/www.conf
RUN adduser -D app
WORKDIR /srv
COPY $ABSOLUTE_PATH/composer.json $ABSOLUTE_PATH/composer.lock $ABSOLUTE_PATH/symfony.lock ./
RUN set -eux; \
composer update symfony/flex --no-plugins --no-scripts; \
composer install --prefer-dist --no-autoloader --no-scripts --no-progress --no-suggest; \
composer clear-cache
COPY $ABSOLUTE_PATH/bin bin/
COPY $ABSOLUTE_PATH/config config/
COPY $ABSOLUTE_PATH/public public/
COPY $ABSOLUTE_PATH/src src/
RUN mkdir -p var/cache var/log var/cache/prod
RUN chown -R app:app /srv
RUN set -eux; \
mkdir -p var/cache var/log; \
composer dump-autoload --classmap-authoritative --no-dev; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync
#ENTRYPOINT
COPY $CONFIG_PATH/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint
USER app
ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]
Of course IP and domain name is only as a example. Any idea what I did wrong?

shell_exec sh file including npm, npx

I have a .sh file that I execute from PHP with the shell_exec command. The .sh files are like below.
It creates the directories and "npm init -yes" line is executed successfully and I get the return lines but "npm install --save-dev hardhat" and "npx hardhat" lines have no return and don't do what expected to. Any idea how can I execute it and it works? When I execute the .sh file with root on the terminal it works also. But not with user apache.
cd /var/nftprojects
mkdir nft57
cd nft57
chmod -R 777 /var/nftprojects/nft57
mkdir ethereum
chmod -R 777 /var/nftprojects/nft57/ethereum
cd ethereum
npm init -yes
npm install --save-dev hardhat
npx hardhat --verbose
cd /var/nftprojects/nft57
mkdir /var/nftprojects/nft57/web
cd /var/nftprojects/nft57/web
npx create-next-app#latest
cd /var/nftprojects/nft57/ethereum
touch .env
npm install dotenv --save

laravel - Docker build failure in Production mode

I'm running a laravel 9 application (php v8) on docker. In my local setup docker is building without errors. When docker Up, I used cli and do the composer install and then artisan commands. I just do docker-compose up for that
After finishing initial developments, before pushing to Dockerhub, I checked by doing a build that is related to production. Here is my Dockerfile.
RUN apk add --no-cache php8 php8-cli php8-curl php8-intl php8-pdo_mysql php8-session php8-tokenizer php8-phar php8-xmlwriter php8-mbstring php8-simplexml php8-iconv php8-fileinfo php8-dom php8-xml php8-zip php8-openssl php8-pdo_sqlite php8-gd
RUN apk add --no-cache git
RUN apk add --no-cache composer
RUN apk add --no-cache npm
RUN cp /usr/bin/php8 /usr/bin/php
ARG BUILD_ENV
WORKDIR /
COPY conf/nginx.conf /etc/nginx/nginx.conf
COPY conf/php.ini /etc/php8/php.ini
COPY ./project/laravel /www
RUN if [[ "$BUILD_ENV" == "PRD" ]]; then \
cd /www && \
composer install; \
npm install; \
npm run dev; \
fi
RUN if [[ "$BUILD_ENV" == "PRD" ]]; then \
chown -R php:php /www; \
fi
Then I run the command
docker build -t my-laravel-web --build-arg BUILD_ENV=PRD .
But it shows this error
Any help to fix this issue before updating my dockerhub. I think the same problem will occur when I try to use the docker image to deploy in AWS.

How to map composer vendor folder to docker volume ? (case of Symfony bundle linked to my projet)

I am stuck with that problem.
I have a docker compose file with volumes mapping that way :
volumes:
- ./:/var/www/html:rw
- ../epossobundle/:/var/www/epossobundle :rw;
In composer.json, there is a repo linked on a bundle which I am working on it like this :
"repositories": [
{
"type": "path",
"url": "../epossobundle"
},
But when I start my container I got the error
Warning: include(/var/www/html/vendor/composer/../epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php): failed to open stream: No such file or directory
How can I do ??
PS : It is not possible to install something in Docker image because it is intranet network
Thanks a lot
Serge
I assume that the vendor file is missing dependencies, specifically epo/api-auth-sso-bundle/EpoApiAuthSsoBundle.php so you'll need to install them.
Grab a root shell inside the container:
docker exec -it -u root <container_id_or_name> /bin/bash
Install composer (requires root):
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Then install your dependencies (if not in production, no need for --no-dev):
composer install --no-dev
Finally, change the permissions back (ls -rvla):
# Nginx
chown -R nginx:nginx .
# Apache2
chown -R www-data:www-data .
Note: This is a "hacky-fix", if you rebuild the container you will have to do this every time. You should look to do this inside your Dockerfile as a permanent solution.
Untested example of the Dockerfile:
FROM php:7.4-fpm
WORKDIR /var/www/html
# Nginx
RUN groupadd -g 101 nginx && \
useradd -u 101 -ms /bin/bash -g nginx nginx
# Apache2
RUN groupadd -g 101 www-data && \
useradd -u 101 -ms /bin/bash -g www-data www-data
# Download and install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Nginx
USER nginx
# Apache2
USER www-data
# Or add this as an entry-point (runs as user for permissions)
RUN composer install --no-dev

Cannot expose port from container to host, using PHP static webserver

I have this multi-stage build in a Dockerfile:
## Stage 1
FROM node:9 as builder
RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force
COPY dist .
## Stage 2
FROM php:5.6.30
RUN apt-get update && apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
COPY --from=builder /home/newuser/app /home/newuser/app
RUN ls -a /home/newuser/app
CMD ["php", "-S", "localhost:3000"]
the image build successfully, using:
docker build -t x .
Then I run it with:
docker run -p 3000:3000 x
but when I go to localhost:3000 on the host machine, I don't get a response. The webpage is blank.
Does anyone know why that might happen?
I also tried:
CMD ["sudo", "php", "-S", "localhost:80"]
and
docker run -p 3000:80 x
and a few other variations, still nothing.
Not sure why the PHP static server wasn't working, but I got it working with Node.js static server package:
FROM node:9 as builder
RUN apt-get update && \
apt-get -y install sudo
RUN echo "newuser ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN useradd -ms /bin/bash newuser
USER newuser
RUN mkdir -p /home/newuser/app
WORKDIR /home/newuser/app
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib
RUN sudo chown -R $(whoami) $(npm config get prefix)/lib/node_modules
RUN sudo chown -R $(whoami) $(npm config get prefix)/bin
RUN sudo chown -R $(whoami) $(npm config get prefix)/share
RUN sudo chown -R $(whoami) /usr/local/lib
RUN npm set progress=false
RUN npm config set depth 0
RUN npm cache clean --force
COPY dist .
RUN npm install -g http-server
ENTRYPOINT ["http-server", "."]
you build it like so:
docker build -t x .
and then run it like so:
docker run -p 3000:8080 x

Categories