Skip to content

Instructions 02 Docker

Christian Nagel edited this page Sep 28, 2023 · 3 revisions

Part 2 - Docker

The game and a bot service will run locally using Docker Desktop.

This requires installation of Docker Desktop.

Dockerfile

With the Games Service, create a Dockerfile.

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["Codebreaker.GameAPIs.csproj", "."]
RUN dotnet restore "./Codebreaker.GameAPIs.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Codebreaker.GameAPIs.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
RUN dotnet publish "Codebreaker.GameAPIs.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Codebreaker.GameAPIs.dll"]

Docker Image

Build a docker image for the games service

docker build . -t codebreaker/gameapis:3.6.1 -t codebreaker/gameapis:basta

The Bot service doesn't have a Dockerfile, but here we'll use dotnet publish. Update the project file with these changes:

<PropertyGroup>   
  <ContainerRepository>codebreaker/bot</ContainerRepository>
  <ContainerImageTags>3.6.1;basta</ContainerImageTags>
</PropertyGroup>

<ItemGroup>
  <ContainerPort Include="8080" Type="tcp" />    
</ItemGroup>
dotnet publish Codebreaker.Bot.csproj --os linux --arch x64 /t:PublishContainer -c Release

Run the services

Start the games API:

docker run -p 8080:8080 -d codebreaker/gameapis:basta

Start the bot service, and pass the configuration to the URL of the games API:

docker run -p 8088:8080 -d -e ApiBase=http://host.docker.internal:8080 codebreaker/bot:basta

Start games by using the Swagger UI from the Bot, and monitor log output from the services. Use the option -f to follow the logs.

docker container logs <container-name>

Stop the services using docker stop <container-name>

Docker Compose

Create a Docker Compose file to start both services.

services:
  codebreaker.gameapis:
    image: codebreaker/gameapis:basta
    container_name: codebreaker.gameapis
    hostname: gameapis
    ports:
      - "8080:8080"
  codebreaker.bot:
    image: codebreaker/bot:latest
    container_name: codebreaker.bot
    hostname: bot
    depends_on:
      - codebreaker.gameapis
    ports:
      - "8088:8080"     
    environment:
      APIBase: "http://gameapis:8080"

Start the services, invoke the swagger link to the bot service, start multiple games, and monitor the console output from the services.

docker compose up -d

Open the Swagger page from the bot service, start games and monitor the console output from the services.

Clone this wiki locally