Skip to content

Commit

Permalink
reduce file size of docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Nov 14, 2024
1 parent 1391668 commit 68c119f
Showing 2 changed files with 67 additions and 10 deletions.
16 changes: 9 additions & 7 deletions devops/docker_release/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
FROM php:cli
FROM php:8.3.13-cli-alpine3.20

COPY ./phpunuhi.phar /usr/local/bin/phpunuhi
COPY ./run.sh /run.sh
COPY --chmod=0755 ./phpunuhi.phar /usr/local/bin/phpunuhi
COPY --chmod=0755 ./run.sh /run.sh

RUN chmod +x /usr/local/bin/phpunuhi \
&& echo "memory_limit=-1" > /usr/local/etc/php/conf.d/memory-limit.ini \
&& apt-get update && apt-get install -y libicu-dev \
RUN echo "memory_limit=-1" > /usr/local/etc/php/conf.d/memory-limit.ini \
&& apk add --no-cache \
icu-dev \
aspell \
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl \
&& rm -rf /var/lib/apt/lists/*
&& rm -rf /var/cache/apk/*


WORKDIR /app
61 changes: 58 additions & 3 deletions scripts/build.php
Original file line number Diff line number Diff line change
@@ -16,13 +16,63 @@

$phar = new Phar($buildRoot . "/" . $pharName, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, $pharName);

# embed all php files from directory
$phar->buildFromDirectory($srcRoot, '/.$/');
$folder = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($srcRoot));
$items = array();
foreach ($folder as $item) {

$filePath = $item->getPathname();
$relativePath = str_replace($srcRoot . '/', '', $filePath);

// Skip if the file or directory ends with "." or ".."
if (preg_match('#(\.|\.\.)$#', $relativePath)) {
continue;
}

$ignore = [
'.idea/',
'.github/',
'.reports/',
'.run/',
'.svrunit/',
'build',
'devops/',
'schema/',
'scripts/',
'tests/',
'.gitignore',
'.php_cs.php',
'.phpstan.neon',
'infection.js',
'makefile',
'php_min_version.php',
'phparkitect.php',
'phpunit.xml',
'rector.php',
'svrunit.xml',
];

foreach ($ignore as $i) {
if (startsWith($relativePath, $i)) {
continue 2;
}
}

// Skip files in directories starting with "tests" or ".github"
if (preg_match('#^(tests|\.github)/#', $relativePath)) {
continue;
}

$filename = pathinfo($item->getPathName(), PATHINFO_BASENAME);

$items[$relativePath] = $filePath;
}
$phar->buildFromIterator(new ArrayIterator($items));

# set stub to index.php file
$phar->setStub($phar->createDefaultStub("src/index.php"));

echo ">> build complete...";

echo ">> build complete..." . PHP_EOL;


/**
@@ -52,3 +102,8 @@ function deleteDirectory(string $dir): bool

return rmdir($dir);
}

function startsWith(string $haystack, string $needle): bool
{
return strncmp($haystack, $needle, strlen($needle)) === 0;
}

0 comments on commit 68c119f

Please sign in to comment.