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

Propolis's avatar

Docker: Create separate volume for storage

I made a Docker image for my Laravel project. If I create a container for this it works fine. However if my image is updated I need to replace the container. After that the upload data and files in my storage/app/public folder are lost.

That is an undesirable effect. I do want to refresh the entire laravel project when I replace my container with a new version. But I want the upload data of users to remain in the storage/app/public folder.

According to Docker, the solution to this is to create a volume. Because the data will then be saved in the volume. Even if the container is refreshed.

How do I create a volume specific for the storage folder in the Laravel project?

0 likes
6 replies
Sinnbeck's avatar

Add this to the php service in your docker-compose.yml file.

This will map the current directory (where the docker-compose.yml is located) to /app in the container. Change the path on the left side of the : if its the wrong path

        volumes:
            - ./:/app
Propolis's avatar

@Sinnbeck

This line is already there.

Some additional context. I have an Ubuntu development server. Here I am creating a Docker image via docker build. I push this image to Docker hub.

Then I have another server. A Windows server. Here I do a pull to get the new image. I delete the old container. I'm starting a new container for the latest image.

After that I lost my storage data on the Windows Server.

Sinnbeck's avatar

@Propolis Ok strange. I dont use windows so I am not sure how that works there. I recreate my containers quite often, and have never had a loss of storage data. But you can try and be more explicit with the storage directory perhaps?

        volumes:
            - ./:/app
            - c:\path\to\storage:/storage
Propolis's avatar

@Sinnbeck

According to the manual I should be able to create a named volume via yml file with:

volumes:
- appdata:/var/www/storage/app

I also have the following lines in the yml file:

#Volumes
volumes:
  appdata:
    driver: local

I run the container. And when I run the command "docker volume ls" this is what I see:

DRIVER    VOLUME NAME
local     mysql-volume

I only see the mysql-volume. But that is another container. I want to achieve the same with my Laravel container as with the MySQL container.

Propolis's avatar

@Sinnbeck

Yes correct. The laravel application needs to be distributed to different OS environments. I'm now working on a windows server. But Ubuntu servers also follow.

I think I found a solution. I can also specify a volume when I run the "docker run" command. Via the parameter: -v app_storage:/var/www/storage/app

Now a named volume has been succesfully created. I thought I could achieve the same effect via yml. But via command line when starting the container is also workable for me.

1 like

Please or to participate in this conversation.