You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求协助:构建适用于.NET应用的健壮Docker Compose配置文件

Convert Your Manual docker run to a Robust Docker Compose Setup

Let’s turn your existing manual Docker command into a clean, maintainable Docker Compose configuration that handles your private registry, restart policies, and application settings. Here’s a step-by-step breakdown:

1. Complete docker-compose.yml Example

First, here’s the full Compose file that mirrors your docker run command, plus best practices for private repos and environment management:

version: '3.8'

services:
  test_rest:
    # Replace with your private registry's full image path (e.g., registry.yourdomain.com/test_rest:latest)
    image: your-private-registry/test_rest
    container_name: test_rest
    restart: unless-stopped
    tty: true
    ports:
      - "9095:80"
    entrypoint: dotnet
    # Pass your application arguments here, or use environment variables (see below)
    command: ["script_var.Core.dll", "test_var:MongoSource=mongodb://192.168.22.22", "KafkaReceiveStream:KafkaBroker=192.168.x.x"]
    # Optional: Use environment variables for cleaner configuration
    # environment:
    #   - TEST_VAR_MONGOSOURCE=mongodb://192.168.22.22
    #   - KAFKA_RECEIVESTREAM_KAFKABROKER=192.168.x.x

2. Key Mapping to Your Original docker run Command

Let’s connect each part of the Compose file to your manual command:

  • restart: unless-stopped: Matches --restart unless-stopped (ensures your container restarts unless explicitly stopped)
  • tty: true: Equivalent to -t (allocates a pseudo-TTY for the container)
  • container_name: test_rest: Replaces --name test_rest (sets a fixed container name)
  • ports: ["9095:80"]: Maps to -p 9095:80 (forwards host port 9095 to container port 80)
  • entrypoint: dotnet: Replaces --entrypoint dotnet (sets the entrypoint executable)
  • command: [...]: Contains the arguments you passed after dotnet in your docker run command

3. Handling Private Docker Registry

Since your images are in a private registry:

  1. First, log in to your registry from the machine where you’ll run Compose:
    docker login your-private-registry.com
    
    Docker will store your credentials locally, so Compose can pull the image automatically.
  2. Ensure your image field uses the full registry path (e.g., your-private-registry.com/test_rest:latest), not just test_rest.

4. Optional Best Practices

  • Use a .env file for environment variables: Instead of hardcoding IPs in the command field, define them in a .env file and reference them in Compose. This makes updates easier and keeps sensitive values out of the Compose file:
    # .env file
    MONGO_SOURCE=mongodb://192.168.22.22
    KAFKA_BROKER=192.168.x.x
    
    Then update the Compose command:
    command: ["script_var.Core.dll", "test_var:MongoSource=${MONGO_SOURCE}", "KafkaReceiveStream:KafkaBroker=${KAFKA_BROKER}"]
    
  • Add health checks: Ensure your container is running correctly by adding a health check (adjust the command to match your app’s health endpoint):
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
    
  • Avoid fixed container names (optional): If you plan to scale the service later, remove container_name—Compose will generate unique names automatically.

How to Use This

  1. Save the docker-compose.yml file (and .env if using) in your project directory.
  2. Run docker-compose up -d to start the container in detached mode (equivalent to -d in docker run).
  3. To stop the container: docker-compose down

内容的提问来源于stack exchange,提问作者Ganesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:49:03