Run ASP.NET Core on OpenShift

by Carlos Mendible on 26 Jul 2017 » dotNetCore

Today I’ll show you how to Run ASP.NET Core on OpenShift.

First be aware of the following prerequisites:

  • You’ll need a working Docker installation. If you are using Windows 10 you can get Docker for Windows here.
  • Be sure to Disable TLS for 127.0.0.1:2375 for your Docker installation
  • Be sure to set 172.30.0.0/16 as an Insecure Registry in your Docker installation
  • Be sure to set 172.30.1.1:5000 as a Registry Mirror in your Docker installation
  • You’ll need download the oc Client Tools

Note: If you want to learn about OpenShift and what can you do with it, I recommend the free book: Openshift for Developers.

Now let’s start:

1. Create a folder for your new project


Open a command promt an run:

mkdir aspnet.on.openshift

2. Create the project


cd aspnet.on.openshift
dotnet new web

No you have a working Hello World application.

3. Publish your application


Restore the nuget packages and publish your application with the following commands:

dotnet restore
dotnet publish -c release

4. Create a Dockerfile


Create a Dockerfile (be aware of the capital D) with the following contents:

FROM microsoft/dotnet 

# Set ASPNETCORE_URLS
ENV ASPNETCORE_URLS=https://*:8080

# Switch to root for changing dir ownership/permissions
USER 0

# Copy the binaries
COPY /bin/release/netcoreapp1.1/publish app

# Change to app directory
WORKDIR app

# In order to drop the root user, we have to make some directories world
# writable as OpenShift default security model is to run the container under
# random UID.
RUN chown -R 1001:0 /app && chmod -R og+rwx /app

# Expose port 8080 for the application.
EXPOSE 8080

# Run container by default as user with id 1001 (default)
USER 1001

# Start the application using dotnet!!!
ENTRYPOINT dotnet aspnet.on.openshift.dll

5. Create you OpenShift Cluster


Run the following command:

oc cluster up --host-data-dir=/mydata --version=v1.5.1

You should have a working cluster. To get the url of your cluster run:

oc status 

Browse to your cluster (i.e https://10.0.75.2:8443) and login with username: developer and password developer

6. Create an app in OpenShift


Run the following command:

oc new-app . --name=aspnetoc

7. Build your OpenShift image


Run the following command

oc start-build aspnetoc --from-dir=.

8. Create a route so you can access the application


Run the following commands

oc create route edge --service=aspnetoc
oc get route aspnetoc

Copy the host/port output of the previous command (i.e. aspnetoc-myproject.10.0.75.2.nip.io)

9. Check the status and browse to you application


Check the status with the following command

oc status

When your pod has been deployed browse to your application using the host you copied in step 8 (i.e. https://aspnetoc-myproject.10.0.75.2.nip.io).

Enjoy! you just deployed your ASP.NET Core application to OpenShift.

You can get the code here.

Hope it helps!