Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
docker_basic_dockerize [2020/01/24 18:43] – created andonovj | docker_basic_dockerize [2020/05/03 11:49] (current) – [Wrong ASP.NET] andonovj | ||
---|---|---|---|
Line 9: | Line 9: | ||
=====Steps===== | =====Steps===== | ||
- | Firstly, you need a Dockerfile in the place of your project: | + | Firstly, you need a Dockerfile in the place of your project. |
+ | Since my project is on C#.NET Core and assuming your application is called: HttpServerDemo.dll, | ||
+ | ==Dockerfile== | ||
+ | <sxh bash> | ||
+ | FROM mcr.microsoft.com/ | ||
+ | WORKDIR /app | ||
+ | # Copy csproj and restore as distinct layers | ||
+ | COPY *.csproj ./ | ||
+ | RUN dotnet restore | ||
+ | # Copy everything else and build | ||
+ | COPY . ./ | ||
+ | RUN dotnet publish -c Release -o out | ||
+ | |||
+ | # Build runtime image | ||
+ | FROM mcr.microsoft.com/ | ||
+ | WORKDIR /app | ||
+ | COPY --from=build-env /app/out . | ||
+ | ENTRYPOINT [" | ||
+ | </ | ||
+ | |||
+ | Now, let's go through the steps: | ||
+ | * Workdir - The workdir on the container which will be used. | ||
+ | * FROM - The external resources which will be used. | ||
+ | * COPY & RUN - These are self explanatory I hope. | ||
+ | |||
+ | You have to verify you have docker at least 17.05 or newer. | ||
+ | My current one is 19.03: | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | Docker version 19.03.5, build 633a0ea | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====Build==== | ||
+ | The building is also done fairy simple. | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool HttpServerDemo]# | ||
+ | Sending build context to Docker daemon | ||
+ | Step 1/10 : FROM mcr.microsoft.com/ | ||
+ | | ||
+ | Step 2/10 : WORKDIR /app | ||
+ | | ||
+ | | ||
+ | Step 3/10 : COPY *.csproj ./ | ||
+ | | ||
+ | | ||
+ | Step 4/10 : RUN dotnet restore | ||
+ | | ||
+ | | ||
+ | Step 5/10 : COPY . ./ | ||
+ | | ||
+ | Step 6/10 : RUN dotnet publish -c Release -o out | ||
+ | | ||
+ | Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core | ||
+ | Copyright (C) Microsoft Corporation. All rights reserved. | ||
+ | |||
+ | Restore completed in 127.94 ms for / | ||
+ | HttpServerDemo -> / | ||
+ | HttpServerDemo -> /app/out/ | ||
+ | Removing intermediate container 34db589b7d0b | ||
+ | | ||
+ | Step 7/10 : FROM mcr.microsoft.com/ | ||
+ | | ||
+ | Step 8/10 : WORKDIR /app | ||
+ | | ||
+ | | ||
+ | Step 9/10 : COPY --from=build-env /app/out . | ||
+ | | ||
+ | Step 10/10 : ENTRYPOINT [" | ||
+ | | ||
+ | Removing intermediate container ff14d57a08fc | ||
+ | | ||
+ | Successfully built 46b4167e5b2d | ||
+ | Successfully tagged httpserverdemo: | ||
+ | [root@postgresqlpgpool HttpServerDemo]# | ||
+ | </ | ||
+ | |||
+ | |||
+ | You can list the images as follows: | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | REPOSITORY | ||
+ | httpserverdemo | ||
+ | </ | ||
+ | |||
+ | ====Start==== | ||
+ | Once the image has been built, you can start the container using that image: | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool HttpServerDemo]# | ||
+ | c23e4bb41a9f660f8b409addb58d398b98dce38e93da00682214141f5bc719c9 | ||
+ | [root@postgresqlpgpool HttpServerDemo]# | ||
+ | </ | ||
+ | |||
+ | Finally, you can check the container: | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | CONTAINER ID IMAGE | ||
+ | 9afb2dcbd6c2 | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====Upload in docker hub==== | ||
+ | Docker hub is the default registry for docker images. You can upload your image there if you want, bare in mind that it will be public. | ||
+ | |||
+ | ==Login== | ||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | Password: | ||
+ | WARNING! Your password will be stored unencrypted in / | ||
+ | Configure a credential helper to remove this warning. See | ||
+ | https:// | ||
+ | |||
+ | Login Succeeded | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | </ | ||
+ | |||
+ | ==Package== | ||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | REPOSITORY | ||
+ | httpserverdemo | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | </ | ||
+ | |||
+ | ==Push== | ||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | The push refers to repository [docker.io/ | ||
+ | 6f2df37fa757: | ||
+ | 64bca5e22c97: | ||
+ | 05682a6a4056: | ||
+ | fcd021389694: | ||
+ | 2c52aed6692d: | ||
+ | c51868eee26f: | ||
+ | 556c5fb0d91b: | ||
+ | latest: digest: sha256: | ||
+ | [root@postgresqlpgpool netcoreapp3.1]# | ||
+ | </ | ||
+ | |||
+ | Now you can also download this simple web from your docker hub or list it as follows: | ||
+ | |||
+ | <sxh bash> | ||
+ | [root@postgresqlpgpool ~]# docker images | ||
+ | REPOSITORY | ||
+ | andonovj/ | ||
+ | [root@postgresqlpgpool ~]# | ||
+ | </ | ||
+ | |||
+ | =====Troubleshooting===== | ||
+ | You can have the following error: | ||
+ | |||
+ | |||
+ | ====Searching wrong Nuget==== | ||
+ | <Code: C#|Nuget Issue> | ||
+ | Restore completed in 41.36 ms for / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | / | ||
+ | The command '/ | ||
+ | </ | ||
+ | |||
+ | In that case, either: | ||
+ | |||
+ | * Delete the " | ||
+ | * Add a command to the " | ||
+ | |||
+ | <Code: bash| Edit Dockerfile> | ||
+ | # copy and build everything else | ||
+ | COPY . ./ | ||
+ | RUN find -type d -name bin -prune -exec rm -rf {} \; && find -type d -name obj -prune -exec rm -rf {} \; <- This line | ||
+ | ENTRYPOINT [" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====Wrong ASP.NET==== | ||
+ | Be careful about the dockerizing the application. I tried to build the application with ASP.NET 2.2 while the app was build with ASP.NET 3.1, so I had to update my Dockerfile as follows: | ||
+ | |||
+ | <Code: bash| Updated Docker File> | ||
+ | FROM mcr.microsoft.com/ | ||
+ | WORKDIR /app | ||
+ | |||
+ | # Copy csproj and restore as distinct layers | ||
+ | COPY *.csproj ./ | ||
+ | RUN dotnet restore | ||
+ | |||
+ | # Copy everything else and build | ||
+ | COPY . ./ | ||
+ | RUN dotnet publish -c Release -o out | ||
+ | |||
+ | # Build runtime image | ||
+ | #FROM mcr.microsoft.com/ | ||
+ | FROM mcr.microsoft.com/ | ||
+ | WORKDIR /app | ||
+ | COPY --from=build-env /app/out . | ||
+ | ENTRYPOINT [" | ||
+ | </ | ||
+ | |||
+ | Otherwise, you will receive the following error: | ||
+ | |||
+ | <Code: bash| Error for wrong ASP.NET version> | ||
+ | It was not possible to find any compatible framework version | ||
+ | The specified framework ' | ||
+ | - Check application dependencies and target a framework version installed at: | ||
+ | / | ||
+ | - Installing .NET Core prerequisites might help resolve this problem: | ||
+ | https:// | ||
+ | - The .NET Core framework and SDK can be installed from: | ||
+ | https:// | ||
+ | - The following versions are installed: | ||
+ | 2.2.8 at [/ | ||
+ | </ |