Docker in Action: Containerizing Your Applications with Docker Compose Now that you have Docker installed, it’s time to see how you can leverage it to containerize your applications. In this article, we’ll explore two examples: one for an Angular application and another for a C# (ASP.NET Core) application using Docker Compose. We’ll also cover how to update your Docker images when you make changes to your application.

Example 1: Dockerizing an Angular Application

  1. Create a Dockerfile for Your Angular App For an Angular application, you typically need to build your project and then serve it. A common approach is to use a multi-stage build. Here’s an example Dockerfile:

# Stage 1: Build the Angular application
FROM node:14 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build --prod

Stage 2: Serve the application using nginx

FROM nginx:alpine COPY --from=builder /app/dist/your-angular-app /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] This Dockerfile uses Node.js to build your Angular project and then serves the production-ready files with Nginx.

  1. Define Your Docker Compose File Next, create a docker-compose.yml file to manage your Angular container. This file can be extended with other services if needed:
version: '3'
services:
  angular-app:
    build: .
    ports:
      - "80:80"
    container_name: angular_container

This simple configuration builds the Docker image using your Dockerfile and maps port 80 of the container to port 80 on your host machine.

Example 2: Dockerizing a C# (ASP.NET Core) Application

  1. Create a Dockerfile for Your C# App For a C# application (such as an ASP.NET Core web app), you can use the official .NET SDK image for building and a runtime image for running your application. Here’s an example Dockerfile:
# Stage 1: Build the application
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["YourApp.csproj", "./"]
RUN dotnet restore "YourApp.csproj"
COPY . .
RUN dotnet publish "YourApp.csproj" -c Release -o /app/publish

# Stage 2: Run the application
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY --from=build /app/publish .
EXPOSE 80
ENTRYPOINT ["dotnet", "YourApp.dll"]

This Dockerfile restores dependencies, builds your project, publishes it, and then runs it using the ASP.NET Core runtime image.

  1. Define Your Docker Compose File Similarly, you can define a docker-compose.yml file for your C# application:
version: '3'
services:
  csharp-app:
    build: .
    ports:
      - "80:80"
    container_name: csharp_container

This configuration builds the image for your ASP.NET Core app and maps port 80 of the container to port 80 on your host.

Updating Your Docker Containers After Application Changes When you make changes to your application, updating your Docker containers is straightforward. Here are the steps you can follow:

Modify Your Application Code: Make any necessary changes in your Angular or C# project.

Rebuild the Docker Image: Navigate to your project directory and rebuild your Docker image using the docker-compose build command:

docker-compose build

This command rebuilds the image based on your updated code.

Restart the Containers: After the image has been rebuilt, restart your containers to use the updated image:

docker-compose up -d

The -d flag runs the containers in detached mode.

Verify the Update: Once your containers are running, verify that your changes have been applied by accessing your application through its mapped ports.

By following these steps, you ensure that your changes are reflected in your Dockerized environment with minimal downtime.

Conclusion Docker Compose provides a streamlined way to manage and orchestrate multiple containerized applications. Whether you’re working with an Angular front-end or a C# back-end, using Docker Compose simplifies the build, deployment, and update processes. With the examples provided, you can extend your own Docker setups to suit your project’s needs and maintain a seamless development workflow.

Happy containerizing!