gpt4 book ai didi

sql-server - Docker 上的 Asp.Net Core + SQL Server - sleep 启动数据库

转载 作者:行者123 更新时间:2023-12-03 00:54:44 26 4
gpt4 key购买 nike

我注意到,当我第一次尝试运行 docker-compose up 命令时,出现错误:

Starting mssql ...
Starting mssql ... done
Recreating api ...
Recreating api ... done
Attaching to mssql, api
api exited with code 1

因为api尝试从DB获取数据,但MSSQL尚未启动。

那么,我的问题是否有可能以某种方式等待数据库,然后运行 ​​API?

这是我的 docker-compose 和 dockerfile

docker-compose:版本:'3.3'

服务:

  api:
image: api
container_name: api
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:80"
depends_on:
- db
db:
image: "microsoft/mssql-server-linux"
container_name: mssql
environment:
SA_PASSWORD: "testtest3030!"
ACCEPT_EULA: "Y"
MSSQL_PID: "Express"
ports:
- "8001:1433"

docker 文件:

# Build Stage
FROM microsoft/aspnetcore-build as build-env
WORKDIR /source
COPY . .
RUN dotnet restore
RUN dotnet publish -o /publish --configuration Release

# Publish Stage
FROM microsoft/aspnetcore
WORKDIR /app
COPY --from=build-env /publish .
ENTRYPOINT ["dotnet", "Api.dll"]

我还在日志中注意到:

2017-11-17 22:12:42.67 Logon       Error: 18456, Severity: 14, State: 38.
2017-11-17 22:12:42.67 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'MyDb'. [CLIENT: 172.26.0.3]

最佳答案

您可以使用一个简单的entrypoint.sh脚本:

#!/bin/bash

set -e

run_cmd="dotnet your_app.dll"

sleep 10

exec $run_cmd

docker 文件也会相应改变:

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-bionic AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

COPY ["src/entrypoint.sh", ""]
RUN chmod +x entrypoint.sh

# .... here your copy/restore/build/publish

ENTRYPOINT [ "/bin/bash", "entrypoint.sh" ]

关于sql-server - Docker 上的 Asp.Net Core + SQL Server - sleep 启动数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47359796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com