Search code examples
dockerdocker-volume

Docker Volume mount hiding files?


I have a simple Dockerfile, and I am using the VSCode Docker extension to debug my code.

I see the container is created with a volume mount

--mount "type=bind,source=C:\Users\james\source\repos\aprogram,destination=/app"

When I inspect the container and look at the files though, in app, I can't see my published app to /app/somepath, although the app runs fine and I can debug it, so it must be seeing these files.

I have seen an answer on this SO post (note, this question relates to docker-compose but mentions hiding files at the container level), which suggests that files will be hidden when mounting a volume from the host, but I do not know why. If they are hidden, are the files still there in the background or has the program been started and then removed?

docker compose volumes not contains files

Can anyone please explain why this is happening and point me to any documentation that will help me better understand this?


FROM mcr.microsoft.com/dotnet/sdk:7.0 AS base

EXPOSE 5000

ENV ASPNETCORE_URLS=http://+:5000

WORKDIR /app

COPY . .

RUN dotnet restore

FROM base as publish

RUN dotnet publish "aprogram.csproj" -o /app/somepath

FROM base as final

WORKDIR /app
COPY --from=publish /app/somepath .

ENTRYPOINT ["dotnet", "aprogram.dll"]

Solution

  • See the docker documentation for bind mounts: https://docs.docker.com/storage/bind-mounts/

    If you bind-mount a directory into a non-empty directory on the container, the directory’s existing contents are obscured by the bind mount. This can be beneficial, such as when you want to test a new version of your application without building a new image. However, it can also be surprising and this behavior differs from that of docker volumes.

    Since the files are "obscured", I assume you can think of them as being baked into the image, but overwritten at runtime by the bind mount. So in your terminology, they are still "in the background", but not visible by the "local file system" in the container.