Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker WORKDIR doesn't create folder

Update:

Here is the code repo: https://github.com/franva/dockerapi/tree/master/dockerapi


I am new to Docker, I used Visual Studio 2017 to add Docker Support, so basically it just creates a Dockerfile under a project folder.

I run:

docker build -t dockerapi:init .

E:\temp\playground\dockerapi (master -> origin) 
λ docker build -t dockerapi:imp . 

Sending build context to Docker daemon  25.09kB 
Step 1/17 : FROM microsoft/dotnet:2.2-aspnetcore-runtime 
AS base  ---> c56aab97bc42 
Step 2/17 : WORKDIR /app  ---> Using cache  --->8f9c09673b05 
Step 3/17 : EXPOSE 80  ---> Using cache  --->7b773fae0164 
Step 4/17 : EXPOSE 443  ---> Using cache  --->a5c32bea1008 
Step 5/17 : FROM microsoft/dotnet:2.2-sdk AS build  --->155911c343f3 
Step 6/17 : WORKDIR "/src"  ---> Using cache  --->b5357cf7b8ef 
Step 7/17 : COPY ["dockerapi/dockerapi.csproj","dockerapi/"]  ---> Using cache  ---> 354963ea2dcd 
Step 8/17 : RUN dotnet restore "dockerapi/dockerapi.csproj"  ---> Using cache  --->7ef0324fd068 
Step 9/17 : COPY . .  ---> 93ef0a22b520 
Step 10/17 : WORKDIR /src/dockerapi  ---> Running in 2179c7f48179 
Removing intermediate container 2179c7f48179  ---> ede8a86afa07 

Step 11/17 : RUN dotnet build "dockerapi.csproj" -c Release -o /app  ---> Running in a4d4864c8208 

Microsoft (R) Build Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C) Microsoft Corporation. All rightsreserved.

  Restore completed in 532.79 ms for /src/dockerapp/dockerapp.csproj. 
  Restore completed in 874.46 ms for /src/dockerapi/dockerapi.csproj.  

dockerapp -> /app/dockerapp.dll   
dockerapi -> /app/dockerapi.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:06.46 Removing intermediate container a4d4864c8208 ---> 84f9f429528d 
Step 12/17 : FROM build AS publish  ---> 84f9f429528d 
Step 13/17 : RUN dotnet publish "dockerapi.csproj" -c
Release -o /app  ---> Running in beed612f6c1f 

Microsoft (R) Build
Engine version 16.1.76+g14b0a930a7 for .NET Core Copyright (C)
Microsoft Corporation. All rights reserved.

  Restore completed in 80.99 ms for /src/dockerapp/dockerapp.csproj.  

  Restore completed in 104.18 ms for /src/dockerapi/dockerapi.csproj.  

dockerapp -> /src/dockerapp/bin/Release/netcoreapp2.2/dockerapp.dll 
dockerapi -> /src/dockerapi/bin/Release/netcoreapp2.2/dockerapi.dll 
dockerapi -> /app/ Removing intermediate container beed612f6c1f  ---> 2969bcee4855 
Step 14/17 : FROM base AS final  ---> a5c32bea1008 
Step 15/17 : WORKDIR /app  ---> Using cache  ---> be6b73f67179 
Step 16/17 : COPY --from=publish /app .  ---> Using cache  ---> f4512e5248b0 
Step 17/17 : ENTRYPOINT ["dotnet", "dockerapi.dll"]  ---> Using cache  --->b60b91d0d89f 

Successfully built b60b91d0d89f Successfully tagged dockerapi:imp 

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

I moved Dockerfile to the solution folder which is 1 level above the project folder, then the docker build ran correctly.

BUT then I took a closer look and found it did not create the src folder

I first got the latest image and run the image:

docker image ls

docker run b60b91d0d89f

then I found the new container

docker ps

then I bashed into the container

E:\temp\playground\dockerapi (master -> origin)
λ docker exec -it f191ed6a110b bash
root@f191ed6a110b:/app# ls
appsettings.Development.json  dockerapi.pdb                     dockerapp.dll                     web.config
appsettings.json              dockerapi.runtimeconfig.dev.json  dockerapp.pdb
dockerapi.deps.json           dockerapi.runtimeconfig.json      dockerapp.runtimeconfig.dev.json
dockerapi.dll                 dockerapp.deps.json               dockerapp.runtimeconfig.json

Here is the code.

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app <--- this line does create app folder
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
WORKDIR "/src" <--- this line doesn't create src folder
COPY ["dockerapi/dockerapi.csproj", "dockerapi/"]
RUN dotnet restore "dockerapi/dockerapi.csproj"
COPY . .
WORKDIR "/src/dockerapi"
RUN dotnet build "dockerapi.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "dockerapi.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "dockerapi.dll"]
like image 580
Franva Avatar asked Jul 15 '26 04:07

Franva


1 Answers

This is totally normal.

Your dockerfile uses multistage build. You should read the documentation about it. Basically, you have one stage (base) which is the image you will publish, and another stage (build) which is were you will build and publish your application.

Everything you do on the build stage won't be published, except if you explicitly copy it to the final image.

Your dockerfile specify you copy your source code to the /src directory of the build stage. Then, you build and publish it, still on the build stage. Once that's done, you copy the results from the build stage to the base stage. This is what the COPY --from=publish /app . instruction does.

It allows you to publish lighter image. Notice the base image is microsoft/dotnet:2.2-aspnetcore-runtime whereas the build image is based on the heavier microsoft/dotnet:2.2-sdk.


Original Answer

The problem is not the /src or /src/dockerapi directory not existing in your image. It's the dockerapi/dockerapi.csproj file which is not found on your host.

This is simply a relative path error. You're executing the docker build command from inside the C:\temp\playground\dockerapp\dockerapi directory. It means, the build context is this the dockerapi directory.

When you you execute this line COPY ["dockerapi/dockerapi.csproj", "dockerapi/"], docker is searching for the C:\temp\playground\dockerapp\dockerapi\dockerapi\dockerapi.csproj (notice the \dockerapi\dockerapi) file which doesn't exists.

Try to change the docker building context by executing the build command from the dockerapp folder and passing the path to the Dockerfile.

C:\temp\playground\dockerapp> docker build -f .\dockerapi\Dockerfile .

See the docker documentation for more info https://docs.docker.com/engine/reference/builder/#usage

like image 51
Justin Lessard Avatar answered Jul 17 '26 16:07

Justin Lessard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!