Skip to content

containerization

Walter Pinson edited this page Dec 18, 2017 · 12 revisions

Containerization

The Reference Architecture API uses Docker for containerization and to support a build once run in all environments scenario.

The repository includes three (3) Docker configuration files that, collectively, support various build and deploy scenarios.

File Name Description
./.dockerignore Instructs Docker to ignore files and folders when building images
./Dockerfile Instructions to build docker image
./docker-compose.yml Instructions to build a docker service

There are two approaches to running the Reference Architecture API.

  1. Run the Reference Architecture API as a docker service, with all dependencies hosted inside Docker containers.
  2. Run the Reference Architecture API as a single docker image, with dependencies running on the local machine or other hosted environments.

Run the Reference Architecture API as a Docker Service

Build and Run the Reference Architecture API Service

Running the Reference Architecture as a Docker Service requires Docker Compose and the ./docker-compose.yml configuration file.

Configure the Service

The ./docker-compose.yml configuration file defines one network

  • dbnet

and two services

  • notetaking
  • mongodb

The notetaking service is configured to be available on port 5002 and be dependent on the availability of the mongodb service. The mongodb service makes itself available on on port 27017, for administrative purposes. Each of the two services is configured to use the same network, dbnet. (NOTE: The MongoDb port would likely not be publicly exposed in a production scenario.) The notetaking service and the mongodb service are both configured to communicate with one another via the dbnet network.

Run the Service

Issue the following command, from the repository root, to run the service.

docker-compose up

This should produce output similar to the following:

docker compose

The command builds the following docker images and then runs them as a service.

docker images

When the command has completed, you can confirm that the service is running by issuing the following command:

curl http://{{docker-machine-ip}}:5002/ping

Run the Reference Architecture API as Single Docker Image

Build the API Image

Prior to running a docker container, we must first build a docker image.

This is a simple build. The Dockerfile assumes that the application has already been built and published into the ./publish folder.

FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY ./publish .
ENTRYPOINT ["dotnet", "Infrastructure.WebApi.dll"]

The following command builds the image.

docker build -t walterpinson/reference-architecture-api .

Alternatively, the ./sh/build_image.sh script can be run to achieve the same result.

Run the API Container

The following command can be used to run the docker container. This command assumes certain dependencies are in place and running in the local environment, such as a MongoDB server.

docker run  -itd -p 5001:80 \
-e ASPNETCORE_ENVIRONMENT \
-e NoteTaker__NotificationService__SendGrid__ApiKey \
-e ConnectionStrings__NoteTakingService \
--name refarch_api walterpinson/reference-architecture-api

Alternatively, the ./sh/build_container.sh bash script can be run to achieve the same result.