Search code examples
dockerdocker-composedocker-volumedocker-image

How to create docker volume using docker-compose file so the app can write files to when running?


I have an app written using ASP.NET Core 3.1 framework. I am trying to create a docker image to allow me to run the app on a Linux system.

My app allows the user to upload files to the server, so it writes the uploaded file onto a folder called "Storage" located on the root folder of my project.

I want to create a permanent storage on the Linux machine so it is not destroyed when the image is removed.

I create the following docker-compose.yml file with instructions on how to create volumes as follow

version: '3.4'

services:
  myproject:
    image: ${DOCKER_REGISTRY-}myproject
    build:
      context: .
      dockerfile: MyProject/Dockerfile
    environment:
    - ASPNETCORE_ENVIRONMENT=Development
    - ASPNETCORE_URLS=https://localhost;http://localhost
    - ASPNETCORE_HTTPS_PORT=44344
    ports:
    - 51736:80
    - 44344:443
    volumes:
      - photos:/app/Storage

volumes:
  photos:

According to my understanding, the volumes located under the myproject service mapps the volume that is called photos to /app/Storage.

However, I am not what command would I use to create the volume on the server so it is not deleted.

How can I correctly create a volume and point the image to use it?


Solution

  • You don't need anything else. You have everything in there already. docker-compose will automatically create the photos volume for you and retain it across container restarts. Check docker volume ls after you start this stack for the first time and you'll see the volume listed.