Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Ensure reliable /etc/hosts handling for EasyEngine in containerized environments

未关闭
#445 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
docker, php

调研方向

从 src/helper/site-utils.php 中的 remove_etc_hosts_entry 开始,检查 /etc/hosts 的读取和更新方式。在挂载了 /etc/hosts 的 Docker 容器和主机级环境中分别验证其行为,包括读取或写入失败的情况。完成的标准是保留现有的删除行为,同时容器化操作不再静默失败。

由索引模型根据 Issue 内容生成。

描述

Description:

This change ensures that EasyEngine can reliably handle /etc/hosts entries when deployed in Docker or containerized environments, without affecting functionality on host systems.

Context:

When EasyEngine is run inside a container, its attempt to update the host system's /etc/hosts file via a mounted volume may silently fail due to permission or compatibility issues in the Filesystem()->dumpFile() method.

-> Deploy Easyengine-Docker

/**
 * Removes entry of the site from /etc/hosts
 *
 * @param string $site_url site name.
 *
 */
function remove_etc_hosts_entry( $site_url ) {
	$fs = new Filesystem();

	$hosts_file = file_get_contents( '/etc/hosts' );

	$site_url_escaped = preg_replace( '/\./', '\.', $site_url );
	$hosts_file_new   = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file );

	$fs->dumpFile( '/etc/hosts', $hosts_file_new );
}
Objective:

Not to change existing behavior, but to ensure consistent and reliable operation of the remove_etc_hosts_entry function across environments, especially in container deployments.

Modifications:

Refactored remove_etc_hosts_entry in src/helper/site-utils.php to use PHP's native file I/O operations with added safety and compatibility:

  • Replaced Filesystem()->dumpFile() with file_put_contents() and file_get_contents().
  • Added LOCK_EX to ensure atomic writes.
  • Used stream_context_create with 'file' => ['privileged' => true] to improve write permissions handling in volume-mounted /etc/hosts.
  • Implemented preg_quote() to safely match hostnames.
  • Included explicit error handling for file read/write failures.
Benefits:
  • Container Compatibility: Improves EasyEngine’s robustness when run in Docker or similar environments.
  • Backward-Safe: Function continues to work without modification on traditional (host-level) deployments.
  • Resilience: Adds clear error messages in case of failure, reducing silent breakages.
Testing:
  • Verified successful read/write operations to /etc/hosts in:

    • Docker container with volume-mounted /etc/hosts
    • Host-level execution without container
Updated Function:
/**
 * Removes entry of the site from /etc/hosts
 *
 * @param string $site_url site name.
 *
 */
function remove_etc_hosts_entry($site_url) {
    $hosts_file = @file_get_contents('/etc/hosts');
    if ($hosts_file === false) {
        throw new Exception("Failed to read /etc/hosts");
    }

    $site_url_escaped = preg_quote($site_url, '/');
    $hosts_file_new = preg_replace("/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file);

    $context = stream_context_create([
        'file' => ['privileged' => true]
    ]);

    $result = file_put_contents('/etc/hosts', $hosts_file_new, LOCK_EX, $context);
    if ($result === false) {
        throw new Exception("Failed to update /etc/hosts");
    }
}
主要语言
PHP
星标
18
派生
27
平均合并
5 小时 1 分钟
30 天内合并 PR
7

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

EasyEngine/site-command 的其他 Issue

查看 EasyEngine/site-command 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。