title: Dockerfile entrypoint unable to switch user
body: <p>I am unable to switch user to a non-root user from the entry point script. The User directive to change the user in Dockerfile works, but I am not able to change permissions using chmod. To overcome this issue I created <code>entrypoint.sh</code> script to change the folder permissions but when I try to switch user using <code>su</code> command, it apparently doesn't work, the container is still running as root.</p>
  
  <p>The Dockerfile</p>
  
  <pre><code>FROM php:7.2-fpm
  
  # Installing dependencies
  RUN apt-get update &amp;&amp; apt-get install -y \
      build-essential \
      mysql-client \
      libpng-dev \
      libjpeg62-turbo-dev \
      libfreetype6-dev \
      locales \
      zip \
      jpegoptim optipng pngquant gifsicle \
      vim \
      unzip \
      git \
      curl
  
  # Installing composer
  RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  
  ENV USER_ID=1000
  ENV GROUP_ID=1000
  ENV USER_NAME=www
  ENV GROUP_NAME=www
  
  RUN groupadd -g $GROUP_ID $GROUP_NAME
  RUN useradd -u $USER_ID -ms /bin/bash -g $GROUP_NAME $USER_NAME
  RUN mkdir /app
  WORKDIR /app
  
  EXPOSE 9000
  
  COPY ./entrypoint.sh /
  RUN ["chmod", "+x", "/entrypoint.sh"]
  ENTRYPOINT ["/entrypoint.sh"]
  </code></pre>
  
  <p>Entrypoint.sh file</p>
  
  <pre><code>#!/bin/bash
  if [ -n "$USER_ID" -a -n "$GROUP_ID" ]; then
      chown -R $USER_NAME:$GROUP_NAME .
      su $USER_NAME
  fi
  
  php-fpm
  
  exec "$@"
  </code></pre>
  
  <p>whatever I do I am not able to switch user from the entrypoint.sh script.</p>
  
  <p>My case is to run the container as non-root user.</p>