This is an old revision of the document!
Overview
After we learned a little bit a docker and containers, let's use docker for what it was meant to :) I have created a simple HTTP server which listens on port: 1234
Source code you can see in my Git
Packaging application in image, we call dockerize and it is done pretty simle.
Steps
Firstly, you need a Dockerfile in the place of your project. Since my project is on C#.NET Core, I have used the following Dockerfile
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env 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/dotnet/core/aspnet:2.2 WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT ["dotnet", "HttpServerDemo.dll"]
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:
[root@postgresqlpgpool netcoreapp3.1]# docker --version Docker version 19.03.5, build 633a0ea
Build
The building is also done fairy simple.
[root@postgresqlpgpool HttpServerDemo]# docker build -t httpserverdemo . Sending build context to Docker daemon 398.3kB Step 1/10 : FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env ---> 2fe8fe202baf Step 2/10 : WORKDIR /app ---> Using cache ---> 931056fe84ac Step 3/10 : COPY *.csproj ./ ---> Using cache ---> 33c701ccd6cf Step 4/10 : RUN dotnet restore ---> Using cache ---> 2acbc600037a Step 5/10 : COPY . ./ ---> 68177b9bdcf6 Step 6/10 : RUN dotnet publish -c Release -o out ---> Running in 34db589b7d0b 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 /app/HttpServerDemo.csproj. HttpServerDemo -> /app/bin/Release/netcoreapp3.1/HttpServerDemo.dll HttpServerDemo -> /app/out/ Removing intermediate container 34db589b7d0b ---> d81f005279bc Step 7/10 : FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 ---> 5b704ff3cb6b Step 8/10 : WORKDIR /app ---> Using cache ---> e35d6ba6b507 Step 9/10 : COPY --from=build-env /app/out . ---> dc3b27c05e4d Step 10/10 : ENTRYPOINT ["dotnet", "HttpServerDemo.dll"] ---> Running in ff14d57a08fc Removing intermediate container ff14d57a08fc ---> 46b4167e5b2d Successfully built 46b4167e5b2d Successfully tagged httpserverdemo:latest [root@postgresqlpgpool HttpServerDemo]#
You can list the images as follows:
[root@postgresqlpgpool netcoreapp3.1]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE httpserverdemo latest 4ab730ac8ab5 6 hours ago 207MB
Start
Once the image has been built, you can start the container using that image:
[root@postgresqlpgpool HttpServerDemo]# docker run -d httpserverdemo 80:1234 c23e4bb41a9f660f8b409addb58d398b98dce38e93da00682214141f5bc719c9 [root@postgresqlpgpool HttpServerDemo]#
Finally, you can check the container:
[root@postgresqlpgpool netcoreapp3.1]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9afb2dcbd6c2 httpserverdemo "dotnet HttpServerDe…" 5 hours ago Up 5 hours 0.0.0.0:80->1234/tcp web [root@postgresqlpgpool netcoreapp3.1]#