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.
Join the DZone community and get the full member experience.
Join For Free
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]
go
select * from [dbo].[speech]
go
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.
user =sa;password=passw0rd
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]
go
select * from [dbo].[speech]
go
post a request
open postman and post a request.
verify that the database table dbo. speech has one row
use [logcorner.edusync.speech.database]
go
select * from [dbo].[speech]
go
source code is available here dockerization .
in the next step, i will implement integration tests.
regards!
further reading
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
Published at DZone with permission of Gora LEYE, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
A Comprehensive Guide To Testing and Debugging AWS Lambda Functions
-
Personalized Code Searches Using OpenGrok
-
How To Use Geo-Partitioning to Comply With Data Regulations and Deliver Low Latency Globally
-
Manifold vs. Lombok: Enhancing Java With Property Support
Comments