diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 087a4b71..8a4060d4 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -764,19 +764,32 @@ function sysctl_parameters() { /** * Removes entry of the site from /etc/hosts + * Optimized for Docker environments to prevent Inode/Device busy errors. * * @param string $site_url site name. - * + * @throws Exception If /etc/hosts cannot be read or written. */ function remove_etc_hosts_entry( $site_url ) { - $fs = new Filesystem(); - $hosts_file = file_get_contents( '/etc/hosts' ); + // 1. Read /etc/hosts file; suppress warnings and handle failure manually + $hosts_file = @file_get_contents( '/etc/hosts' ); + if ( $hosts_file === false ) { + throw new Exception( "Failed to read /etc/hosts" ); + } + + // 2. Escape special characters in domain (e.g., dots) for regex safety + $site_url_escaped = preg_quote( $site_url, '/' ); + + // 3. Remove the specific line matching the Localhost IP and the site URL + $hosts_file_new = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file ); - $site_url_escaped = preg_replace( '/\./', '\.', $site_url ); - $hosts_file_new = preg_replace( "/127\.0\.0\.1\s+$site_url_escaped\n/", '', $hosts_file ); + // 4. Overwrite file directly to maintain the Inode (fixes Docker mount issues) + // Using LOCK_EX to prevent race conditions during write + $result = file_put_contents( '/etc/hosts', $hosts_file_new, LOCK_EX ); - $fs->dumpFile( '/etc/hosts', $hosts_file_new ); + if ( $result === false ) { + throw new Exception( "Failed to update /etc/hosts" ); + } } /**