Using Docker to run the catalog service

This section explains how to combine our catalog service with Docker to get it running locally. Let's start by examining the systems behind the catalog service:

As we can see from the preceding diagram, the web service part runs over the microsoft/dotnet Docker image, and the data source part runs over the Microsoft SQL Server instance using the microsoft/mssql-server-linux Docker image (we already dealt with the containerization of MSSQL in Chapter 8, Building the Data Access Layer). Both of the images are downloaded from the public Microsoft repository already present in Docker Hub; let's take a look at how to use docker-compose to define the whole infrastructure of the service.

First of all, let's create a docker-compose.yml file with the following content in the root folder of the project:

version: "3.7"
services:
catalog_api:
container_name: catalog_api
build:
context: .
dockerfile: containers/api/Dockerfile
env_file:
- containers/api/api.env
networks:
- my_network
ports:
- 5000:5000
- 5001:5001
depends_on:
- catalog_db

catalog_db:
image: microsoft/mssql-server-linux
container_name: catalog_db
ports:
- 1433:1433
env_file:
- containers/db/db.env
networks:
- my_network

networks:
my_network:
driver: bridge

The preceding file uses the YAML syntax to define two containers. The first is the catalog_api container: it will be used to host the core part of the service built on top of the ASP.NET Core framework. The second container is catalog_db, which uses a microsoft/mssql-server-linux image (the same that we used in Chapter 8Building the Data Access Layer) to set up the MSSQL database.

Furthermore, we should proceed by creating the following folder structure in the root of the project:

mkdir containers
mkdir containers/api
mkdir containers/db

The preceding folders will contain the files related to the API and the database containers specified in the docker-compose.yml file. Let's continue by examining the definition of the catalog_api container:

  catalog_api:
container_name: catalog_api
build:
context: .

dockerfile: containers/api/Dockerfile

The preceding snippet of code specifies the current folder as the build context and the containers/api/Dockerfile file to build the Docker image. It also refers to an environment variables file by using the following syntax:   

...
env_file:
- containers/api/api.env
...

Finally, it declares the container under a network called my_network and exposes port 5000 to the hosting system using the ports: directive. 

In the same way, the catalog_db container declares the same network defined for the catalog_api container, and it specifies a different environment variables file using the same approach as seen previously.

At the end of the docker-compose.yml file, there is the definition of my_network, which uses a bridge driver. The bridge driver is the default option for the network. Two containers under the same bridge network can share traffic.

For more information about the types of drivers provided out of the box, refer to the following link: https://docs.docker.com/network/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.105.215