2

I want to add user www-data as my default user when I run my container in bash mode.

Currently, when I type WHOAMI then it's showing root as user, but I need www-data as user, how to do this in docker.

enter image description here

This is my docker file:

    FROM php:7.2-fpm-alpine

LABEL maintainer="y.ghorecha@abc.de" \
      muz.customer="xxx" \
      muz.product="WIDC" \
      container.mode="production"

#https://pkgs.alpinelinux.org/packages
RUN apk add --no-cache --virtual .deps autoconf tzdata build-base libzip-dev mysql-dev gmp-dev \
            libxml2-dev libpng-dev zlib-dev freetype-dev jpeg-dev icu-dev openldap-dev libxslt-dev &&\
    docker-php-ext-install zip xml mbstring json intl gd pdo pdo_mysql iconv soap \
                           dom gmp fileinfo sockets bcmath mysqli ldap xsl &&\
    echo 'date.timezone="Europe/Berlin"' >> "${PHP_INI_DIR}"/php.ini &&\
    cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime &&\
    echo 'Europe/Berlin' > /etc/timezone &&\
    apk del .deps &&\
    apk add --no-cache libzip mysql libxml2 libpng zlib freetype jpeg icu gmp git subversion libxslt openldap \
            apache2 apache2-ldap apache2-proxy libreoffice openjdk11-jre ghostscript msttcorefonts-installer \
            terminus-font ghostscript-fonts &&\
    ln -s /usr/lib/apache2 /usr/lib/apache2/modules &&\
    ln -s /usr/sbin/httpd /etc/init.d/httpd &&\
    update-ms-fonts

# imap setup
RUN apk --update --virtual build-deps add imap-dev
RUN apk add imap
RUN docker-php-ext-install imap

# copy all codebase
COPY ./ /var/www

# SSH setup
RUN apk update && \
    apk add --no-cache \
    openssh-keygen \
    openssh

# copy Azure specific files
COPY backend/build/azure/backend/ /var/www/backend/

# User owner setup
RUN chown -R www-data:www-data /var/www/

# Work directory setup
WORKDIR /var/www

# copy apache httpd.conf file
COPY httpd.conf /etc/apache2/httpd.conf

# copy crontabs for root user
COPY backend/data/CRONTAB/production/crontab.txt /etc/crontabs/www-data

# SSH Key setup
RUN mkdir -p /home/www-data/.ssh
RUN chown -R www-data:www-data /home/www-data/

#https://github.com/docker-library/httpd/blob/3ebff8dadf1e38dbe694ea0b8f379f6b8bcd993e/2.4/alpine/httpd-foreground
#https://github.com/docker-library/php/blob/master/7.2/alpine3.10/fpm/Dockerfile

CMD ["/bin/sh", "-c", "rm -f /usr/local/apache2/logs/httpd.pid && /usr/sbin/crond start && httpd -DBACKGROUND && php-fpm"]

Please advice on above.

1
  • What have you already tried? I'd advise to not try running an ssh daemon inside a Docker container at all; the security issues around it are tricky and you shouldn't routinely need a shell in the container anyways.
    – David Maze
    Dec 9, 2019 at 11:10

2 Answers 2

4

I'd start with first checking if the www-data user even exits in the image. Execute in the running container something like:

sudo cat /etc/passwd | grep www-data

If the user does exist then add the USER www-data directive to the Dockerfile after all commands that do installs, create directories, change permissions, etc. It would be required to also add USER 0 at the beginning to switch to the root user for those commands if the base image doesn't run as root. Looking at the Dockerfile I'd suggest to add USER www-data before the CMD directive.

If the www-data user doesn't exist then it has to be added first. The commands for Alpine Linux are addgroup and adduser. Something like these if the user id for www-data is to be 33 and the group it belongs to is also named www-data and has id of 33:

RUN addgroup -S -g 33 www-data \
 && adduser -S -D -u 33 -s /sbin/nologin -h /var/www -G www-data www-data

Add the above just before RUN chown -R www-data:www-data /var/www/, or make it a single RUN directive:

RUN addgroup -S -g 33 www-data \
 && adduser -S -D -u 33 -s /sbin/nologin -h /var/www -G www-data www-data \
 && chown -R www-data:www-data /var/www/
4
  • 1
    Good work on explaining "how that supposed to work" and not only "how to quick-fix it"
    – Nick
    Dec 9, 2019 at 14:03
  • @gears I'm using 7.2-fpm-alpine which is already have www-data user, check this github.com/docker-library/php/blob/… Dec 9, 2019 at 14:10
  • Error: Step 13/20 : RUN addgroup -S -g 33 www-data && adduser -S -D -u 33 -s /sbin/nologin -h /var/www -G www-data www-data && chown -R www-data:www-data /var/www/ ---> Running in 5b3a56e2ace8 addgroup: group 'www-data' in use The command '/bin/sh -c addgroup -S -g 33 www-data && adduser -S -D -u 33 -s /sbin/nologin -h /var/www -G www-data www-data && chown -R www-data:www-data /var/www/' returned a non-zero code: 1 Dec 9, 2019 at 16:03
  • 1
    @YogiGhorecha yes, "addgroup: group 'www-data' in use" error is to be expected if the www-data group exists already. My answer is split into two parts depending on whether the www-data user exists or not.
    – gears
    Dec 9, 2019 at 16:12
3

You can add this line to you dockerfile in order to become the www-data user

USER www-data

This can be either added

  • in the end of your file (if you want at to become that user before the script exits), or
  • in the beginning if you want to perform your actions within the docker file as this user.
3
  • where to write www-data?? Dec 9, 2019 at 9:23
  • 1
    in the end of your file if you want at the end of operation to become that user, or before you want to perform a certain action, check my updated answer Dec 9, 2019 at 10:07
  • I am really glad I could help you @YogiGhorecha Dec 10, 2019 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.