Up and Running Dot Net core in Docker

Hello !!! lets just start quickly

It will not take much time to get up and running Microsoft Dot Net core on Docker.

Step-0: Do you know about docker Already ?

If already you know docker, just go to step-1 and skip this step, else go through below article, which i have written to get started with docker and get your basic idea on docker.

ABCD of Docker

 

Step-1: Learn something about Microsoft Dot Net Core on Docker Hub

Give your 5 minutes of time to learn and understanding Dot Net core Hosted on Docker hub

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

Step-2: Create docker file to run your first application on Dot Net core on Docker

  • Create a file without any extension called “Dockerfile” (Same if you are windows/Linux or Mac)
  • Specify the dotnet image on docker hub
  • Execute dot net core restore and build command
  • Expose application on a port (5000)
  • Declare the Entry point of the application (Usually this is the last line before you run application)
  • run application
FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app

RUN dotnet restore
RUN dotnet build

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS https://*:5000
ENTRYPOINT dotnet run

Now add above content in “Dockerfile” and save it

Step-3: Build and Run Docker Container

Build the docker image, as so far we have created only the docker file but we have not build it yet.

BUILD:

Open terminal and stay in the directory where you have created the Dockerfile and run below command to build the image. Usually it will read the docker file and build the image with a name given by you. For this example i have given name as “dotnetcore-debugmycode“. I will use this image name in all my example commands in this article.

docker build -t dotnetcore-debugmycode .

RUN:

For running docker container we have 2 options

  • Run it interactively
  • Run it Silently – in the background

Run Interactively. We need “it” parameter passed in docker Run command. This technique is used during docker development time.

docker run -it -p 5000:5000

Run silently, We need “d” parameter passed in docker Run command. This command is used during production server deployment.

docker run -d -p 5000:5000

Step-4: See it on your browser

As docker container in exposed on port 5000, we should access the application on port 5000.

Check docker IP and open your browser and type IP with port. Here you might face some issue. First you need to know the IP address that is taken by docker for your setup. to do so, you need to fire following command

docker-ip dotnetcore-debugmycode

There is also another way to get the IP address of a container to do so we can fire modern docker client syntax

docker inspect --format '{{ .NetworkSettings.IPAddress }}' dotnetcore-debugmycode

Step-5: Check Docker Container running status

See list of running docker containers

docker ps

Happy Docker :)