Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pawooo's avatar
Level 5

Dockerizing Laravel (TALL) app for production, need advice on how to treat .env file/treat optimization commands

I still have a lot to learn, any advice appreciated. I have checked similar posts [1] [2] prior to writing mine.

Want:

A working dockerized Laravel (TALL) application for production that follows every possible best practice/convention.

I run into issues when I run php artisan config:cache as .env file gets cached into bootstrap/cache/config.php and the path values are all local (duh), so the app explodes when dockerized as docker-compose.yml has no idea where/how to look for environment variables in a non-env type file.

    environment:
      - APP_NAME=${APP_NAME}
      - APP_ENV=${APP_ENV}
      - APP_KEY=${APP_KEY}
      - APP_DEBUG=${APP_DEBUG}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}

I also tried running php artisan optimize inside container with .env file, but I get /var/www/service/vendor/autoload.php not found error, so I must be doing something wrong.

Have

AWS EC2 Instance.

.env

APP_NAME=app
APP_ENV=testing
APP_KEY=****
APP_DEBUG=false
APP_URL=****

LOG_CHANNEL=stack
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
# DB_HOST=host.docker.internal
DB_PORT=3306
DB_DATABASE=****
DB_USERNAME=****
DB_PASSWORD=****

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME=https
PUSHER_APP_CLUSTER=mt1

VITE_APP_NAME="${APP_NAME}"
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

docker-compose.yml

services:
  web:
    image: image-name
    container_name: container-name
    environment:
      - APP_NAME=${APP_NAME}
      - APP_ENV=${APP_ENV}
      - APP_KEY=${APP_KEY}
      - APP_DEBUG=${APP_DEBUG}
      - APP_URL=${APP_URL}
      - DB_CONNECTION=${DB_CONNECTION}
      - DB_HOST=${DB_HOST}
      - DB_PORT=${DB_PORT}
      - DB_DATABASE=${DB_DATABASE}
      - DB_USERNAME=${DB_USERNAME}
      - DB_PASSWORD=${DB_PASSWORD}
    ports:
      - '80:80'
    volumes:
      - ./:/var/www/service
    build:
      context: .
      dockerfile: './Dockerfile'

Dockerfile

FROM php:8.3-fpm

WORKDIR /var/www/service

RUN apt-get update && apt-get install -y \
		libfreetype-dev \
		libjpeg62-turbo-dev \
		libpng-dev \
	&& docker-php-ext-configure gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql;

RUN apt-get update && \
    apt-get install -y -q nginx


COPY ./ /var/www/service

COPY ./default.conf /etc/nginx/sites-available/default

# RUN php artisan optimize

RUN mkdir -p /var/www/html/storage/logs \
    && chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 775 /var/www/html/storage 

CMD ["sh", "-c", "php-fpm & nginx -g 'daemon off;'"]

Background:

I've been trying to dockerize my TALL Filament app for production for the past two weeks. Since it's for production and needs to be fast/lightweight, (If my understanding is correct) I cannot use Laravel Sail, though I did use it as a reference at one point. Deployment on AWS EC2.

I figured out (or at least I think) my docker-compose.yml and Dockerfile for the most part (the application is being served), but I don't know how to handle caching/optimization properly.

Any advice would be much appreciated.

0 likes
1 reply
Pawooo's avatar
Pawooo
OP
Best Answer
Level 5

Okay so the answer (at least it works for me, I hoped to get any conventional advice but alas) is:

  1. chmod -R 777 /storage/logs folder (if you wish to save logs outside of container, extra steps required, but hey that's something for another day)
  2. Have both .env file and php artisan config:cache generated file inside your container (?)
  3. Make sure your DB allows for connections from container IP or you will get 500 on any operation that requires DB Auth even if you set DB_HOST to docker.host.internal

Will update this post as I figure things out

Reference that helped me:

  1. https://www.howtoforge.com/how-to-install-laravel-with-docker-on-ubuntu-22-04/
  2. https://stackoverflow.com/questions/56321315/laravel-docker-container-not-connecting-to-local-mysql
  3. https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04
  4. https://stackoverflow.com/questions/56295895/can-not-find-autoload-php-when-running-laravel-in-a-docker-container

Please or to participate in this conversation.