Building Microservices Through Event-Driven Architecture, Part 5: Dockerization

DZone 's Guide to

Building Microservices Through Event-Driven Architecture, Part 5: Dockerization

Learn how to utilize Dockerization through Web API Core and SQL Server Linux. Build images and run containers.

· Microservices Zone ·
Free Resource


Read on to find out more about Dockerization.

Note: The EventStore is not implemented yet, I will set up the integration tests, configure the CI/CD and I will implement the EventStore later with service bus and NoSQL database.

During this journey, I will talk about the dockerization of web API and SQL server Linux database.

The Web API will be published and live, and it will need a database. I will dockerize the web API and the SQL Server database: LogCorner.EduSync.Speech.Presentation and LogCorner.EduSync.Speech.Database.

This dockerization will produce two images, which will be pushed to a repository of a container registry and used by Azure Kubernetes Service.

You may also like: Building Microservices Through Event-Driven Architecture, Part 4: Repositories

Web API Dockerization

Right, click on LogCorner.EduSync.Speech.Presentation project name, and select ContainerOrchestration Support.

Choose Docker Compose and click OK.

The following Dockerfile will be generated:

Build Images

for this demonstration, I delete all the containers and images, do not run these commands if you don't want to delete all your images.

# Stop all containers
docker stop $(docker ps -a -q)

# Delete all containers
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)

To build the previous Dockerfile, locate CommandInterfaces directory and run the following command: it builds the DockerFile from the current directory as build context and names the resulted images as web API-image.

docker build -t web API-image -f LogCorner.EduSync.Speech\LogCorner.EduSync.Speech.Presentation\Dockerfile.

run the following command to list all images

docker images

docker images -filter "dangling=false"

The following images are created:

  • Microsoft/dotnet:2.2-aspnetcore-runtime (from Dockerfile).
  • Microsoft/dotnet:2.2-SDK (from Dockerfile).
  • Web API-image (from build command).

Run Container

Run the following command: it runs the web API-image image, creates a container webapi-container and maps port 80 of the container to port 8080 outside of the container.

docker run -d -p 8080:80 -name webapi-container web API-image

The following command list all running containers.

We have a running container named webapi-container.

Run the following command to view the web API-container logs.

docker logs webapi-container

Web API is now running and listening on port 80 inside the container and port 8080 outside the container.

So http://localhost:8080/api/speech should hit the API as follows.

Run again docker logs webapi-container.

docker logs webapi-container

The logs say that an error occurred because it cannot connect to the database.

Let us fix it on the next step.

SQL Server Linux Dockerization

From LogCorner.EduSync.Speech.Database project, open project properties, click on the Project Settings tab and then check to Create a script (.sql file).

It will create a script file for creating the database. Whenever the database is updated, this script will be updated.

Click on tab Build Events, create a Post-build event command as following:

xcopy "$(ProjectDir)bin\$(Configuration)\LogCorner.EduSync.Speech.Database_Create.sql" "$(ProjectDir)Docker\Restore" /Y

It will copy the SQL script generated on the previous step to a specific directory (Docker/Restore on my case).

DockerFile will use this script to create the database image.

The SQL server Linux Dockerfile.

Locate the directory where the SQL Server Dockerfile is located and run the following command to create database-image with SA_PASSWORD.

docker build -t database-image. -build-arg SA_PASSWORD='PassW0rd'

List all images.

docker images -filter "dangling=false"

We have two additional images: Microsoft/MySQL-server-Linux and database-image.

Run Container

Run the following command to create a database container (database-container) based on database-image, mapped on port 1433 inside and outside the container.

docker run -d -p 1433:1433 -name database-container database-image

List running containers.

We have a new container based on database-image.

Run the following command to attach a shell on the database-container.

Docker exec -it database-container "bash".

Run the following command to connect to the SQL server instance of the running container.

/opt/mssql-tools/bin/sqlcmd -S localhost -U SAP 'PassW0rd'

Run the following command to list all databases.

Select name from sys.databases — Go.

We can see that the database LogCorner.EduSync.Speech.The database is created via the script on Dockerfile.

Run the following command to select [dbo].[Speech] the table on that database.

se [LogCorner.EduSync.Speech.Database]


select * from [dbo].[Speech]

Compose is a tool for defining and running multi-container Docker applications. To learn more about Compose refer to the following documentation: https://docs.docker.com/compose/overview/.

Open the docker-compose.yml file, it already contains a log corner logcorner.edusync.speech.presentation service. update this service to make it depend on database service: logcorner.edusync.speech.presentation.data.

Add a logcorner.edusync.speech.presentation.data service, use the SQL Dockerfile create earlier and SA_PASSWORD as an argument.

The override file, as its name implies, can contain configuration overrides for existing services or entirely new services: https://docs.docker.com/compose/extends/.

Open the docker-compose.override.yml file, and set ASPNETCORE_ENVIRONMENT = Docker or something else . the goal is to use the appsettings.Docker.json file to set all configuration parameters specific to that environment.

Web API service listens on port 80 inside the container and 8080 outside.

The database service listens on port 1433 inside the container and 1433 outside.

Open appsettings.Docker.json file and add a connectionString to use the SQL Server database.

Data Source=logcorner.edusync.speech.presentation.data (name of the database service).

Initial Catalog=LogCorner.EduSync.Speech.Database.


Build Images

docker-compose build

docker images -filter "dangling=false"

Run Container

Run Container

docker-compose up

Attach Shell

docker exec -it 1997 "bash"

Connect to SQL Server Instance of the Running Container

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'PassW0rd'

Verify That the Database Table Dbo. Speech Is Empty

use [LogCorner.EduSync.Speech.Database]


select * from [dbo].[Speech]

Post a Request

Open postman and post a request.

Verify That the Database Table DBO. Speech Has One Row

use [LogCorner.EduSync.Speech.Database]

select * from [dbo].[Speech]

Source code is available here Dockerization.

In the next step, I will implement integration tests.


Further Reading

Building Microservices Through Event-Driven Architecture, Part 1: Application-Specific Business Rules

Building Microservices Through Event-Driven Architecture, Part 2: Domain Objects and Business Rules

Building Microservices Through Event-Driven Architecture, Part 3: Presenters, Views, and Controllers

microservices ,tutorial ,docker ,dockerization ,web api ,web api dockerization ,api core ,sql ,linux

Published at DZone with permission of Gora LEYE , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}