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:
Create a folder for your new project
Open a command promt an run:
1mkdir aspnet.on.openshift
Create the project
1cd aspnet.on.openshift
2dotnet new web
No you have a working Hello World application.
Publish your application
Restore the nuget packages and publish your application with the following commands:
1dotnet restore
2dotnet publish -c release
Create a Dockerfile
Create a Dockerfile (be aware of the capital D) with the following contents:
1FROM microsoft/dotnet
2
3# Set ASPNETCORE_URLS
4ENV ASPNETCORE_URLS=https://*:8080
5
6# Switch to root for changing dir ownership/permissions
7USER 0
8
9# Copy the binaries
10COPY /bin/release/netcoreapp1.1/publish app
11
12# Change to app directory
13WORKDIR app
14
15# In order to drop the root user, we have to make some directories world
16# writable as OpenShift default security model is to run the container under
17# random UID.
18RUN chown -R 1001:0 /app && chmod -R og+rwx /app
19
20# Expose port 8080 for the application.
21EXPOSE 8080
22
23# Run container by default as user with id 1001 (default)
24USER 1001
25
26# Start the application using dotnet!!!
27ENTRYPOINT dotnet aspnet.on.openshift.dll
Create you OpenShift Cluster
Run the following command:
1oc cluster up --host-data-dir=/mydata --version=v1.5.1
You should have a working cluster. To get the url of your cluster run:
1oc status
Browse to your cluster (i.e https://10.0.75.2:8443) and login with username: developer and password developer
Create an app in OpenShift
Run the following command:
1oc new-app . --name=aspnetoc
Build your OpenShift image
Run the following command
1oc start-build aspnetoc --from-dir=.
Create a route so you can access the application
Run the following commands
1oc create route edge --service=aspnetoc
2oc get route aspnetoc
Copy the host/port output of the previous command (i.e. aspnetoc-myproject.10.0.75.2.nip.io)
Check the status and browse to you application
Check the status with the following command
1oc 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!
Comments