S
S
spaceatmoon2021-11-18 12:51:19
Docker
spaceatmoon, 2021-11-18 12:51:19

Why does docker-compose ignore the user directive?

This config works fine under vagrant, but there is some problem with installing modules via npm. I decided to double-check on normal hosting and on normal hosting npm is installed as it should. However, docker-composer ignores the user directive in docker-compose.yml and simply passes the permissions as is into the container without changing them. Composer and docker versions are the same with virtual and real vps.

Each time doing chown on a host under a fictitious user is somehow strange. This looks like a bug.

Dockerfile

FROM php:8.0.12-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libcurl4-openssl-dev \
    libpng-dev \
    libpq-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install curl zip pdo pgsql pdo_pgsql mbstring exif pcntl bcmath gd intl opcache xml

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# nvm environment variables
ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 14.16.0
ENV NPM_VERSION 7.6.3

SHELL ["/bin/bash", "--login", "-c"]
RUN mkdir -p $NVM_DIR

# Install nvm with node and npm
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default \
    && npm install [email protected]$NPM_VERSION -g

ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
#ENV PATH      $NVM_DIR:$PATH
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

WORKDIR /var/www

USER $user



docker-compose.yml

version: "3.7"
services:
    app:
        build:
            args:
                user: momo
                uid: 1000
            context: ./dockerfiles
            dockerfile: Dockerfile
        image: appmail
        container_name: appmail-app
        restart: unless-stopped
        working_dir: /var/www/
        volumes:
            - ./:/var/www
        networks:
            - appmail

    db:
        container_name: appmail-db-pgsql
        image: postgres:14
        env_file: ./.env
        environment:
            POSTGRES_DB: ${DB_DATABASE}
            POSTGRES_USER: ${DB_USERNAME}
            POSTGRES_PASSWORD: ${DB_PASSWORD}
            PGDATA: /data/postgres14
        volumes:
            - appmail-db-pgsql14:/data/postgres14
        ports:
            - ${DB_PORT}:5432
        networks:
            - appmail
        restart: unless-stopped

    admin:
        image: adminer
        restart: always
        depends_on:
            - db
        ports:
            - 8082:8080
        networks:
            - appmail

    nginx:
        image: nginx:alpine
        container_name: appmail-nginx
        restart: unless-stopped
        ports:
            - ${APPLICATION_PORT}:80
            #- 433:433
        volumes:
            - ./:/var/www
            - ./docker-compose/nginx:/etc/nginx/conf.d/
        networks:
            - appmail

networks:
    appmail:
        driver: bridge

volumes:
    appmail-db-pgsql14:



-rwxrwxr-x 1 1002 1002   4051 Nov 18 07:20 README.md
Должно быть так:
-rwxrwxr-x 1 momo momo   4051 Nov 18 07:20 README.md

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mureevms, 2021-11-18
@mureevms

How exactly do you check?
What is the UID of the momo user in the container? Wang, that the first output without a user is in your host system, since there is no user with this UID in it. And the UID of the momo user in the container == 1002

Each time doing chown on a host under a fictitious user is somehow strange. This looks like a bug.

All usernames are made for the convenience of a person, the system operates with a UID. Chown also knows how to specify a UID, not a username: chown -R 1002:1002 /target

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question