Skip to content

Commit 9f78ece

Browse files
committed
Add command line
1 parent 77ae643 commit 9f78ece

13 files changed

+383
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/vendor
2+
3+
/composer.lock

composer.json

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,37 @@
33
"description": "Production ready Docker files for Laravel",
44
"type": "library",
55
"license": "MIT",
6-
"autoload": {
7-
"psr-4": {
8-
"BlameButton\\LaravelDockerBuilder\\": "src/"
9-
}
10-
},
116
"authors": [
127
{
138
"name": "Bram Ceulemans",
149
"email": "bram@ceulemans.dev"
1510
}
1611
],
1712
"bin": [
18-
"bin/docker-build",
19-
"bin/docker-php-entrypoint"
13+
"bin/docker-build"
2014
],
21-
"require": {}
15+
"require": {
16+
"php": "^8.0",
17+
"illuminate/console": "^9.47",
18+
"illuminate/contracts": "^9.47",
19+
"illuminate/support": "^9.47",
20+
"twig/twig": "^3.0"
21+
},
22+
"extra": {
23+
"laravel": {
24+
"providers": [
25+
"BlameButton\\LaravelDockerBuilder\\DockerServiceProvider"
26+
]
27+
}
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"BlameButton\\LaravelDockerBuilder\\": "src/"
32+
}
33+
},
34+
"config": {
35+
"optimize-autoloader": true,
36+
"preferred-install": "dist",
37+
"sort-packages": true
38+
}
2239
}

docker/php.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Composer installation
2-
FROM php:8.2-fpm AS composer
2+
FROM php:${PHP_VERSION}-fpm AS composer
33
WORKDIR /app
44
## Install Composer
55
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

docker/template/nginx.dockerfile.twig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{# import node partial, exposed as "node" layer #}
2+
{% include 'partials/node.dockerfile.twig' %}
3+
4+
# Final Image
5+
FROM nginx:1-alpine
6+
7+
WORKDIR /app/public
8+
9+
{% if node_build_tool == 'vite' %}
10+
{# only copy assets if npm build is enabled #}
11+
COPY --from=node /app/public/build/ /app/public/build/
12+
{% elseif node_build_tool == 'mix' %}
13+
COPY --from=node /app/public/css/ /app/public/css/
14+
COPY --from=node /app/public/js/ /app/public/js/
15+
COPY --from=node /app/public/fonts/ /app/public/fonts/
16+
{% endif %}
17+
18+
COPY /public/ /app/public/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{% if node_package_manager %}
2+
FROM node:lts-alpine AS node
3+
WORKDIR /app
4+
5+
{# install node dependencies #}
6+
{% if node_package_manager == 'npm' %}
7+
{# install dependencies with npm #}
8+
COPY /package.json /package-lock.json /app/
9+
RUN npm ci
10+
{% elseif node_package_manager == 'yarn' %}
11+
{# install dependencies with yarn #}
12+
COPY /package.json /yarn.lock /app/
13+
RUN yarn install
14+
{% endif %}
15+
16+
COPY /resources/ /app/resources/
17+
18+
{# build assets #}
19+
{% if node_build_tool == 'vite' %}
20+
COPY /vite.config.js /app/
21+
{# build with vite #}
22+
RUN npm run build
23+
{% elseif node_build_tool == 'mix' %}
24+
COPY /webpack.mix.js /app/
25+
{# build with mix #}
26+
RUN npm run production
27+
{% endif %}
28+
{% endif %}

docker/template/php.dockerfile.twig

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Composer installation
2+
FROM php:{{ php_version }}-fpm AS composer
3+
WORKDIR /app
4+
5+
## Install Composer
6+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
7+
RUN install-php-extensions @composer
8+
## Install dependencies
9+
COPY / /app
10+
RUN composer install --optimize-autoloader --no-dev
11+
12+
{# import node partial, exposed as "node" layer #}
13+
{% include 'partials/node.dockerfile.twig' %}
14+
15+
# Final Image
16+
FROM php:{{ php_version }}-fpm
17+
18+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
19+
RUN install-php-extensions bcmath pdo_pgsql redis
20+
21+
WORKDIR /app
22+
23+
COPY / /app
24+
25+
{% if node_build_tool == 'vite' %}
26+
{# only copy assets if npm build is enabled #}
27+
COPY --from=node /app/public/build/ /app/public/build/
28+
{% elseif node_build_tool == 'mix' %}
29+
COPY --from=node /app/public/css/ /app/public/css/
30+
COPY --from=node /app/public/js/ /app/public/js/
31+
COPY --from=node /app/public/fonts/ /app/public/fonts/
32+
{% endif %}
33+
34+
COPY --from=composer /app/vendor/ /app/vendor/
35+
36+
RUN chown --recursive www-data:www-data /app/storage
37+
38+
{% if artisan_optimize %}
39+
RUN echo "php artisan optimize --no-ansi && php-fpm" >> /usr/bin/entrypoint.sh && \
40+
chmod +x /usr/bin/entrypoint.sh
41+
{% else %}
42+
RUN echo "php-fpm" >> /usr/bin/entrypoint.sh && \
43+
chmod +x /usr/bin/entrypoint.sh
44+
{% endif %}
45+
46+
CMD ["/usr/bin/entrypoint.sh"]

src/Commands/BaseCommand.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
abstract class BaseCommand extends Command
8+
{
9+
private const NONE = 'none';
10+
11+
protected function optionalChoice(string $question, array $choices, $default = null): string|false
12+
{
13+
$choice = $this->choice(
14+
question: $question,
15+
choices: array_merge($choices, [self::NONE]),
16+
default: $default,
17+
);
18+
return $choice === self::NONE ? false : $choice;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands\Choices;
4+
5+
class NodeBuildTool
6+
{
7+
public const VITE = 'vite';
8+
public const MIX = 'mix';
9+
10+
public static function values(): array
11+
{
12+
return [
13+
self::VITE,
14+
self::MIX,
15+
];
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands\Choices;
4+
5+
class NodePackageManager
6+
{
7+
public const NPM = 'npm';
8+
public const YARN = 'yarn';
9+
10+
public static function values(): array
11+
{
12+
return [
13+
self::NPM,
14+
self::YARN,
15+
];
16+
}
17+
}

src/Commands/Choices/PhpVersion.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands\Choices;
4+
5+
class PhpVersion
6+
{
7+
public const v8_2 = '8.2';
8+
public const v8_1 = '8.1';
9+
public const v8_0 = '8.0';
10+
11+
public static function values(): array
12+
{
13+
return [
14+
self::v8_2,
15+
self::v8_1,
16+
self::v8_0,
17+
];
18+
}
19+
}

0 commit comments

Comments
 (0)