axy / docker-dockerfile-builder
Builds a dockerfile
0.0.1
2021-05-14 08:21 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-14 16:18:58 UTC
README
The class that builds dockerfile.
The package is used for specific things. Some options are not tested. Some things are done ineffectively. Using this package is recommended for nobody.
Hierarchy of classes
DockerfileBuileder
- common builderDockerfilePHPBuilder
- added some PHP specific features
Example
use axy\docker\dockerfile\builder\DockerfilePHPBuilder; $builder = new DockerfilePHPBuilder('8.0', 'fpm', false); $builder->labels['author'] = 'Me'; $builder->env['var'] = 'value'; $builder->user = '1000:1000'; $builder->cmd = 'php -m'; $builder->workdir('/var/www/app'); $builder->copy('file.php', 'index.php', '1000:1000'); $builder->packages(['curl', 'libpng-dev', 'libonig-dev', 'zip'], 'Install CURL etc'); $builder->extensions = ['bcmath', 'mbstring', 'pdo_mysql']; $builder->pecl = ['xdebug']; $builder->ini['error_reporting'] = 'E_ALL'; $builder->ini['post_max_size'] = '128M'; echo $builder->build();
Result:
FROM php:8.0-fpm
LABEL author="Me"
ENV var=value
WORKDIR /var/www/app
COPY --chown=1000:1000 file.php index.php
# Install CURL etc
RUN apt-get update && apt-get install -y \
curl \
libpng-dev \
libonig-dev \
zip
RUN docker-php-ext-install \
bcmath \
mbstring \
pdo_mysql \
&& pecl install \
xdebug \
&& docker-php-ext-enable \
xdebug \
&& echo "error_reporting=E_ALL" >> /usr/local/etc/php/conf.d/docker.ini \
&& echo "post_max_size=128M" >> /usr/local/etc/php/conf.d/docker.ini \
&& curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN rm -rf /var/lib/apt/lists/*
CMD php -m
USER 1000:1000
DockerfileBuilder
__construct(public string $image, public bool $isAlpine = true)
image
- the image nameisAlpine
- use alpine distributive (see "Package Manager" below)
Properties
string|array|null $cmd
- CMD instruction (NULL - no CMD)string|array|null $entrypoint
- see CMDstring[] $labels
- LABELs (key => value)string[] $env
- environment variables (key => value)string[] $directives
- directives (key => value)string[] $args
- ARGs (key => value)?string $syntax
-syntax
directive separately?string $escape
-escape
directive separatelystring[] $volume
- list of volumesstring[] $expose
- list of expose portsbool $isPackagesCacheDisabled
- see "Package Manager" below
Methods
All these methods add one instruction to the dockerfile.
The argument $comment
add comment line before the instruction.
instruction(string|string[] $instruction, ?string $comment)
$instruction
- an instruction as string or multiline (joined by "")
run(string|string[] $commands, ?string $comment)
$commands
- a command forRUN
instructions or a list of commands (joined by "&&")
copy(string|string[] $src, string $dest, ?string $chown)
- COPY instructionadd(string|string[] $src, string $dest, ?string $chown)
- ADD instructionworkdir(string $dir, ?string $comment = null)
- WORKDIR instructionpackages(string[] $packages, ?string $comment)
$packages
- a list of packages for installation- See "Package Manager" below
Build
build(bool $pretty = true)
- returns the dockerfile content$pretty
- if FALSE, empty lines and comments will be deleted
Package Manager
- Alpine uses "apk"
- Not alpine uses "apt-get"
- Before install "update" will be performed
- With multiple call
packages()
, "update" will be performed only for the first - If
$isPackagesCacheDisabled
is TRUE (by default)- alpine - install will be executed with
--no-cache
- other - the cache directory will be deleted at the end
- alpine - install will be executed with
DockerfilePHPBuilder
bool $isComposerRequired
- if TRUE (by default) the composer will be installed globallystring[] $extensions
- the list of names of extensions that required for installationstring[] $pecl
- the list of names of extensions that installed via peclstring[] $ini
- the list of php.ini settings (key => value)