Record the Usage of Docker's -v Command

Preface

Previously, I briefly learned Docker to facilitate the deployment of .NET projects (deploying the packaged project).

The Dockerfile is as follows:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5031
EXPOSE 7031

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY . .

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

Then, to download the image and create the container, I used the following commands:

docker build -t app .

docker run -d -p 80:80 --name app app

Problem Occurrence

If I simply use the above commands, the project can be deployed correctly. However, there is a problem: if my project is updated, I need to first delete the container and then delete the image to redeploy. If I only delete the container and redeploy it, the project will not update.

Why does this happen?

It's because I didn't mount any directories; Docker defaults to mounting a randomly named directory. If I don't delete the image, no matter how many containers I create, the project won't change.

Problem Resolution

How to solve this problem is simple: just use the -v command.

As follows:

docker run -d -p 80:80 -v Project Path:/src --name app app

After deploying the container in this way, the mount path in the image will point to your project path. This way, every time the project is updated, you only need to delete the container and then recreate it.

Note: This is a .NET project, with the mount path set to /src. I haven't explored other projects yet; I tried changing this mount path, but it didn't work well. When creating the container, Docker automatically creates an src directory for you inside the container. If there are solutions, please comment on this article; I hope the experts can help me solve my doubts.

Extension

You can use docker inspect Image Name to view the mount path and find Mounts to view the mount information.