How to Dockerize Sample .NET Core API/Web Application

Words
150
Reading
1 min
Listen
Play
7y

I always use docker to run or publish my applications. It makes life so easy to build, publish and update applications on servers.

Example files

I uploaded all example files in to my GitHub Repository.

Sample Application

Use sample code to create .NET Core Web Application
nncggk.png
24q684.png

Docker Ignore

To build an application we only need Source Code, let's ignore unnecessary files like ./obj or ./bin.

Dockerfile

We will use Microsoft official SDK and Runtime Docker Containers to Build and Run .NET Core Application. This will make Application Container lighter since it will not include Source Code, only artifacts and libraries etc. Please replace Application name if needed.

Build

Build Docker Container from Dockerfile file in current directory:
docker build -t web-application .

6wx1dy.png

Execute

Run built web-application container in detached mode with api name and Port 80 accessible to localhost:
docker run -d -p 80:80 --name api web-application

x7ltsw.png

Test

To test if Application runs, open http://localhost URL in a Web Browser.

5v73bk.png

Stop and Remove Container

docker stop api && docker rm api

oduv34.png

How to Dockerize Sample .NET Core API/Web Application | Ecency