docker – Volume not mounting to .NET project


I’m trying to connect a volume to a .net project, and I’m facing many difficulties with getting it to work. For example, if i set the volume path to be /app/data it will save it inside the project itself which defeats the purpose of persistent storage. When I set the volume like this:

 "//c/Users/smedawar/cms_data:/home/app/cms_data"

My C# code is saving correctly without giving exceptions, bu the cms_data folder inside the smedawar is empty.

Upon analyzing the containers windows in visual studio, I find no mention of the cms_data folder so I don’t think it’s properly adding the volume:
enter image description here

Here’s my launch profile:

"Container (Dockerfile)": {
  "commandName": "Docker",
  "launchBrowser": true,
  "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
  "environmentVariables": {
    "ASPNETCORE_HTTPS_PORTS": "8081",
    "ASPNETCORE_HTTP_PORTS": "8080"
  },
  "publishAllPorts": true,
  "useSSL": true,
  "dockerRun": {
    "volumes": [
      "//c/Users/smedawar/cms_data:/home/app/cms_data"
    ]
  }
}

And this is my docker file:

# -------------------------
# Base runtime image
# -------------------------
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base

USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081

# -------------------------
# Build stage
# -------------------------
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src


# Copy NuGet config
COPY ["nuget.config", "."]

# Copy all project files for restore
COPY [REDACTED]

# Restore dependencies
RUN dotnet restore [REDACTED]

# Copy all source code
COPY . .

# Build the application
WORKDIR "[REDACTED]"
RUN dotnet build "[REDACTED]" -c $BUILD_CONFIGURATION -o /app/build

# -------------------------
# Publish stage
# -------------------------
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "[REDACTED]" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# -------------------------
# Final stage
# -------------------------
FROM base AS final
WORKDIR /app

# Copy published app
COPY --from=publish /app/publish .

# Install curl and gosu for health checks and user switching
USER root
RUN apt-get update && \
    apt-get install -y curl gosu && \
    rm -rf /var/lib/apt/lists/*


# Start script
RUN echo '#!/bin/bash\n\
echo "Initializing [REDACTED] application..."\n\
chown -R app:app /app/data\n\
chmod -R 755 /app/data\n\
echo "Starting [REDACTED]application..."\n\
exec gosu app dotnet [REDACTED].Web.dll' > /app/start.sh && \
chmod +x /app/start.sh

# Switch back to app user
USER app

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  CMD curl -f http://localhost:8080/ || exit 1

ENTRYPOINT ["/app/start.sh"]

    enter code here

C# Code:

if (!Directory.Exists(fullDockerPath))
{
      Directory.CreateDirectory(fullDockerPath);
      LogHelper.LogError(_webHostEnvironment.WebRootPath, null, extramessage: $"Created Docker volume directory: {fullDockerPath}");
}

using (FileStream fileStream = new FileStream(fullFilePath, FileMode.Create))
{
       await file.CopyToAsync(fileStream);
}

EDIT: if I change the volume to /app/data for example, the code to save files will save it in the actual src folder instead of skwid_data. for example, it will create a folder called /data inside the root folder of the project and put the image there. So it seems like the volume isn’t even connecting at all.

Leave a Reply

Your email address will not be published. Required fields are marked *