Docker Compose 101: use it on Simple .Net Core API

Docker Compose is a tool that you will use to define and share multi-container applications. This means you can run a project with multiple containers using a single source, you will need to create a YAML file to define the services you need and run with a single command.

docker-compose.yml
services:
  simplecore.api:
    container_name: simplecoreapi.from.docker.composed
    #image: bindplus.api.v2                # will use image
    image: ${DOCKER_REGISTRY-}simplecoreapi:1.0.0    # will use Dockerfile
    build:
      context: . # ASP-NET-Core-Simple-API replace with the actual name of your project, docker file location
      dockerfile: ASP-NET-Core-Simple-API/Dockerfile
    ports:
      - 49195:80    #[targer port]:[docker file port]


The docker-compose.override.yml is the configuration file where you can override existing settings from the docker-compose.yml or can even add completely new services.

docker-compose.override.yml
services:
  simplecore.api:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - LogJson__Path=Payload 
    volumes:
      - /tmp/Payload:/app/Payload         # [Local path]:[container path]     

Run docker compose using the command: docker compose up -d //daemon

$ docker compose up -d
[+] Running 1/2
Network projects_default                      Created                                                                                                                                                 0.5s 
Container simplecoreapi.from.docker.composed  Started

The figure below shows we tested the api if it is running after running docker compose and shutting it down using the command: docker compose down

$ curl http://localhost:49195/api/values
{"machineName":"6ac7227899e2","environment":"Development","logJsonPath":"Payload"}
$ docker compose down
[+] Running 2/2
Container simplecoreapi.from.docker.composed  Removed                                                                                                                        0.2s 
Network projects_default                      Removed 

Leave a Reply

Your email address will not be published. Required fields are marked *