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

ioiofadhil's avatar

Docker removing Laravel Storage Image

Hi, so I just implemented docker container for my Laravel Application. But what I don't understand is, the container doesn't mirror the latest build of my image. I tried to restart the container, It's still using the old codes. What's working for me is remove completely container using docker compose down --volume and up again using docker compose up -d. But i lost my storage images. How to correctly updating my codes?

  • docker-compose.yml
version: '3'
services:
  php-fpm:
    image: laravel_image:1.0.0
    volumes:
      - shared-data:/var/www/html:consistent
    environment:
      - APP_ENV=local
      - ...
  nginx:
    image: nginx:latest
    ports:
      - "8080:443"
    volumes:
      - shared-data:/var/www/html:consistent
      - ...
    depends_on:
      - php-fpm
  
volumes:
  shared-data:
  • Dockerfile
# Install PHP FPM
FROM php:8.1-fpm

# apt-get update
RUN apt-get update

# One Line
RUN apt-get install -y libicu-dev libxml2-dev libssl-dev libzip-dev libpng-dev software-properties-common

# Install PDO and PDO_SQL for PostgreSQL
RUN apt-get update && \
    apt-get install -y libpq-dev && \
    docker-php-ext-install pdo pdo_pgsql


# Libraries
RUN apt-get update && apt-get install -y zlib1g-dev && docker-php-ext-install zip

# Set the working directory in the container
WORKDIR /var/www/html

# COPY
COPY . /var/www/html
RUN cp ./.env.example ./.env

# Set permissions for the /storage directory (adjust permissions as needed)
RUN chown -R www-data:www-data /var/www
RUN chmod -R 777 /var/www/html/storage
RUN chmod -R 777 /var/www/html/bootstrap/cache

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

# Install application dependencies using Composer
COPY ./composer.json ./composer.lock /var/www/html/

# Create a non-root user and switch to it
RUN adduser --disabled-password --gecos '' myuser
USER myuser

# Install
RUN composer install --no-interaction

# Switch back to root user if needed
USER root

# Start PHP FPM
CMD ["php-fpm"]
0 likes
1 reply
Tray2's avatar

That is how docker works, it only builds the container if it does not previously exists. If you want to update the image, then you need to rebuild it, there is probably some commandline option to force it to build every time.

Any files you want to keep, source files, uploaded images, database files, and so on, needs to be stored outside the container.

Please or to participate in this conversation.