Who doesn’t love flexibility? From programming to juggling chainsaws, the ability to adapt to any situation is always a welcome capability. Enter .NET Core, Docker, and Kentico Cloud. Each of these are extremely capable components of a flexible architecture developers can use to build some amazing solutions. Don’t believe me? Let me show you how…
A Sample Scenario
You are technical evangelist looking to head to some developer conferences. You want to have a working demo of Kentico Cloud, but you don’t necessarily want to host something all the time. You need something light-weight, easily updatable, and at a minimal cost. Sure, you could totally throw it up in Azure and start/stop when you need, but that logic derails this blog, so let’s pretend that’s not an option. The solution? Raspberry Pi + Docker + .NET Core. Yes, that will do nicely! Let me show you how it can work.
Get the Kentico Cloud Boilerplate
Everything starts with a base. In my case, it was the Kentico Cloud .NET Core boilerplate. This community-driven sample site is the creation of some VERY talented partners and Kentico devs to show how to leverage a headless CMS with a super-slim, .NET Core app. This ultra-light application shows how to connect to the Kentico Cloud API, retrieve data, and display it using MVC. It’s a great base for any developer looking to get started with Kentico Cloud and the perfect candidate for some Docker-a-fication.
Want to get the boilerplate? Grab it from here:
For my scenario, I cloned the repo locally and got everything running on my machine.
With the boilerplate in place, the next step was to get Docker. I hadn’t done any previous development with it, so I needed to install the Docker for Windows platform. This created a working Docker environment on my machine, allowing me to deploy containers to my local computer and write a bunch of command line code.
Because I was ultimately going to deploy my image to a Raspberry Pi, I updated my Docker instance in my Taskbar to Linux containers.
Add Docker Support
With Docker installed, the next step was to add Docker support to my app. Visual Studio offers a built-in way to do this right from the context menu. This adds a new project to your solution, along with the associated Docker files. This is a nice way to add support, if you want to only use Visual Studio.
In my case, I opted to create a Docker file manually. This allowed me to set the values exactly as I wanted.
Here is the full code of the Docker file. Note the build command and runtime configuration are set to linux-arm and arm32v7.
FROM microsoft/aspnetcore-build:2 AS build-env
# 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 -r linux-arm
# Build runtime image
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "KenticoCloudNETCoreDockerDemo1.dll"]
Build the Docker Image
With Docker support added, I was ready to build my image. I jumped over to Visual Studio Code to leverage its built-in terminal (and to be cool). To build the image, I executed the following command. (Note the trailing space/period)
docker build -t silver8642/kenticocloudnetcoredockerdemo1 .
This command created the local image of the Docker container. The image could then be uploaded to my Docker Hub account for deployment.
Push the Docker Image
Once the image was created, it needed to be accessible to my Raspberry Pi. I used the following command to push the image to my Docker Hub account. Another option would be to copy the image to a USB drive and deploy manually, but using the interwebs is far more fun.
docker push silver8642/kenticocloudnetcoredockerdemo1
Here is the uploaded image.
With the image created and uploaded, the last piece was to deploy it to my Raspberry Pi. For my environment, I created a new Rasbian instance and made sure everything was updated. Next, I installed Docker using the following command:
curl -sSL https://get.docker.com | sh
Next, I confirmed Docker was running and my containers were empty.
sudo docker ps -a
The next step was to pull my new image and set up a new container. The following command contained everything I needed to pull the image, create a new container that listened on port 80, and set it always to restart.
sudo docker run –restart=always -d -p 80:80 silver8642/kenticoclouddotnetcoredockerdemo1
To confirm the container was successfully created, I ran the container list command again.
sudo docker ps -a
The Final Result
From my laptop, I created a new hosts file entry for the Raspberry Pi’s IP address and confirmed the site displayed properly.
Sweet! I now had a working, portable web server with my Kentico Cloud .NET Core site. This would allow me to access it quickly, and push out updates as needed, all without dealing with any hosting environment.
In this article, I wanted to show just how versatile the Kentico Cloud platform, .NET Core, and Docker can be. By leveraging .NET Core and Docker, you can easily deploy your Kentico Cloud sites on nearly any device. Because Kentico Cloud’s API is technology-agnostic, you could use the same concept for NodeJS, PHP, or any other programming language. I know you will come up with some awesome scenarios and love working with Kentico Cloud. Good luck!
Want to learn more about the process? Check out these links:
You can get my Docker-a-fied Kentico Cloud .NET Core Boilerplate project here:
What You Should Know about the California Consumer Privacy Act (CCPA)
Dec 10, 2019 • 3 minute read
One Digital Agency’s Practical Approach to AI for Customer Engagement – Part 2
Aug 14, 2018 • 6 minute read