Testing dotnetcore on CentOS Docker image

Continuing the last post, I wanted to try dotnetcore on CentOS.

I’m going to copy the root filesystem structure and install dotnetcore and create a Docker image out of it.

[dotnet]# cp -R centos_image/ centos_image_dotnetcore
[dotnet]# export centos_root='/centos_image_dotnetcore/rootfs'
[dotnet]# cp /etc/resolv.conf $centos_root/etc
[dotnet]# mount -o bind /dev $centos_root/dev
[dotnet]# chroot $centos_root /bin/bash <<EOF
> rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
> yum install aspnetcore-runtime-2.2 -y
> yum clean all
> EOF
Retrieving https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
warning: /var/tmp/rpm-tmp.vnu7hw: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
Preparing... ################################# [100%]
 package packages-microsoft-prod-1.0-1.el7.noarch is already installed
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.0x.sg
 * extras: mirror.0x.sg
 * updates: mirror.0x.sg
Resolving Dependencies
--> Running transaction check
---> Package aspnetcore-runtime-2.2.x86_64 0:2.2.2-1 will be installed
--> Processing Dependency: dotnet-runtime-2.2 >= 2.2.2 for package: aspnetcore-runtime-2.2-2.2.2-1.x86_64
--> Running transaction check
---> Package dotnet-runtime-2.2.x86_64 0:2.2.2-1 will be installed
--> Processing Dependency: dotnet-runtime-deps-2.2 >= 2.2.2 for package: dotnet-runtime-2.2-2.2.2-1.x86_64
--> Processing Dependency: dotnet-hostfxr-2.2 >= 2.2.2 for package: dotnet-runtime-2.2-2.2.2-1.x86_64
--> Running transaction check
---> Package dotnet-hostfxr-2.2.x86_64 0:2.2.2-1 will be installed
--> Processing Dependency: dotnet-host >= 2.2.2 for package: dotnet-hostfxr-2.2-2.2.2-1.x86_64
---> Package dotnet-runtime-deps-2.2.x86_64 0:2.2.2-1 will be installed
--> Processing Dependency: libicu for package: dotnet-runtime-deps-2.2-2.2.2-1.x86_64
--> Running transaction check
---> Package dotnet-host.x86_64 0:2.2.2-1 will be installed
---> Package libicu.x86_64 0:50.1.2-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================================================================
 Package Arch Version Repository Size
=====================================================================================================================================================
Installing:
 aspnetcore-runtime-2.2 x86_64 2.2.2-1 packages-microsoft-com-prod 30 M
Installing for dependencies:
 dotnet-host x86_64 2.2.2-1 packages-microsoft-com-prod 45 k
 dotnet-hostfxr-2.2 x86_64 2.2.2-1 packages-microsoft-com-prod 196 k
 dotnet-runtime-2.2 x86_64 2.2.2-1 packages-microsoft-com-prod 27 M
 dotnet-runtime-deps-2.2 x86_64 2.2.2-1 packages-microsoft-com-prod 2.8 k
 libicu x86_64 50.1.2-17.el7 base 6.9 M

Transaction Summary
=====================================================================================================================================================
Install 1 Package (+5 Dependent packages)

Total download size: 64 M
Installed size: 81 M
Downloading packages:
warning: [fd 20]: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY ] 0.0 B/s | 0 B --:--:-- ETA
Public key for dotnet-host-2.2.2-x64.rpm is not installed
(1/6): dotnet-host-2.2.2-x64.rpm | 45 kB 00:00:00
(2/6): dotnet-hostfxr-2.2.2-x64.rpm | 196 kB 00:00:00
(3/6): dotnet-runtime-2.2.2-x64.rpm | 27 MB 00:00:01
(4/6): dotnet-runtime-deps-2.2.2-rhel.7-x64.rpm | 2.8 kB 00:00:00
(5/6): aspnetcore-runtime-2.2.2-x64.rpm | 30 MB 00:00:03
(6/6): libicu-50.1.2-17.el7.x86_64.rpm | 6.9 MB 00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------
Total 18 MB/s | 64 MB 00:00:03
Retrieving key from https://packages.microsoft.com/keys/microsoft.asc
Importing GPG key 0xBE1229CF:
 Userid : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
 Fingerprint: bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
 From : https://packages.microsoft.com/keys/microsoft.asc
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
 Installing : libicu-50.1.2-17.el7.x86_64 1/6
 Installing : dotnet-runtime-deps-2.2-2.2.2-1.x86_64 2/6
 Installing : dotnet-host-2.2.2-1.x86_64 3/6
Creating dotnet host symbolic link: /usr/bin/dotnet
 Installing : dotnet-hostfxr-2.2-2.2.2-1.x86_64 4/6
 Installing : dotnet-runtime-2.2-2.2.2-1.x86_64 5/6
 Installing : aspnetcore-runtime-2.2-2.2.2-1.x86_64 6/6
 Verifying : dotnet-runtime-2.2-2.2.2-1.x86_64 1/6
 Verifying : dotnet-runtime-deps-2.2-2.2.2-1.x86_64 2/6
 Verifying : dotnet-hostfxr-2.2-2.2.2-1.x86_64 3/6
 Verifying : dotnet-host-2.2.2-1.x86_64 4/6
 Verifying : aspnetcore-runtime-2.2-2.2.2-1.x86_64 5/6
 Verifying : libicu-50.1.2-17.el7.x86_64 6/6

Installed:
 aspnetcore-runtime-2.2.x86_64 0:2.2.2-1

Dependency Installed:
 dotnet-host.x86_64 0:2.2.2-1 dotnet-hostfxr-2.2.x86_64 0:2.2.2-1 dotnet-runtime-2.2.x86_64 0:2.2.2-1 dotnet-runtime-deps-2.2.x86_64 0:2.2.2-1
 libicu.x86_64 0:50.1.2-17.el7

Complete!
Loaded plugins: fastestmirror
Cleaning repos: base extras packages-microsoft-com-prod updates
Cleaning up list of fastest mirrors
[dotnet]# rm -f $centos_root/etc/resolv.conf
[dotnet]# umount $centos_root/dev
[dotnet]# tar -C $centos_root -c . | docker import - centos-dotnetcore
sha256:f5953a7e49df031d7c6199ef20e79e27d208cfeafec1ad51d8957883720c3494
[dotnet]#

Checking our newly created image

[dotnet]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-dotnetcore latest f5953a7e49df 44 seconds ago 378 MB
centos latest 6fced9621cdc 25 hours ago 193 MB
docker.io/registry 2 d0eed8dad114 5 weeks ago 25.8 MB
[dotnet]#

Publishing an ASP.NET web application, I copied it to my docker host and created the following Dockerfile. (NOTE: Need to secure this. This runs as root)

[dotnet]# cat Dockerfile
# Use the recently built centos-dotnetcore as the parent image
FROM centos-dotnetcore

# Set the working directory in the container to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Make the container's port 5000 available to the outside world
EXPOSE 5000

# Run when the container launches
CMD ["/bin/dotnet", "/app/Release/netcoreapp2.2/publish/FusionApp.dll", "--urls=http://0.0.0.0:5000/"]

Doing a docker build

 [dotnet]#  docker build -t dotnet-webapp .

And checking our images

[dotnet]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dotnet-webapp latest 5f7d3e66ae78 About a minute ago 383 MB
centos-dotnetcore latest 390de8bce490 50 minutes ago 378 MB
centos latest 6fced9621cdc 27 hours ago 193 MB

And running a container out of the newly created image

[dotnet]# docker run -d -p 5001:5000 --name webapp dotnet-webapp
7b97c0e8c7bc9c602ca8ac3eff89194e1c9c3948cb0f845c77d387c723ca5879
[dotnet]#

Let’s check if our container is running

[dotnet]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7b97c0e8c7bc dotnet-webapp "/bin/dotnet /app/..." 34 seconds ago Up 32 seconds 0.0.0.0:5001->5000/tcp webapp

Let’s check the logs

[dotnet]# docker logs webapp
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
 User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[58]
 Creating key {36f9c085-9148-44b1-9e06-92824fd7b5e8} with creation date 2019-03-09 13:05:26Z, activation date 2019-03-09 13:05:26Z, and expiration date 2019-06-07 13:05:26Z.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
 No XML encryptor configured. Key {36f9c085-9148-44b1-9e06-92824fd7b5e8} may be persisted to storage in unencrypted form.
info: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[39]
 Writing data to file '/root/.aspnet/DataProtection-Keys/key-36f9c085-9148-44b1-9e06-92824fd7b5e8.xml'.
Hosting environment: Production
Content root path: /app
Now listening on: http://0.0.0.0:5000
Application started. Press Ctrl+C to shut down.
[dotnet]#

Checking our app from browser