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

Mega_Aleksandar's avatar

Laravel through docker container

Hi everyone,

Just putting this out here if anyone has/had any issues like I did. Also, I am interested to find out why is this particular view - 9863e6fd842e71f3f1bb5c3122567b0d.php missing from the composer creation of a new laravel/laravel project. Rather, I get the symphony file_put_contents permission denied.

So, what I have figured out in the past week of banging my head is that, when you have a docker compose build for all services separate (httpd, php, node, mysql, artisan, composer) and using the compose to build and up the containers, you will get errors for permissions when you try to view your Laravel app in the browser. Once you build your images and up the containers, you create a project with docker compose run composer create-project laravel/laravel . (you can even put a command: ["composer", "create-project", "laravel/laravel", "."], wait for it to finish. Then, if you try to view it in the browser, it will complain about views missing or rather the permissions (maybe even the storage/log permissions). Mind you, I am not adding/changing any users from the images themselves, so no uid/gid trickery.

Now comes the interesting part.

The view in question has something to do with the sqlite DB_CONNECTION in the .env, because, when I change it to mysql the app is finally accessible in the browser.

Have not tested it out fully, since I lost a week of my life on this and I just had to "vent out".

So, there you have it folks, hopefuly it helps people out.

Best regards,

Mega Aleksandar

0 likes
2 replies
LaryAI's avatar
Level 58

Hi Mega Aleksandar,

It sounds like you've encountered a common issue with file permissions when using Docker for Laravel development. The problem you're describing is often related to the way Docker handles file permissions and user IDs inside containers. Here’s a step-by-step solution to address the permission issues and ensure your Laravel application runs smoothly in a Docker environment.

Step-by-Step Solution

  1. Adjust File Permissions: Ensure that the storage and bootstrap/cache directories are writable by the web server. You can do this by running the following commands:

    sudo chown -R www-data:www-data storage bootstrap/cache
    sudo chmod -R 775 storage bootstrap/cache
    

    If you are using a different user inside your Docker container, replace www-data with the appropriate user.

  2. Dockerfile and Docker Compose Configuration: Make sure your Dockerfile and docker-compose.yml are set up correctly. Here’s an example configuration:

    Dockerfile:

    FROM php:7.4-fpm
    
    # Install dependencies
    RUN apt-get update && apt-get install -y \
        build-essential \
        libpng-dev \
        libjpeg62-turbo-dev \
        libfreetype6-dev \
        locales \
        zip \
        jpegoptim optipng pngquant gifsicle \
        vim \
        unzip \
        git \
        curl
    
    # Clear cache
    RUN apt-get clean && rm -rf /var/lib/apt/lists/*
    
    # Install PHP extensions
    RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd
    
    # Set working directory
    WORKDIR /var/www
    
    # Copy existing application directory contents
    COPY . /var/www
    
    # Copy existing application directory permissions
    COPY --chown=www-data:www-data . /var/www
    
    # Change current user to www
    USER www-data
    
    # Expose port 9000 and start php-fpm server
    EXPOSE 9000
    CMD ["php-fpm"]
    

    docker-compose.yml:

    version: '3.7'
    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile
        image: laravel-app
        container_name: laravel-app
        restart: unless-stopped
        working_dir: /var/www
        volumes:
          - .:/var/www
        networks:
          - laravel
    
      webserver:
        image: nginx:alpine
        container_name: nginx
        restart: unless-stopped
        ports:
          - "8000:80"
        volumes:
          - .:/var/www
          - ./nginx/conf.d:/etc/nginx/conf.d
        networks:
          - laravel
    
      db:
        image: mysql:5.7
        container_name: mysql
        restart: unless-stopped
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: laravel
          MYSQL_USER: laravel
          MYSQL_PASSWORD: secret
        ports:
          - "3306:3306"
        volumes:
          - dbdata:/var/lib/mysql
        networks:
          - laravel
    
    networks:
      laravel:
    
    volumes:
      dbdata:
    
  3. Nginx Configuration: Ensure you have a proper Nginx configuration. Create a file nginx/conf.d/default.conf with the following content:

    server {
        listen 80;
        index index.php index.html;
        server_name localhost;
        root /var/www/public;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    
        location ~ /\.ht {
            deny all;
        }
    }
    
  4. Database Configuration: Ensure your .env file is correctly configured for MySQL:

    DB_CONNECTION=mysql
    DB_HOST=db
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=laravel
    DB_PASSWORD=secret
    
  5. Rebuild and Start Containers: Rebuild your Docker images and start the containers:

    docker-compose up --build
    
  6. Run Laravel Commands: After the containers are up, you might need to run some Laravel commands to set up your application:

    docker-compose exec app php artisan key:generate
    docker-compose exec app php artisan migrate
    

By following these steps, you should be able to resolve the permission issues and get your Laravel application running smoothly in a Docker environment. If you continue to experience issues, double-check the user permissions and ensure that the Docker containers are correctly configured.

Best regards, LaracastsGPT

Mega_Aleksandar's avatar

@LaryAI Thank you Larry, but the thing is that I am using composer inside of a container to create-project rather than having the files "created" on the host.

Please or to participate in this conversation.