Docke run volume root file ownership issue - php

I'm having issues getting a mounted docker volume to use a user other than root.
Any files I create with this command:
docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.1-cli php "$#"
will always have root ownership on my host directory. Can someone clue me into how I get it to mount with my host's dir user? (Ex: /home/meisuser/mountedvolume
I've tried:
docker run -it --rm --name my-running-script --user $(id -u):$(id -g) -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.1-cli php "$#"
but it doesn't seem to work.

Related

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

Nginx process and php-fpm share volumes

I have problems with a share volumes that both nginx apline and php-fpm can write and delete folder and file.
Nginx container will create static file on var/run.
FROM nginx:alpine
RUN set -x ; \
addgroup -g 82 -S www-data ; \
adduser -u 82 -D -S -G www-data www-data && exit 0 ; exit 1
RUN chmod 777 -R /var/run
RUN mkdir -p /var/run/static-cache/
I also create a same directory on php-fpm container so it can clear all the cache when I update a page.
FROM php:7.4-fpm-alpine
RUN apk add shadow && usermod -u 1000 www-data && groupmod -g 1000 www-data
RUN chmod 777 -R /var/run
RUN mkdir -p /var/run/static-cache.
WORKDIR /var/www/html/
A mount volume will link these two directories between 2 containers.
However, when I save a page, I'm not able to clear any folder on php-fpm container due to permision denied.
Is ther any solutions to solve arround this?

php and apache together in docker

first of all i pulled apache image:
docker pull httpd
then i ran my apache on 8080 port
docker run -dit --name httpd -p 8080:80 -v "$PWD":/usr/local/apache2/htdocs/ httpd
i opened 127.0.0.1:8080 and it is ok
i have a file named info.php and it exists in PWD and when i open it in my browser i see php code and it had not been compiled
how can it use php and apache together in docker and how i am able to run php in my browser
im new comer to docker
you can use this image php:<version>-apache
php:-apache
This image contains Debian's Apache httpd in conjunction with PHP (as
mod_php) and uses mpm_prefork by default.
docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache
Or you can copy PHP files at build times.
Tree
├── Dockerfile
└── src
└── info.php
Dockerfile
FROM php:7.2-apache
COPY src/ /var/www/html/
Run container
docker run -d -p 80:80 --name my-apache-php-app my_image

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

unable to run Apache/php from docker container

My requirement is using openface i need to train the dataset(images) and test each input image from webinterface (PHP) and all this activity should run from docker container.
I am able to achieve the above requirment on ubuntu machine. we are trying to install the complete setup(apache/php & openface) in docker. currently we are unable to invoke the html files from apache server using docker
The following the docker file used to import the project into docker and install apache/PHP. Please let me know if any changes need to be done in the dockerfile.
FROM ubuntu:16.04
RUN apt-get update && \
apt-get -y install sudo
RUN useradd -m docker && echo "docker:docker" | chpasswd && adduser docker sudo
ADD opencv-3.0.0 /
ADD openface_setup.sh /
RUN /openface_setup.sh
ADD openface_work /
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
apache2 php7.0 libapache2-mod-php7.0 curl lynx-cur
RUN a2enmod php7.0
RUN a2enmod rewrite
RUN sed -i "s/short_open_tag = Off/short_open_tag = On/" /etc/php/7.0/apache2/php.ini
RUN sed -i "s/error_reporting = .*$/error_reporting = E_ERROR | E_WARNING | E_PARSE/" /etc/php/7.0/apache2/php.ini
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
EXPOSE 8080
VOLUME /var/www/html # **my PHP/html files are located here. In the docker container the html/php files are not reflecting**
ADD apache-config.conf /etc/apache2/sites-enabled/000-default.conf
CMD /usr/sbin/apache2ctl -D FOREGROUND
Once the container is started i want the test.html(located in /var/www/html) to be running.
FYI :
command to created docker image
sudo docker build -t myname/apache-test .
command to start the docker container
docker run -p 8080:80 -d <imageid>
I'd suggest to use the official PHP image with a pre-installed Apache installation.
Your project might look like this:
.
├── Dockerfile
└── src
└── index.php
while your Dockerfile consists of this:
FROM php:7.1-apache
# now RUN here your commands to install openface etc.
and your index.php could look like this:
<?php phpinfo();
Then build the image:
docker build -t myapache .
docker run --rm -p 8080:80 -v $(pwd)/src:/var/www/html myapache
http://localhost:8080 shows the php-info page.
You can extend the image to your needs and it's much simpler than your approach. Hope this might help.
If you do not need to install anything else, you can directly use the php:7.1-apache image when creating a new container.
try typing docker ps to get all the processes running in containers.
Then just type docker run -it container-id
It will start the apache server and show the address where it hosted it unless you want to add a different one in /etc/docker/daemon.json (https://docs.docker.com/engine/userguide/networking/default_network/custom-docker0/)

Categories