eshop/catalog-api image. For simplicity’s sake, there is no build:
key
setting in the file. This means that the image must have been previously built (with docker build)
or have been downloaded (with the docker pull command) from any Docker registry.
•
It defines an environment variable named ConnectionString with the connection string to be
used by Entity Framework to access the SQL Server instance that contains the catalog data
model. In this case, the same SQL Server container is holding multiple databases. Therefore, you
need less memory in your development machine for Docker. However, you could also deploy
one SQL Server container for each microservice database.
•
The SQL Server name is
sqldata , which is the same name used for the container that is running
the SQL Server instance for Linux. This is convenient; being able to use this name resolution
(internal to the Docker host) will resolve the network address so you don’t need to know the
internal IP for the containers you are accessing from other containers.
Because the connection string is defined by an environment variable, you could set that variable
through a different mechanism and at a different time. For example, you could set a different
connection string when deploying to production in the final hosts, or by doing it from your CI/CD
pipelines in Azure DevOps Services or your preferred DevOps system.
•
It exposes port 80 for internal access to the
catalog-api service within the Docker host. The host
is currently a Linux VM because it is based on a Docker image for Linux, but you could configure
the container to run on a Windows image instead.
•
It forwards the exposed port 80 on the container to port 5101 on the Docker host machine (the
Linux VM).
•
It links the web service to the
sqldata service (the SQL Server instance for Linux database
running in a container). When you specify this dependency, the catalog-api container will not
start until the sqldata container has already started; this aspect is important because catalog-api
needs to have the SQL Server database up and running first. However, this kind of container
dependency is not enough in many cases, because Docker checks only at the container level.
Sometimes the service (in this case SQL Server) might still not be ready, so it is advisable to
implement retry logic with exponential backoff in your client microservices. That way, if a
dependency container is not ready for a short time, the application will still be resilient.
•
It is configured to allow access to external servers: the extra_hosts setting allows you to access
external servers or machines outside of the Docker host (that is, outside the default Linux VM,
which is a development Docker host), such as a local SQL Server instance on your development
PC.
There are also other, more advanced
docker-compose.yml
settings that we’ll discuss in the following
sections.
120
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications