php icon indicating copy to clipboard operation
php copied to clipboard

Bug: Docker socket ownership is changed by startup ownership script when APP_GID conflicts with an existing group

Open Amin-Jafari-Manesh opened this issue 1 month ago • 6 comments

Description

When starting containers based on shinsenter/php:8.3-fpm, the startup ownership script can unexpectedly change the ownership of the host-mounted Docker socket (/var/run/docker.sock). This breaks the expected root:docker ownership and can affect Docker access for host services and other containers.

Environment

  • Image: shinsenter/php:8.3-fpm
  • APP_UID=1001
  • APP_GID=998
  • APP_USER=www-data
  • APP_GROUP=www-data
  • DISABLE_AUTORUN_FIX_OWNER_GROUP=1
  • Host Docker socket mounted at /var/run/docker.sock
  • Host GID 998 is the docker group

Observed behavior

The container startup logs include:

groupmod: GID '998' already exists
Changed www-data UID: 33 → 1001
Updating file ownership: 33:33 → 1001:33

Additional startup logs show:

Updating file ownership: 1001:998 → 1001:33

On the host, audit logs recorded a find / -xdev ... -exec chown -h 1001:33 command changing /run/docker.sock ownership to 1001:33 .

Relevant implementation

/usr/local/bin/docker-php-entrypoint unconditionally invokes:

ownership "${APP_GROUP:-www-data}" "${APP_GID:-}" "${APP_USER:-www-data}" "${APP_UID:-}"

/usr/local/utils/ownership runs a filesystem-wide ownership update when the user or group IDs change:

find / -xdev \( -uid "$OLD_UID" -o -gid "$OLD_GID" \) \\
    -exec chown -h "$FINAL_UID":"$FINAL_GID" {} \; 2>/dev/null

The group change fails because GID 998 already exists inside the container as systemd-network, while www-data remains GID 33. The filesystem-wide ownership operation can affect the host-mounted Docker socket.

Setting DISABLE_AUTORUN_FIX_OWNER_GROUP=1 does not prevent this ownership invocation; it only affects a separate ownership-fix hook.

Expected behavior

  1. Startup ownership migration must not change ownership or permissions of host-mounted resources such as /var/run/docker.sock.
  2. Ownership changes should be scoped to intended application directories rather than the entire container filesystem, including mounted host resources.
  3. If the requested GID already exists and group migration fails, the startup script should handle the conflict safely and avoid running a potentially destructive ownership migration.
  4. Please clarify whether DISABLE_AUTORUN_FIX_OWNER_GROUP is intended to disable the entrypoint's ownership command. If so, it should also guard this invocation.

Actual behavior

The startup script attempts to migrate ownership despite the GID conflict, and the host Docker socket changes from root:docker to 1001:33 (1001:www-data`). This behavior was observed in both PHP and Horizon containers mounting the host Docker socket.

Requested fix

Please update the startup ownership logic to handle GID conflicts safely and prevent ownership changes on mounted host resources, especially /var/run/docker.sock. Ideally, make the ownership migration target configurable or restrict it to application paths, and document the intended behavior of DISABLE_AUTORUN_FIX_OWNER_GROUP.

Impact

The Docker socket repeatedly loses its expected ownership, potentially breaking Docker access for services and containers relying on the host socket.

Amin-Jafari-Manesh avatar Oct 26 '26 14:10 Amin-Jafari-Manesh

Thanks for the detailed report and the audit log. The cause is what you described: when groupmod failed because GID 998 already exists, the startup ownership script still ran find / -xdev ... chown, and -xdev does not skip a mounted file such as /var/run/docker.sock.

I have fixed it on the 6.x branch:

  • If groupmod fails, the group keeps its current GID, so no ownership migration is done toward a GID that belongs to another group.
  • The ownership migration now skips mount points, sockets and devices, so host-mounted files such as the Docker socket keep their owner.
  • DISABLE_AUTORUN_FIX_OWNER_GROUP=1 now also stops any chown on APP_PATH and DOCUMENT_ROOT. The README documents this.

The dev- images are being built now: https://github.com/shinsenter/php/actions/runs/40347048369

When the run has finished, you can pull the dev- tag of your image and check again, for example:

docker pull shinsenter/php:dev-8.3-fpm

Run it with the same settings as before (APP_UID=1001, APP_GID=998, the Docker socket mounted), then check ls -ln /var/run/docker.sock on the host. It should still be root:docker. Please tell me if the owner still changes, or if the container behaves differently with the dev- image.

shinsenter avatar Oct 26 '26 15:10 shinsenter

Thanks a lot for the quick fix and for documenting the issue in KNOWN_ISSUES.md.

For now, I've worked around the issue by mounting the Docker socket read-only:

  • /var/run/docker.sock:/var/run/docker.sock:ro

The socket retains its ownership, and Docker API operations still work. However, I'm not sure whether other bind-mounted files and directories are also affected by the ownership migration in the current stable image, including our configuration files, .env, application directory, and storage directory. As far as I understand, -xdev alone does not prevent ownership changes to bind-mounted filesystems. Could you confirm whether these mounts are protected from ownership changes?

I'm planning to use this image in production, so I'd prefer not to pull a -dev image, as it could introduce unexpected changes or disrupt our existing setup.

I see that the fix is included in 6.0.0, which is currently unreleased, contains breaking changes, and has apparently not yet been tested in a container. Would it be possible to backport this fix to the 5.x branch so that it can be included in the stable 8.3-fpm tag without requiring us to adopt the other changes planned for version 6?

Once the fix is released, I'll check the ownership of the socket and other bind-mounted files on the host after container restarts and recreation, and report the results.

There's also a side effect I'd like to clarify. With the previous behavior, even when groupmod -g 998 www-data failed, usermod still set www-data's primary GID to 998. Our setup inadvertently relied on this behavior because our queue workers use runuser -u www-data and need access to the Docker socket, which is owned by root:docker with GID 998.

With the fix, www-data retains its original primary GID of 33, so the queue workers may lose access to the socket.

What is the recommended way to add a supplementary group to APP_USER, such as the host's Docker GID, without changing the user's primary GID or renumbering its existing group? An environment variable to configure supplementary groups would be very useful.

More generally, whenever I've pulled your images in the past, I've had to modify my Docker Compose configuration to accommodate changes in the image. It would be very helpful to have a more stable, production-ready image that preserves compatibility with existing Compose configurations wherever possible.

The latest example was the PHP 8.3.36 security update:

  • Previously, we bind-mounted our own PHP-FPM pool configuration file into /usr/local/etc/php-fpm.d/. It contained per-customer settings such as pm, slowlog, rlimit_files, open_basedir, and session configuration. This file-based approach worked as expected.
  • With the updated image, PHP-FPM starts with -y .../zz-docker-shinsenter-php.conf, and our mounted pool configuration file appears to be silently ignored.
  • As a result, we had to migrate these settings to PHP_* environment variables for every customer just to apply the security update. Would you consider preserving support for mounted php-fpm.d/*.conf files by loading them alongside the generated configuration, or providing a documented custom configuration path? At a minimum, logging a warning when a mounted pool configuration file is not loaded would help prevent silent configuration changes. Clearly identifying potentially breaking changes in the release notes would also make upgrades safer.

Thanks again for maintaining the images and for taking the time to address these issues

Amin-Jafari-Manesh avatar Oct 26 '26 17:10 Amin-Jafari-Manesh

@Amin-Jafari-Manesh

Thanks for the detailed follow-up.

Other mounts: On the current 5.x image, mounted files (config files, .env, the Docker socket) can be changed by the startup ownership migration. The top directory of a bind-mounted directory can be changed too, but its contents are not touched. The fix skips all mount points and sockets.

The application directory is a separate mechanism: the ownership-fix hook can change it recursively, and DISABLE_AUTORUN_FIX_OWNER_GROUP=1 turns it off.

Where the fix ships: The fix is on the 6.x branch and ships with 6.0.0. It will not be backported to 5.x.

Primary group: The old behavior (the primary GID set to 998 even when groupmod failed) was accidental, and the fix removes it. To give www-data access to the Docker socket, add the group as a supplementary group with a boot hook, for example an executable /etc/hooks/onboot/10-docker-group containing usermod -aG 998 www-data. runuser -u www-data then sees the group. I do not plan a built-in variable for this.

Mounted php-fpm.d/*.conf: These images are designed so that PHP and PHP-FPM settings change through PHP_* environment variables. Mounting your own pool file is outside that design, so it is not guaranteed to take effect. If you want to see it supported, please start a Discussion so we can talk it through.

Stability: Noted. Breaking changes are listed in the 6.0.0 changelog.

shinsenter avatar Oct 26 '26 22:10 shinsenter

BTW, "breaking changes" in 6.x remove three redundant variables:

  • APACHE_DOCUMENT_ROOT: use DOCUMENT_ROOT.
  • NGINX_DOCUMENT_ROOT: use DOCUMENT_ROOT.
  • ENABLE_SSH: use ENABLE_SSHD.

Nothing else breaks. If your setup do not use these variables, you do not need to change anything.

shinsenter avatar Oct 26 '26 22:10 shinsenter

I plan to support mounted pool files like this, in 5c78932:

  • PHP-FPM loads /usr/local/etc/php-fpm.d/custom-*.conf after the generated settings, so your values win. Other files in that directory are still not loaded.
  • PHP_PM* variables you pass now win over ENABLE_TUNING_FPM.

I will rebuild the dev- images today so you can test it.

shinsenter avatar Oct 26 '26 22:10 shinsenter

@Amin-Jafari-Manesh About customizing PHP-FPM, please check this: https://github.com/shinsenter/php#customizing-php-fpm-

shinsenter avatar Oct 27 '26 00:10 shinsenter