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

reza305's avatar

Can't write file to storage folder in a laravel container

Hi everyone. I'm using docker for developing a laravel application on a ubuntu 20.04 server. The appliction is running wll. But the problem is It can't write files to the storage folder. Here is the controller method:

    public function store(CategoryCreateRequest $request)
    {
        if (!$path = Storage::putFile('media/service/home/category/svg', $request->validated()['icon_file']))
            return back()->withErrors(__('validation.custom.file'));
        Category::create($request->safe()->merge([
            'icon' => $path
        ])->toArray());
        return to_route('admin.home.category.index')->with('success', __('message.admin.create'));
    }

It does not throw an error and even the path value is stored in the database. but there is no files in the storage folder. Here is my docker-compose.yml file:

version: '3.8'
services:

  #PHP Service
  app:
    build: ./laravel
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/laravel
    volumes:
       - ./laravel:/var/www/laravel
       - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: webserver
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./nginx/conf.d/:/etc/nginx/conf.d/
      - web-root:/var/www/html
      - certbot-etc:/etc/letsencrypt
      - certbot-var:/var/lib/letsencrypt
      - dhparam:/etc/ssl/certs
    networks:
      - app-network

  certbot:
    image: certbot/certbot
    container_name: certbot
    volumes:
      - certbot-etc:/etc/letsencrypt
      - certbot-var:/var/lib/letsencrypt
      - web-root:/var/www/html
    depends_on:
      - webserver
    command: certonly --webroot --webroot-path=/var/www/html --email [email protected] --agree-tos --no-eff-email --force-renewal -d example.com  -d www.example.com
  
  #MySQL Service
  db:
    image: mysql:latest
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: example_db
      MYSQL_ROOT_PASSWORD: "something"
      MYSQL_ROOT_HOST: "%"
      MYSQL_HOST_AUTH_METHOD: "trust"
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql
      - ./mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "something"]
      retries: 3
      timeout: 5s
  redis:
    image: 'redis:alpine'
    ports:
      - '6379:6379'
    volumes:
      - 'dbredis:/data'
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      retries: 3
      timeout: 5s
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    container_name: phpmyadmin
    links:
      - db
    depends_on:
      - db
    ports:
      - "8183:80"
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
      MYSQL_ROOT_PASSWORD: "something"
    networks:
      - app-network
#Docker Networks
networks:
  app-network:
    driver: bridge

#Volumes
volumes:
  dbdata:
    driver: local
  dbredis:
    driver: local
  certbot-etc:
  certbot-var:
  web-root:
    driver: local
    driver_opts:
      type: none
      device: /home/reza/example/html/
      o: bind
  dhparam:
    driver: local
    driver_opts:
      type: none
      device: /home/reza/example/dhparam/
      o: bind

and here is laravel app Dockerfile:

FROM php:8.1-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/laravel/

# Set working directory
WORKDIR /var/www/laravel

# Install dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    libonig-dev \
    locales \
    libzip-dev \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd
RUN docker-php-ext-install gd
RUN pecl install -o -f redis \
&&  rm -rf /tmp/pear \
&&  docker-php-ext-enable redis

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www/laravel

# Copy existing application directory permissions
COPY --chown=www:www . /var/www/laravel

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

So what do you think the problem is? any help would be appreciated.

0 likes
2 replies
reza305's avatar

@Sinnbeck Oh I forgot to set "FILESYSTEM_DRIVER" value in my .env file to "public". Thank you very much.

Please or to participate in this conversation.