HUB
Sign inSign up

jensr22/php-production

By jensr22

•Updated 13 days ago

PHP image configured for production. Includes composer, node, npm

Image
Languages & frameworks
0

615

jensr22/php-production repository overview

⁠IMPORTANT

Inside the prod container run commands as www-data like this:

gosu www-data php artisan optimize

⁠On local machine

Create following files in your project, see bottom section (Dockerfiles) .dockerignore file, see bottom for an example made for a Laravel project

docker build -t sudoku_app:latest -f prod/Dockerfile .
docker build -t sudoku_nginx:latest -f prod/Dockerfile.nginx .
docker build -t sudoku_postgres:latest -f prod/Dockerfile.postgres .
docker save -o sudoku_app.tar sudoku_app:latest
docker save -o sudoku_nginx.tar sudoku_nginx:latest
docker save -o sudoku_postgres.tar sudoku_postgres:latest
scp sudoku_app.tar prod:/var/www/sudoku
scp sudoku_nginx.tar prod:/var/www/sudoku
scp sudoku_postgres.tar prod:/var/www/sudoku

⁠On prod server

Run commands only first time on prod server to setup initial deployment

mkdir -p data/app config data/database
touch config/.env
touch config/nginx.conf
touch docker-compose.yml
  1. Copy contents of .env.example to config/.env and edit values for production
  2. Copy this to docker-compose.yml
services:
  app:
    image: sudoku_app:latest
    container_name: sudoku-prod-app
    restart: unless-stopped
    volumes:
      - ./data/app:/var/www/storage
      - ./config/.env:/var/www/.env
    command:
      - sh
      - -c
      - |
        mkdir -p storage/app/public/uploadFileStorage storage/framework/cache/data storage/framework/views storage/framework/sessions storage/logs bootstrap/cache &&
        chown -R www-data:www-data storage bootstrap/cache &&
        gosu www-data php artisan optimize &&
        exec php-fpm

  nginx:
    image: sudoku_nginx:latest
    container_name: sudoku-prod-nginx
    restart: unless-stopped
    ports:
      - "127.0.0.1:81:80"
    volumes:
      - ./data/app:/var/www/storage
      - ./config/nginx.conf:/etc/nginx/conf.d/default.conf

  postgres:
    image: sudoku_postgres:latest
    container_name: sudoku-prod-postgres
    restart: unless-stopped
    ports:
      - "127.0.0.1:5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
    volumes:
      - ./data/database:/var/lib/postgresql/18/docker

  # login (system: PostgreSQL, server: postgres, username: postgres, password: postgres, database: postgres)
  adminer:
    image: adminer
    container_name: sudoku-prod-adminer
    restart: unless-stopped
    ports:
      - "127.0.0.1:8082:8080"
  1. Copy this to config/nginx.conf
server {
    listen 80;
    index index.php;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    client_max_body_size 100M;

    # PHP-FPM lives in the "app" compose service (use the service name, not container_name).
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    # Never serve dotfiles (e.g. a stray .env).
    location ~ /\.(?!well-known).* {
        deny all;
    }
}
docker load -i /var/www/sudoku/sudoku_app.tar && docker load -i /var/www/sudoku/sudoku_nginx.tar && docker load -i /var/www/sudoku/sudoku_postgres.tar
docker compose up -d
docker image prune

⁠Local dockerfiles

⁠prod/Dockerfile

# ============================================================
# Stage 1: Node build (JS/CSS assets)
# ============================================================
FROM jensr22/php-production:8.5 AS build

# copy app source
COPY . /var/www/
RUN chmod -R a+rX /var/www
RUN rm -r ./html

RUN mkdir -p bootstrap/cache storage/app/public/uploadFileStorage storage/framework/cache/data storage/framework/views storage/framework/sessions storage/logs

RUN composer install --prefer-dist --no-interaction --no-dev --optimize-autoloader --no-ansi --no-progress
RUN npm ci
RUN npm run build
RUN npm prune --omit=dev

### ============================================================
### Stage 2: Final image
### ============================================================
FROM jensr22/php-production:8.5 AS final

# Copy app source
COPY . /var/www/
RUN rm -rf ./html

# Copy build artifacts from previous stages
COPY --from=build /var/www/vendor ./vendor
COPY --from=build /var/www/public/build ./public/build

RUN chmod -R a+rX /var/www

# Run post-install Laravel setup
RUN mkdir -p bootstrap/cache storage/app/public/uploadFileStorage storage/framework/cache/data storage/framework/views storage/framework/sessions storage/logs
RUN rm -f bootstrap/cache/packages.php bootstrap/cache/services.php
RUN php artisan package:discover --ansi
RUN php artisan storage:link
# Dont do 'php artisan optimize' Here since it will cache the local .env values (config:cache)

RUN touch .env \
  && touch storage/logs/laravel.log \
  && mkdir -p storage/app/public/uploadFileStorage \
  && chmod -R 755 storage/ \
  && chmod -R 755 bootstrap/ \
  && chown -R www-data:www-data storage/ bootstrap/cache/

# Set aliases
RUN echo "alias a='php artisan'" >> ~/.bashrc
RUN echo "alias csfix='./vendor/bin/php-cs-fixer fix --allow-risky=yes'" >> ~/.bashrc

CMD ["php-fpm"]

⁠prod/Dockerfile.nginx

FROM sudoku_app:latest AS app
FROM nginx:1.26.1
COPY --from=app /var/www/public /var/www/public

⁠.dockerignore example for Laravel

###############################
# Copied from /app/.gitignore
###############################
.phpunit.cache
bootstrap/ssr
node_modules
public/build
public/hot
public/storage
storage/*.key
vendor
.env
.env.example
.env.backup
.env.production
.phpunit.result.cache
Homestead.json
Homestead.yaml
auth.json
npm-debug.log
yarn-error.log
.fleet
.idea
.vscode
worker.log
.gitignore
.gitattributes
tests
README.md
storage/logs/*.log
storage/app/public/uploadFileStorage
public/fonts-manifest.dev.json
storage/pail
resources/js/actions
resources/js/routes
resources/js/wayfinder
.DS_Store
.phpactor.json
.nova
.zed
.playwright/auth/
playwright-report/
test-results/
# Playwright
blob-report/
playwright/.cache/
playwright/.auth/
e2e/.auth/
# Scribe
.scribe
public/vendor/scribe
resources/views/scribe
# AI skills
.agents
.github/skills
.junie
GEMINI.md
.php-cs-fixer.cache

###############################
# Copied from /.gitignore
###############################
backups
backups/*
**/*.sql
**/*.zip
_ide_helper*.php
.ai/mcp

data_postgres_18
data

bootstrap/cache

Tag summary

Content type

Image

Digest

sha256:c22d5e2fe…

Size

309.9 MB

Last updated

over 1 year ago

docker pull jensr22/php-production