Home > OS >  NPM not working on Docker image php:8.1-fpm-alpine
NPM not working on Docker image php:8.1-fpm-alpine

Time:10-10

I have the following Dockerfile:

FROM php:8.1-fpm-alpine
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN apk add --no-cache zip mariadb-client npm && \
    docker-php-ext-install mysqli pdo pdo_mysql

It seems to install correctly but NPM commands do nothing:

/usr/bin $ which node
/usr/bin/node
/usr/bin $ node -v
v18.9.1
/usr/bin $ which npm
/usr/bin/npm
/usr/bin $ npm -v

/usr/bin $

Nothing shows in the logs to explain it:

[10-Oct-2022 12:38:52] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[10-Oct-2022 12:38:52] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[10-Oct-2022 12:38:52] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[10-Oct-2022 12:38:52] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[10-Oct-2022 12:38:52] NOTICE: fpm is running, pid 1
[10-Oct-2022 12:38:52] NOTICE: ready to handle connections

The issue appears to happen when I set the GID/UID before running the compose command:

export uid=$(id -u)
export gid=$(id -g)
docker compose -f docker/docker-compose.yml --env-file .env up

In my docker-compose.yml I have the following setting:

services:
  php:
    build: .
    container_name: ${COMPOSE_PROJECT_NAME}-php
    user: ${uid:-www-data}:${gid:-www-data}
    volumes:
      - ../:/var/www/html

CodePudding user response:

You are asking the container to use a user that it doesn't recognize. You should first create the user:

FROM php:8.1-fpm-alpine

ARG UID
ARG GID

RUN addgroup -S ${GID} && adduser -S ${UID} -G ${GID}

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN apk add --no-cache zip mariadb-client npm && \
    docker-php-ext-install mysqli pdo pdo_mysql

docker-compose.yml:

services:
  php:
    build: 
      context: .
      args:
      - UID=${uid:-www-data}
      - GID=${gid:-www-data}
    container_name: ${COMPOSE_PROJECT_NAME}-php
    user: ${uid:-www-data}:${gid:-www-data}
    volumes:
      - ../:/var/www/html

Then you can run:

export uid=$(id -un)
export gid=$(id -gn)
docker compose -f docker/docker-compose.yml --env-file .env up
  • Related