Code it Yourself…

A blog on Microsoft Azure and .NET by Carlos Mendible

Run ASP.NET Core on OpenShift

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 for your Docker installation
  • Be sure to set as an Insecure Registry in your Docker installation
  • Be sure to set 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 

ENV ASPNETCORE_URLS=https://*:8080

# Switch to root for changing dir ownership/permissions

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

# Change to app directory

# 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.

# 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 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.

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.

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

You can get the code here.

Hope it helps!