Docker Compose报错:services.sqlserver.build含不支持的'environment'选项
Let's jump straight to the issue here. The error you're seeing happens because you've placed the environment configuration inside the build block of your sqlserver service—but environment is a top-level setting for the service itself, not part of the build context setup.
What's Wrong With Your Current Config
Your docker-compose.yml has the environment keys nested under services.sqlserver.build, which isn't a valid location for runtime environment variables in Docker Compose v3:
version: "3" services: coreapi: build: context: ./theapi dockerfile: Dockerfile ports: - "5000:5000" sqlserver: build: context: ./sqlserver environment: # ❌ This is the incorrect placement ACCEPT_EULA="Y" SA_PASSWORD="Pwd12345!" ports: - "11433:1433"
Corrected docker-compose.yml
Move the environment block out of build and make it a direct child of the sqlserver service. Also, Docker Compose environment variables don't require quotes (though they might work in some cases, omitting them follows the standard YAML syntax):
version: "3" services: coreapi: build: context: ./theapi dockerfile: Dockerfile ports: - "5000:5000" sqlserver: build: context: ./sqlserver environment: # ✅ Correct service-level placement ACCEPT_EULA: Y SA_PASSWORD: Pwd12345! ports: - "11433:1433"
Why This Works
- The
buildblock is only for configuring how your Docker image gets built (like context path, Dockerfile location, or build-time arguments). - The
environmentblock at the service level sets variables that will be available inside the running container—exactly what you need for SQL Server's setup requirements (ACCEPT_EULAandSA_PASSWORD).
Quick Additional Tips
- If you ever need to pass build-time only variables (used during image creation but not in the running container), use
argsinside thebuildblock instead ofenvironment. For example:build: context: ./sqlserver args: BUILD_VERSION: 1.0.0 - Double-check YAML indentation—it's strict! Make sure all service-level keys (
build,environment,ports) are aligned at the same indentation level.
内容的提问来源于stack exchange,提问作者SamuraiJack

