Skip to content

Commit 054c5e2

Browse files
authored
Merge pull request #1215 from infosiftr/php-arbitrary-user
Add a new section in the PHP documentation about running as an arbitrary user
2 parents 2cbef97 + 27d9b18 commit 054c5e2

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

drupal/content.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@ The following Docker Hub features can help with the task of keeping your depende
9696

9797
- [Automated Builds](https://docs.docker.com/docker-hub/builds/) let Docker Hub automatically build your Dockerfile each time you push changes to it.
9898
- [Repository Links](https://docs.docker.com/docker-hub/builds/#repository-links) can ensure that your image is also rebuilt any time `%%REPO%%` is updated.
99+
100+
## Running as an arbitrary user
101+
102+
See [the "Running as an arbitrary user" section of the `php` image documentation](https://hub.docker.com/_/php/).

owncloud/content.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ $ docker exec -u www-data some-owncloud php occ status
4747
## %%STACK%%
4848

4949
Run `docker stack deploy -c stack.yml %%REPO%%` (or `docker-compose -f stack.yml up`), wait for it to initialize completely, and visit `http://swarm-ip:8080/`, `http://localhost:8080/`, or `http://host-ip:8080` (as appropriate).
50+
51+
## Running as an arbitrary user
52+
53+
See [the "Running as an arbitrary user" section of the `php` image documentation](https://hub.docker.com/_/php/).

php/content.md

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,20 @@ If you don't want to include a `Dockerfile` in your project, it is sufficient to
7474
$ docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html %%IMAGE%%:7.0-apache
7575
```
7676

77-
### How to install more PHP extensions
77+
### Changing `DocumentRoot`
78+
79+
Some applications may wish to change the default `DocumentRoot` in Apache (away from `/var/www/html`). The following demonstrates one way to do so using an environment variable (which can then be modified at container runtime as well):
80+
81+
```dockerfile
82+
FROM %%IMAGE%%:7.1-apache
83+
84+
ENV APACHE_DOCUMENT_ROOT /path/to/new/root
85+
86+
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
87+
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
88+
```
89+
90+
## How to install more PHP extensions
7891

7992
We provide the helper scripts `docker-php-ext-configure`, `docker-php-ext-install`, and `docker-php-ext-enable` to more easily install PHP extensions.
8093

@@ -87,7 +100,7 @@ RUN docker-php-source extract \
87100
&& docker-php-source delete
88101
```
89102

90-
#### PHP Core Extensions
103+
### PHP Core Extensions
91104

92105
For example, if you want to have a PHP-FPM image with `iconv`, `mcrypt` and `gd` extensions, you can inherit the base image that you like, and write your own `Dockerfile` like this:
93106

@@ -107,7 +120,7 @@ Remember, you must install dependencies for your extensions manually. If an exte
107120

108121
See ["Dockerizing Compiled Software"](https://tianon.xyz/post/2017/12/26/dockerize-compiled-software.html) for a description of the technique Tianon uses for determining the necessary build-time dependencies for any bit of software (which applies directly to compiling PHP extensions).
109122

110-
#### PECL extensions
123+
### PECL extensions
111124

112125
Some extensions are not provided with the PHP source, but are instead available through [PECL](https://pecl.php.net/). To install a PECL extension, use `pecl install` to download and compile it, then use `docker-php-ext-enable` to enable it:
113126

@@ -131,7 +144,7 @@ For example, `memcached-2.2.0` has no PHP version constraints (https://pecl.php.
131144

132145
Beyond the compatibility issue, it's also a good practice to ensure you know when your dependencies receive updates and can control those updates directly.
133146

134-
#### Other extensions
147+
### Other extensions
135148

136149
Some extensions are not provided via either Core or PECL; these can be installed too, although the process is less automated:
137150

@@ -165,7 +178,17 @@ RUN curl -fsSL 'https://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.
165178
&& rm -r /tmp/xcache
166179
```
167180

168-
#### "`E: Package 'php-XXX' has no installation candidate`"
181+
## Running as an arbitrary user
182+
183+
For running the FPM variants as an arbitrary user, the `--user` flag to `docker run` should be used (which can accept both a username/group in the container's `/etc/passwd` file like `--user daemon` or a specific UID/GID like `--user 1000:1000`).
184+
185+
For running the Apache variants as an arbitrary user, there are several choices:
186+
187+
- If your kernel [is version 4.11 or newer](https://github.com/moby/moby/issues/8460#issuecomment-312459310), you can add `--sysctl net.ipv4.ip_unprivileged_port_start=0` and then `--user` should work as it does for FPM.
188+
- If you adjust the Apache configuration to use an "unprivileged" port (greater than 1024 by default), then `--user` should work as it does for FPM regardless of kernel version.
189+
- Otherwise, setting `APACHE_RUN_USER` and/or `APACHE_RUN_GROUP` should have the desired effect (for example, `-e APACHE_RUN_USER=daemon` or `-e APACHE_RUN_USER=#1000` -- see [the Apache `User` directive documentation for details on the expected syntax](https://httpd.apache.org/docs/2.4/mod/mod_unixd.html#user)).
190+
191+
## "`E: Package 'php-XXX' has no installation candidate`"
169192

170193
As of [docker-library/php#542](https://github.com/docker-library/php/pull/542), this image blocks the installation of Debian's PHP packages. There is some additional discussion of this change in [docker-library/php#551 (comment)](https://github.com/docker-library/php/issues/551#issuecomment-354849074), but the gist is that installing Debian's PHP packages in this image leads to two conflicting installations of PHP in a single image, which is almost certainly not the intended outcome.
171194

@@ -176,16 +199,3 @@ RUN rm /etc/apt/preferences.d/no-debian-php
176199
```
177200

178201
The *proper* solution to this error is to either use `FROM debian:XXX` and install Debian's PHP packages directly, or to use `docker-php-ext-install`, `pecl`, and/or `phpize` to install the necessary additional extensions and utilities.
179-
180-
### Changing `DocumentRoot`
181-
182-
Some applications may wish to change the default `DocumentRoot` in Apache (away from `/var/www/html`). The following demonstrates one way to do so using an environment variable (which can then be modified at container runtime as well):
183-
184-
```dockerfile
185-
FROM %%IMAGE%%:7.1-apache
186-
187-
ENV APACHE_DOCUMENT_ROOT /path/to/new/root
188-
189-
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
190-
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
191-
```

wordpress/content.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ The following Docker Hub features can help with the task of keeping your depende
6262

6363
- [Automated Builds](https://docs.docker.com/docker-hub/builds/) let Docker Hub automatically build your Dockerfile each time you push changes to it.
6464
- [Repository Links](https://docs.docker.com/docker-hub/builds/#repository-links) can ensure that your image is also rebuilt any time `%%REPO%%` is updated.
65+
66+
## Running as an arbitrary user
67+
68+
See [the "Running as an arbitrary user" section of the `php` image documentation](https://hub.docker.com/_/php/).

0 commit comments

Comments
 (0)