docker dotnet deployment

HOW TO EASILY DEPLOY DOTNET APPLICATION WITH DOCKER IMAGE IN 4 STEPS

iNTRODUCTION

According to docker’s official website, Docker is a platform designed to help developers build, share, and run modern applications. A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. In this walk through, we will deploy our dotnet API application using container technology.

With docker, compiling and deploying applications is much seemless, as docker erase the problem of it works on my local. This is made possible with the help of the Dockerfile which is created at the root of the project directory. The dockerfile contains all the configurations, dependecies and commands that are needed for the application to successfully build a docker image, and run the docker container.

We will build our dotnet API application using a docker technology in this tutorial. This is a little offshoot of the post where we deployed the dotnet API application on Ubuntu EC2 server with Nginx web server.

deploy dotnet with docker engine

To start, create a Dockerfile at the root of your project directory.

sudo nano Dockerfile

Paste the docker image configuration below inside the editor, and use Ctrl X, Y, Enter to save the file. The configuration is a multi-build docker configuration. This means, the first stage will build the docker image, and the second part will serve only the published directory of the build stage.

# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

ENV ASPNETCORE_URLS=http://+:6996
ENV ASPNETCORE_ENVIRONMENT=”production”
EXPOSE 80

#CMD [“dotnet restore" "dotnet run”]

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "ProjectName.dll"]
Create the docker file.

Run docker build command. Make sure to add the name of your docker image ( here I named my image api), and then specify the location of the Dockerfile. In this case, the file is in the same location as the source code, so I use the period sign (.). The -t flag will make sure the image is tagged with the name we provided, instead of some random strings.

docker build -t api .
build docker image with docker file

Thereafter, run the docker image to start the application.

docker run --name api -d -p 6770:80 --restart always api

Where:
–name will specify the name for your container, in this case api.
-d will run the container in detach mode i.e in the background
-p is to specify the port that the container will run on. The format is hostPort:dockerPort
–restart will restart the docker automatically in case of abrupt shut-down.

run the docker image with the docker run command
conclusion

And that is all, you have deploy dotnet application with docker container, you can map the docker container to an Nginx proxy web server. You will find the steps in the previous post about how to deploy dotnet API application on Ubuntu server.

We have other tutorials for you on oxla.io. Do well to check them out. Also, share this tutorials to your circle who need to develop their cloud skill.

troubleshooting
  1. ERROR: buildx: failed to read current commit information with git rev-parse –is-inside-work-tree
    SOLUTION: Run git status, it should give you the response to add the directory to git safe directory. Run the command provided.
  2. ERROR: You intended to execute a .NET application:
    The application ‘-v’ does not exist.

    SOLUTION: Ensure the version specified in your dockerfile is the same version that your dotnet uses.
git config --global --add safe.directory /home/dareomotosho/[work-directory]

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *