Docker Configuration in Development Environment for stevedunn/pacmanblazor

Prerequisites

Before configuring Docker for the development environment of stevedunn/pacmanblazor, ensure that you have the following installed:

  • Docker Desktop
  • .NET SDK
  • Node.js
  • Blazor SPA

Directory Structure

The typical directory structure for your project should resemble the following:

/pacmanblazor
  ├── /Client
  ├── /Server
  ├── /Shared
  ├── Dockerfile
  └── docker-compose.yml

Dockerfile

A Dockerfile outlines how to build the different services in your application. For pacmanblazor, you may have separate Dockerfiles for the Client and Server, usually located within their respective directories.

Example Dockerfile for the Client: /Client/Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Client/Client.csproj", "Client/"]
RUN dotnet restore "Client/Client.csproj"
COPY . .
WORKDIR "/src/Client"
RUN dotnet build "Client.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Client.csproj" -c Release -o /app/publish

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

Example Dockerfile for the Server: /Server/Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Server/Server.csproj", "Server/"]
RUN dotnet restore "Server/Server.csproj"
COPY . .
WORKDIR "/src/Server"
RUN dotnet build "Server.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Server.csproj" -c Release -o /app/publish

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

Docker Compose

The docker-compose.yml file simplifies the process of managing multiple Docker containers. The following configuration composes the client and server services.

Example docker-compose.yml

version: '3.4'

services:
  client:
    image: pacmanblazor_client
    build:
      context: .
      dockerfile: Client/Dockerfile
    ports:
      - "5000:80"

  server:
    image: pacmanblazor_server
    build:
      context: .
      dockerfile: Server/Dockerfile
    ports:
      - "5001:80"

Building and Running the Docker Containers

To build and run the application using Docker Compose, navigate to the root of your project directory and execute the following commands:

docker-compose build
docker-compose up

These commands compile both the client and server, and start the services. The application will be accessible at:

  • Client: http://localhost:5000
  • Server: http://localhost:5001

Development Considerations

  1. Live Reloading: It’s beneficial to set up your Docker containers to support live reloading during development. This can be configured within your Dockerfile by copying the source files into the container during the build:
COPY . .
  1. Volume Mounts: Instead of building the image after every code change, use Docker volume mounts for development. Modify the docker-compose.yml to include the volumes:
  client:
    ...
    volumes:
      - ./Client:/app
  server:
    ...
    volumes:
      - ./Server:/app

This configuration will allow any changes made in the Client or Server directories to reflect immediately without needing to rebuild the entire image.

Conclusion

This guide provides an overview of the configuration of Docker for the development environment of stevedunn/pacmanblazor. Following the outlined steps, developers can efficiently work within the containerized environment while utilizing Docker’s full potential.

Source: Information derived from the existing project configuration and best practices related to Docker and Blazor development environments.