how to dockerize core application


To dockerize your existing ASP.NET Core application, you need to install docker first. This article will explain on how to leverage docker-compose.  To do  development inside a docker container, you need to create a docker image.
Let’s open a terminal and create a directory to put our project files, call it aspnetdocker.
Then create a new mvc project, or in this example we will clone an existing core application from here.

git clone LatihanUKSW
git clone LatihanUKSW


As you can see there is a Dockerfile inside directory LatihanUKSW, let’s examine the content:

FROM microsoft/dotnet:2.1-sdk AS build
COPY . .
RUN dotnet restore
WORKDIR /app/Latihan.Web
RUN dotnet publish -c Release -o out
FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine AS runtime
COPY –from=build /app/Latihan.Web/out ./
ENTRYPOINT [ "dotnet","Latihan.Web.dll","–urls","" ]

Here we use dotnet 2.1 sdk image from Microsoft, because the LatihanUKSW project was built using dotnet core 2.1.  Then give it a name : build, after that set the working directory to /app then copy current directory contents to the working directory and then run dotnet restore to restore the dotnet packages.
After restoring the packages, set the working directory to /app/Latihan.Web  then run dotnet publish to publish the application to folder out.
Then initialize a new build stage with microsoft/dotnet:2.1-aspnetcore-runtime-alpine as the base image, set the name to runtime. Set the working directory to /app, copy the contents of /app/Latihan.Web/out from build stage to current workdir whic is /app. And finally set the entrypoint to run on port 80.
Now with the dockerfile is complete, we can use it to build our own docker image and tagged it as LatihanUKSW. Open the terminal, change directory to LatihanUKSW then type
docker build . -t latihanuksw

docker build tags
docker build tags

A successful build means that now we have containerized an core application inside a docker image.

Docker Compose

To simulate a containerization of application development in docker, we will use docker-compose which is a tool for defining and running multi container docker applications. In other words, we put everything inside a container including the database and run all of it at the same time inside docker.
So let’s make a new file called docker-compose.yml in the top directory, and define services and images that we’ll use.

version: '3'
image: latihanuksw:latest
context : ./Latihanuksw
dockerfile: Dockerfile
DB_CONN_STRING: "host=postgres_image;port=5432; database=TrainingUKSW; user id=postgres; password=postgres"
depends_on: ['postgres_image','adminer']
image: postgres:10.5-alpine
POSTGRES_USER: "postgres"
image: adminer
restart: always

The above file is the content of our compose file. Basically we define 3 services: the core app, postgresql database and a web interface to manage the database. Especially for the core app, we use an image that we build earlier. The other two, we use the base image from dockerhub and set its environment variables appropriately. To demonstrate if the compose file work correctly, open the terminal and type :
docker-compose up -d

docker compose up -d
docker compose up -d

if everything works well, you can open the browser and go to localhost:8080 to manage the postgres database, make sure to input the connection string the same as the environment variables that we provide on compose file and make sure to create the tables and views from traininguksw.sql file inside directory Latihanuksw . After that go to localhost:81 to test the core web application.
Congratulation, now we have a working compose file and successfully containerized our core application
adminer manage postgres db

aspnet core app docker
aspnet core app docker

Full source code is here.

Published by Gadael Sedubun


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: