Docker php:5.6-Apache Development Environment miss

2019-07-31 23:45发布

I am attempting to create a development environment composed of a php:5.6-apache image, a mysql image, and a volume mount containing the website that will be located in /var/www/html/. The volume mount has has October CMS on it.

I need the docker user to have permissions over the volume mount, but I do not want it to be the owner of that volume, so i can easily modify it outside of the container, and see my modifications immediately.

My docker file is as follows:

FROM php:5.6-apache
MAINTAINER me <me@internet>

# Install modules
RUN apt-get update
RUN apt-get -f install -y
RUN apt-get install -y wget
RUN apt-get update && apt-get install -y \
    libmcrypt-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libmcrypt-dev \
    libpng12-dev \
    php-apc
RUN docker-php-ext-install mbstring mysql mcrypt pdo pdo_mysql zip
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

RUN docker-php-ext-install gd

WORKDIR /var/www/html/

VOLUME /var/www/html/

RUN a2enmod rewrite

RUN usermod -a -G www-data root

RUN php5enmod mcrypt && service apache2 restart

My Docker-compose contains environment variables for the MySql database.

When I run it as is, I just get 500 error, but if I change the ownership of the volume to www-data, the site comes up just fine.

I have tried using environment variables to change the Apache user and group to root, but it did not work.

Any suggestions are greatly appreciated.

UPDATE: I was unable to find a solution that fixed the problem in the exact way I am looking for, and in the end implemented something I view as more of a hack. I added the following lines to the dockerfile:

RUN usermod -u 1000 www-data
RUN groupmod -g 1000 www-data

These lines change the user and group inside the docker to match the owner, thus giving it complete access.

Be aware though that there are some cases your system will not recognize you as user 1000 and group 1000. If this does not work, bash into the docker apache container, and check what the volume has as the user id and group id, and then change the lines above accordingly.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-01 00:17

After you run docker-machine start default you need to ssh into it and run the following for the above to fully work:

sudo mkdir --parents /var/www/html [assuming that /var/www/html is the shared folder in your virtualbox]

sudo mount -t vboxsf -o uid=1000,gid=1000 var/www/html /var/www/html [this is to make sure the uid and gid is 1000 for the next part to work]

查看更多
唯我独甜
3楼-- · 2019-08-01 00:37

All the files that you share in a mounted volume need to have the same uid:guid ownership of the user that runs the CMD in your container. I managed to use some workarounds in my daily work, hopefully one of those could work for you:

  • COPY the resource files in another location and in the entrypoint cp them to the right path, since the user that execute the entrypoint script is the same one that runs the CMD your files will have the same uid:guid. You can see this pattern in the public jenkins docker image
  • Mimic the user and group ownership schema for the needed files in your docker host, in that way both container and host has the same UID/GUID for the files that you want to use therefor the web server won't kill itself when it tries to read those files.
  • Change ownership on the fly with an entrypoint script like postgresql public docker image does.

Hope it helps!

查看更多
登录 后发表回答