Skip to content

Commit 25df7b6

Browse files
committed
Remove separate build user and run as root.
Instead of bringing in the difficulty of a build user that needs to be setup on the host, instead user namespaces can be used to provide a similar level of security for the host system. This should make this image easier to work with, especially for custom additions and running on non-Linux development machines. Group setup is still necessary for volume mounting permissions to work under many setups, but it's configurable by the user instead of hard-coded by this docker image. This should also hopefully improve the experience when trying to run this image on an OSX or Windows host, especially using the newly in beta project released by Docker: https://blog.docker.com/2016/03/docker-for-mac-windows-beta/.
1 parent febbff6 commit 25df7b6

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

Dockerfile

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,12 @@ RUN pacman-key --refresh-keys && \
1717
# warnings.
1818
COPY timezone.ini /etc/php/conf.d/
1919

20-
# Create a separate user for composer to run as. Root access shouldn't
21-
# typically be necessary. We specify the uid so that it is unique.
22-
RUN useradd --uid 55446 --create-home --comment "Composer Build User" build
23-
24-
RUN mkdir /code && chown build:build /code
20+
RUN mkdir /code
2521
WORKDIR /code
2622

27-
USER build
28-
ENV HOME /home/build
23+
ENV HOME /root
2924
ENV COMPOSER_HOME $HOME/.composer
3025

31-
# Set the umask to 002 so that the group has write access inside and outside the
32-
# container.
33-
COPY umask.sh $HOME/
34-
3526
# Setup and install composer into the composer global location. The
3627
# certificate is installed manually to get around open_basedir restrictions.
3728
RUN mkdir -p $COMPOSER_HOME/vendor/bin
@@ -42,5 +33,4 @@ RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=$COMPOSER
4233
# system PATH.
4334
ENV PATH vendor/bin:$COMPOSER_HOME/vendor/bin:$PATH
4435

45-
ENTRYPOINT ["/home/build/umask.sh"]
4636
CMD ["composer", "install"]

README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ This is a base image for building [PHP][PHP] [composer] packages.
55
This docker image builds on top of Arch Linux's base/archlinux image for the
66
purpose of building PHP composer packages. It provides several key features:
77

8-
* A non-root user (`build`) for executing the image build. This is important
9-
for security purposes and to ensure that the package doesn't require root
10-
permissions to be built.
118
* Access to the build location will be in the volume located at `/code`. This
129
directory will be the default working directory.
1310
* Composer bin directories are automatically included in `PATH`. Both a
@@ -38,21 +35,31 @@ docker run -i -t --rm -v /tmp/my-code:/code nubs/composer-build composer update
3835
```
3936

4037
## Permissions
41-
This image uses a build user to run composer. This means that your file
42-
permissions must allow this user to write to certain folders like `vendor`.
43-
The easiest way to do this is to create a group and give that group write
44-
access to the necessary folders.
38+
This image runs as root (PID 0), but for security purposes it is recommended to
39+
use Docker's [user namespace functionality][docker-user-namespaces] to map that
40+
to a non-privileged user on your host system.
41+
42+
If you use volume mounting of your project (e.g., to run `composer install`
43+
inside the container but want to modify the host `vendor` directory), then you
44+
may run into permission issues.
45+
46+
Without Docker's user namespaces, the container will create files/directories
47+
with root ownership on your host which may cause issues when trying to access
48+
them as a non-root user.
49+
50+
When using Docker's user namespaces, the container will be running under a
51+
different user. You may have to adjust permissions on the directory to allow
52+
the user to create/modify files. For example, giving an `/etc/setuid` and
53+
`/etc/subgid` that contains `dockremap:165536:65536` and a docker daemon
54+
running using this default mapping: `docker daemon --userns-remap=default`,
55+
you would need to run the following to give the container access to run
56+
`composer install` and yourself access to do so on the host:
4557

4658
```bash
47-
groupadd --gid 55446 composer-build
59+
groupadd --gid 165536 subgid-root
4860
chmod -R g+w vendor
49-
chgrp -R composer-build vendor
50-
```
51-
52-
You may also want to give your user access to files created by the build user.
53-
54-
```bash
55-
usermod -a -G 55446 "$(whoami)"
61+
chgrp -R subgid-root node_modules
62+
usermod -a -G subgid-root "$(whoami)"
5663
```
5764

5865
### Dockerfile build
@@ -66,11 +73,7 @@ process alone could look like this:
6673
```dockerfile
6774
FROM nubs/composer-build
6875

69-
USER root
70-
7176
RUN pacman --sync --noconfirm --noprogressbar --quiet xdebug
72-
73-
USER build
7477
```
7578

7679
You can then build this docker image and run it against your `composer.json`
@@ -89,4 +92,5 @@ the full license text.
8992

9093
[PHP]: http://php.net/ "PHP: Hypertext Preprocessor"
9194
[composer]: https://getcomposer.org/
95+
[docker-use-namespaces]: https://docs.docker.com/engine/reference/commandline/daemon/#daemon-user-namespace-options
9296
[LICENSE]: https://github.com/nubs/docker-composer-build/blob/master/LICENSE

umask.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)