Gre*_*uff 5 c# docker .net-core
版本
设置
我正在使用 Docker.DotNet 包来管理 ASP.NET Core API 项目中功能测试的容器依赖项
我使用了此博客 ( https://danieldonbavand.com/dockerdotnet/ )中的代码来创建一个类来管理我的测试项目中与 Docker API 的交互 - 我已将此代码添加到帖子因为比较长。
问题
dotnet test当我从终端运行命令时,一切都按预期进行。但是,我想在构建Dockerfile.
当我运行时,docker build -t my-project .出现以下错误:
System.Net.Http.HttpRequestException:连接失败 ---- System.Net.Internals.SocketExceptionFactory + ExtendedSocketException:无法分配请求的地址 /var/run/docker.sock 堆栈跟踪:位于 Microsoft.Net.Http.Client.ManagedHandler。 ProcessRequestAsync(HttpRequestMessage请求,CancellationToken CancellationToken)在Microsoft.Net.Http.Client.ManagedHandler.SendAsync(HttpRequestMessage请求,CancellationToken CancellationToken)在System.Net.Http.HttpClient.FinishSendAsyncBuffered(任务
1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at Docker.DotNet.DockerClient.PrivateMakeRequestAsync(TimeSpan timeout, HttpCompletionOption completionOption, HttpMethod method, String path, IQueryString queryString, IDictionary2标头,IRequestContent数据,CancellationToken CancellationToken)在Docker /src/My.FunctionalTests/ 中的.DotNet.DockerClient.PrivateMakeRequestAsync(TimeSpan超时,HttpCompletionOption finishOption,HttpMethod方法,字符串路径,IQueryString queryString,IDictionary2 headers, IRequestContent data, CancellationToken cancellationToken) at Docker.DotNet.DockerClient.MakeRequestAsync(IEnumerable1 errorHandlers,HttpMethod方法,字符串路径,IQueryString queryString,IRequestContent正文,IDictionary 1环境变量)2 headers, TimeSpan timeout, CancellationToken token) at Docker.DotNet.ContainerOperations.CreateContainerAsync(CreateContainerParameters parameters, CancellationToken cancellationToken) at My.FunctionalTests.Helpers.DockerSetup.StartContainerAsync(String image, String tag, String hostPort, String containerPort, IListHelpers/Docker/DockerSetup.cs:位于 My.FunctionalTests.Helpers.DockerSetupExtensions.StartLocalStackAsync(DockerSetup docker,字符串服务)的第 34 行,位于 /src/My.FunctionalTests/Helpers/Docker/DockerSetupExtentions.cs:位于 My.FunctionalTests 的第 12 行。 /src/My.FunctionalTests/MyTests.cs 中的 MyTests.GetAsync_Valid_Success(String url):第 36 行
经过一番谷歌搜索后,这可能是由localhost冲突引起的。我对这个有点不了解,所以想知道
下面是相关代码:
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
# Copy csproj and restore as distinct layers
COPY ./My.sln .
COPY ./My.API/My.API.csproj ./My.API/My.API.csproj
COPY ./My.FunctionalTests/My.FunctionalTests.csproj ./My.FunctionalTests/My.FunctionalTests.csproj
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o /app
# Functional tests
FROM build as functionaltest
WORKDIR /src/My.FunctionalTests
RUN dotnet test
Run Code Online (Sandbox Code Playgroud)
DockerSetup.cs
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
# Copy csproj and restore as distinct layers
COPY ./My.sln .
COPY ./My.API/My.API.csproj ./My.API/My.API.csproj
COPY ./My.FunctionalTests/My.FunctionalTests.csproj ./My.FunctionalTests/My.FunctionalTests.csproj
RUN dotnet restore
# Copy everything else and build
COPY . .
RUN dotnet publish -c Release -o /app
# Functional tests
FROM build as functionaltest
WORKDIR /src/My.FunctionalTests
RUN dotnet test
Run Code Online (Sandbox Code Playgroud)
然后我在测试中使用此代码,如下所示:
我的测试.cs
public class DockerSetup
{
private readonly DockerClient _dockerClient;
public DockerSetup()
{
_dockerClient = new DockerClientConfiguration(new Uri(DockerApiUri())).CreateClient();
}
private static string DockerApiUri()
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
{
return "npipe://./pipe/docker_engine";
}
var isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
var isMacOS = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
if (isLinux || isMacOS)
{
return "unix:/var/run/docker.sock";
}
throw new Exception("Was unable to determine what OS this is running on, does not appear to be Windows, MacOS or Linux!?");
}
public async Task PullImageAsync(string image, string tag)
{
await _dockerClient.Images
.CreateImageAsync(new ImagesCreateParameters
{
FromImage = image,
Tag = tag
},
new AuthConfig(),
new Progress<JSONMessage>());
}
public async Task<string> StartContainerAsync(string image, string tag, string hostPort, string containerPort, IList<string> environmentVariables)
{
var response = await _dockerClient.Containers.CreateContainerAsync(new CreateContainerParameters
{
Image = $"{image}:{tag}",
ExposedPorts = new Dictionary<string, EmptyStruct>
{
{
containerPort, default(EmptyStruct)
}
},
HostConfig = new HostConfig
{
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{containerPort, new List<PortBinding> {new PortBinding {HostPort = hostPort}}}
},
PublishAllPorts = false
},
Env = environmentVariables
});
await _dockerClient.Containers.StartContainerAsync(response.ID, null);
return response.ID;
}
public async Task StopContainerAsync(string containerId)
{
if (containerId != null)
{
await _dockerClient.Containers.KillContainerAsync(containerId, new ContainerKillParameters());
}
}
}
Run Code Online (Sandbox Code Playgroud)
According to your image there is no docker engine during image build, so your docker client fails to access it.
I'm aware of 2 options of providing docker engine inside a container:
通过卷将主机系统 docker 引擎连接套接字(/var/run/docker.sock)挂载到容器内,因此客户端的调用将传递到主机系统 docker 引擎。
在 docker 镜像中使用 docker 在容器内提供独立的 docker 引擎。
前一种方法的缺点是您无法在映像构建过程中挂载任何内容,因此您必须使用挂载和正确的命令来运行预构建的映像来执行测试,如下所示:
docker run \
--volume //var/run/docker.sock:/var/run/docker.sock \
IMAGE_NAME dotnet test
Run Code Online (Sandbox Code Playgroud)
要使用后一种方法(docker in docker),您可以在 dockerfile 中使用官方镜像docker:dind来获取可用的 docker 引擎。
有关在 docker 中运行 docker 的更多信息。
| 归档时间: |
|
| 查看次数: |
508 次 |
| 最近记录: |