ASP.NET Core 2.2 REST API #14 — Running in Docker

Theodoros Ntakouris
2 min readJul 23, 2019

--

Up Next: Integration Testing

Using a docker file and docker compose. This is not a docker tutorial.

Just create a Dockerfile in the root project directory.

If you select to create a new project with docker support this is pretty much what is going to be generated from visual studio (.Net Core 2.0+)

The Dockerfile

  • Start by using Microsoft’ s .net core base image FROM microsoft/dotnet:2.2-sdk as build from dockerhub
  • ARG BUILDCONFIG=RELEASE to use the release (not debug) .net core version
  • ARG VERSION=1.0.0 for semantic versioning of the image
  • COPY Tweetbook.csproj /build/
  • RUN dotnet restore ./build/Tweetbook.csproj to download/restore all the dependencies of the project
  • COPY . ./build copy all the project into the build directory
  • WORKDIR /build/
  • RUN dotnet publish ./Tweetbook.csproj -c $BUILDCONFIG -o out /p:Version=$VERSION to build it into the /out directory
  • Use the FROM microsoft/dotnet:2.2-aspnetcore-runtime in order to use the production ready .net core runtime
  • WORKDIR /app
  • COPY --from=build /build/out .
  • ENTRYPOINT ["dotnet", "Tweetbook.dll"]

That’s all for the app, but we still need to instantiate some database container. It’s a very bad practise to use a single docker file to mix both the application and the database or other services in the same Dockerfile . That’s a job for

Docker compose

Go into the Solution level and create a new docker-compose.yml .

Let’s use version: '3.5' and let’s get started:

  • Create a network for development purposes
  • Register our Tweetbook api that depends on the database server
  • Register our database mssql server along with the credentials

Change the database connection string to ”DefaultConnection”: “Server=(localdb)\\mssqllocaldb;Database=Tweetbook;Trusted_Connection=True;MultipleActiveResultSets=true”

If we go to the docker-compose.yml ‘s directory and run docker-compose build and docker-compose up we are going to see everything spinning up smoothly.

Everything is up and running at localhost:7000 .

Up Next: Integration Testing

Code is available on Github and the instructional videos are located on YouTube.

Keep Coding

--

--

No responses yet