How to create Docker-Compose & Dockerfile for .Net Core Application

Saurabh Singh
5 min readOct 6, 2018

--

Have you ever wondered what are containers and how we can deploy our applications in them?

In this article, I will assume, you already have basic knowledge of containers, docker and .Net core.

2 months back, I was assigned a task to dockerize/containerize the .Net core web application. The design of deployment should be generic enough to accommodate both development and production/testing environment.

We use docker windows container.

What is dockerfile ? It’s structure and commands. I will cover that in a separate article.

When I started working on this task, got stuck, faced several challenges and I resolved them step by step. But now I know what all challenges one will face.

  • which base image of .netcore to use
  • IP address/port mapping of the server and container
  • how outside world communicate to an application running inside the container.
  • passing an environment variable to .net core application, to let the application know whether it is a development or production environment.
  • the entry point of the application.

I end up with following docker file. In rest of the article I will explain, how to resolved the above hurdles.

Step 1 — Which base image to use

Since my application is in .Net Core 2.1, I used the latest image from docker hub: 2.1.4-aspnetcore-runtime-nanoserver-1709

refer:

https://hub.docker.com/r/microsoft/dotnet/

https://hub.docker.com/r/microsoft/aspnetcore/

Step 2 — Ip address and port mapping

Here the things get a bit tricky. We can configure our container to expose application running inside it to a particular port, generally we run it on default port 80.

If refer below image, 4 docker containers are running on the server and each exposes the running application on port 80. But those applications are not directly accessible to external systems.

So, I opened 4 ports in the server 10001, 10002, 10003 and 10004. Then I mapped each container to those ports.

I can access the application using

http://<server-ip>:10001/10002/10003/10004

But how to do that?

To run an application in docker container, we use docker command something like below:

docker run -it --rm -p 8000:80 --name

Here, port 8000 refer to the server port and 80 refers to the container port. So the application running inside the container is exposed at port 80 and it is mapped to server port 8000.

Practically, we use docker-compose file to pull latest images from docker registry and start the container.

docker-compose.yml file:

It is the sample docker-compose.yml file to manage 2 applications or containers.

Here we defined:

  • the docker image name
  • volume path and mapping— where we save log files: ‘c:/logs:c:/logs’
  • port mapping
  • environment variables to pass to .Net core application — CONSUL_URL and HOST

As per above configuration

  • server’s port 10001(GateKeeper) is mapped with first container’s port 80
  • server’s port 10002(Test Core) is mapped with the second container’s port 80

Step 3— Environment variables

Here you can see 4 variables {GATEKEEPER_PORT}, {HOST_ADDRESS}, {CONSUL_IP}, {TEST_CORE_PORT}. Their values are coming from .env or environment file. It is in the same directory with docker-compose.yml file.

The environment variables defined in docker-compose.yml file are passed to and available in dockerfile. Following two variables are passed from docker-compose to dockerfile.

ENV CONSUL_URL = http://xxxxx:xxx/

ENV HOST = 127.0.0.1:8500

You are free to define other variables also in dockerfile.

EXPOSE 80 — it means the application will be exposed at port 80 of docker container.

Step 4— Entry point

This is the last and most easy step. Here we define the entry point of the application.

docker-compose up -d testcore.web

When we start out an application using the above command, it will execute the command we define in ENTRYPOINT. For .Net Core applications we use the format as defined above in dockerfile.

You can access environment variables in startup.cs class:

Conclusion

With the above setup, we can create separate docker-compose.yml and .env files for different environments.

You can create a batch/.bat file as well to pull and start all applications defined in the docker-compose file.

I hope this article will help you understand the basic flow and structure of docker-compose.yml, .env file, dockerfile and .Net core containerization.

In coming days I will be writing about how to dockerize an application and push to AWS ECR.

Regards

More Readings

--

--