Skip to content

Commit f3d6ab6

Browse files
committed
Initial commit
1 parent f8d7093 commit f3d6ab6

File tree

10 files changed

+263
-0
lines changed

10 files changed

+263
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Laravel Docker Build
2+
3+
## Installation
4+
5+
```shell
6+
composer require --dev blamebutton/laravel-docker-builder
7+
```
8+
9+
## Usage
10+
11+
Add `DOCKER_IMAGE_TAG` to your `.env` and run:
12+
13+
```shell
14+
vendor/bin/docker/build
15+
```

bin/docker-build

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
source .env
4+
5+
PACKAGE="$(dirname "${BASH_SOURCE[0]}")/.."
6+
7+
if ! [[ -f "${PWD}/public/index.php" ]]; then
8+
echo "Missing [/public/index.php], please run from Laravel base directory.";
9+
exit 1
10+
fi
11+
12+
if ! [[ -f "${PWD}/.dockerignore" ]]; then
13+
echo "Missing [/.dockerignore], copying...";
14+
cp "${PACKAGE}/docker/.dockerignore" "${PWD}/.dockerignore"
15+
fi
16+
17+
TAG="${DOCKER_IMAGE_TAG}"
18+
19+
if [[ -z "${TAG}" ]]; then
20+
echo "Environment variable [DOCKER_IMAGE_TAG] not found."
21+
exit 2
22+
fi
23+
24+
echo "Building [${TAG}:nginx]"
25+
docker build \
26+
--tag "${TAG}:nginx" \
27+
--file "${PACKAGE}/docker/nginx.Dockerfile" \
28+
"${PWD}"
29+
30+
echo "Building [${TAG}:php]"
31+
docker build \
32+
--tag "${TAG}:php" \
33+
--file "${PACKAGE}/docker/php.Dockerfile" \
34+
"${PWD}"

bin/docker-php-entrypoint

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env bash
2+
3+
php artisan config:cache
4+
php artisan route:cache
5+
php artisan view:cache
6+
7+
php-fpm

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "blamebutton/laravel-docker-builder",
3+
"description": "Production ready Docker files for Laravel",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"BlameButton\\LaravelDockerBuilder\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Bram Ceulemans",
14+
"email": "bram@ceulemans.dev"
15+
}
16+
],
17+
"bin": [
18+
"bin/docker-build",
19+
"bin/docker-php-entrypoint"
20+
],
21+
"require": {}
22+
}

demo/docker-compose.yml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
services:
2+
nginx:
3+
image: "laravel-docker:nginx"
4+
depends_on:
5+
- php
6+
volumes:
7+
- "./nginx/app.conf:/etc/nginx/conf.d/default.conf"
8+
ports:
9+
- "80:80"
10+
php:
11+
image: "laravel-docker:php"
12+
depends_on:
13+
- postgres
14+
- redis
15+
- minio
16+
environment:
17+
APP_NAME: "Laravel Docker"
18+
APP_ENV: "production"
19+
APP_KEY: "base64:Avm4zs1yLfogxHpwBZRZhKZJ0EC6/7IX0FcVSyQQlLU="
20+
APP_DEBUG: "false"
21+
APP_URL: "http://localhost"
22+
LOG_CHANNEL: "stderr"
23+
# Postgres configuration
24+
DB_CONNECTION: "pgsql"
25+
DB_HOST: "postgres"
26+
DB_DATABASE: "laravel-app"
27+
DB_USERNAME: "laravel"
28+
DB_PASSWORD: "password"
29+
# Redis configuration
30+
REDIS_HOST: "redis"
31+
REDIS_PORT: "6379"
32+
BROADCAST_DRIVER: "redis"
33+
CACHE_DRIVER: "redis"
34+
QUEUE_CONNECTION: "redis"
35+
SESSION_DRIVER: "redis"
36+
# Minio configuration
37+
FILESYSTEM_DISK: "s3"
38+
AWS_ACCESS_KEY_ID: "laravel"
39+
AWS_SECRET_ACCESS_KEY: "password"
40+
AWS_DEFAULT_REGION: "us-east-1"
41+
AWS_BUCKET: "local"
42+
AWS_URL: "http://localhost:9000"
43+
AWS_ENDPOINT: "http://minio:9000"
44+
AWS_USE_PATH_STYLE_ENDPOINT: "true"
45+
postgres:
46+
image: "postgres:15"
47+
environment:
48+
PGPASSWORD: "password"
49+
POSTGRES_DB: "laravel-app"
50+
POSTGRES_USER: "laravel"
51+
POSTGRES_PASSWORD: "password"
52+
volumes:
53+
- "postgres-data:/var/lib/postgresql/data"
54+
healthcheck:
55+
test: [ "CMD", "pg_isready", "-q", "-d", "laravel-app", "-U", "laravel" ]
56+
retries: 3
57+
timeout: 5s
58+
redis:
59+
image: "redis:alpine"
60+
volumes:
61+
- "redis-data:/data"
62+
healthcheck:
63+
test: [ "CMD", "redis-cli", "ping" ]
64+
retries: 3
65+
timeout: 5s
66+
minio:
67+
image: "minio/minio:latest"
68+
environment:
69+
MINIO_ROOT_USER: "laravel"
70+
MINIO_ROOT_PASSWORD: "password"
71+
ports:
72+
- "9000:9000"
73+
- "8900:8900"
74+
volumes:
75+
- "minio-data:/data/minio"
76+
command: "minio server /data/minio --console-address ':8900'"
77+
healthcheck:
78+
test: [ "CMD", "curl", "-f", "http://localhost:9000/minio/health/live" ]
79+
retries: 3
80+
timeout: 5s
81+
volumes:
82+
postgres-data: { }
83+
redis-data: { }
84+
minio-data: { }

demo/nginx/app.conf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
root /app/public;
5+
6+
add_header X-Frame-Options "SAMEORIGIN";
7+
add_header X-Content-Type-Options "nosniff";
8+
9+
index index.php;
10+
11+
charset utf-8;
12+
13+
location / {
14+
try_files $uri $uri/ /index.php?$query_string;
15+
}
16+
17+
location = /favicon.ico { access_log off; log_not_found off; }
18+
location = /robots.txt { access_log off; log_not_found off; }
19+
20+
error_page 404 /index.php;
21+
22+
location ~ \.php$ {
23+
fastcgi_pass php:9000;
24+
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
25+
include fastcgi_params;
26+
}
27+
28+
location ~ /\.(?!well-known).* {
29+
deny all;
30+
}
31+
}

docker/.dockerignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
3+
/node_modules
4+
/vendor
5+
6+
!/app
7+
!/bootstrap/cache/.gitignore
8+
!/bootstrap/app.php
9+
!/config
10+
!/database
11+
!/lang
12+
!/public
13+
!/resources
14+
!/routes
15+
!/storage
16+
17+
!/artisan
18+
!/composer.json
19+
!/composer.lock
20+
!/package.json
21+
!/package-lock.json
22+
!/vite.config.js

docker/nginx.Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM nginx:1-alpine
2+
3+
WORKDIR /app/public
4+
5+
COPY public/ /app/public/

docker/php.Dockerfile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Composer installation
2+
FROM php:8.2-fpm AS composer
3+
4+
WORKDIR /app
5+
6+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
7+
RUN install-php-extensions @composer
8+
9+
COPY / /app
10+
RUN composer install --optimize-autoloader --no-dev
11+
12+
# Vite.js build
13+
FROM node:lts-alpine AS node
14+
15+
WORKDIR /app
16+
17+
# Cache layer for "npm ci"
18+
COPY /package.json /package-lock.json /app/
19+
RUN npm ci
20+
# Copy JavaScript
21+
COPY /vite.config.js /app/
22+
COPY /resources/ /app/resources/
23+
# Build using Vite.js
24+
RUN npm run build
25+
26+
FROM php:8.2-fpm
27+
28+
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
29+
RUN install-php-extensions bcmath pdo_pgsql redis
30+
31+
WORKDIR /app
32+
33+
COPY / /app
34+
COPY --from=node /app/public/build /app/public/build
35+
COPY --from=composer /app/vendor/ /app/vendor
36+
37+
RUN chown --recursive www-data:www-data /app/storage
38+
39+
RUN echo "php artisan optimize --no-ansi && php-fpm" >> /usr/bin/entrypoint.sh && \
40+
chmod +x /usr/bin/entrypoint.sh
41+
42+
CMD ["/usr/bin/entrypoint.sh"]

0 commit comments

Comments
 (0)