如何在 windows 容器中获取 Docker IP?

How to get Docker IP in windows Container?

我无法从浏览器中获取 运行 它的容器 IP 地址。

代码段

PS H:\DevAreaLocal\COMPANY - RAD PROJECTS\DockerApp\WebDockerCoreApp> docker-compose build

Building webdockercoreapp
Step 1/5 : FROM microsoft/aspnetcore:1.1
 ---> 4fe9b4d0d093
Step 2/5 : WORKDIR /WebDockerCoreApp
 ---> Using cache
 ---> b1536c639a21
Step 3/5 : COPY . ./
 ---> Using cache
 ---> 631ca2773407
Step 4/5 : EXPOSE 80
 ---> Using cache
 ---> 94a50bb10fbe
Step 5/5 : ENTRYPOINT dotnet WebDockerCoreApp
 ---> Using cache
 ---> 7003460ebe84
Successfully built 7003460ebe84
Successfully tagged webdockercoreapp:latest

PS H:\DevAreaLocal\COMPANY - RAD PROJECTS\DockerApp\WebDockerCoreApp> docker inspect --format="{{.Id}}" 7003460ebe84

获得波纹管 ID

sha256:7003460ebe84bdc3e8647d7f26f9038936f032de487e70fb4f1ca137f9dde737

如果我运行命令

docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" 7003460ebe84

收到以下回复

模板解析错误:template: :1:19: executing "" at <.NetworkSettings.Net...>: map has no entry for key "NetworkSettings"

Docker.Compose.yml 文件设置

version: '2.1'

services:
  webdockercoreapp:
    image: webdockercoreapp
    build:
      context: ./WebDockerCoreApp
      dockerfile: Dockerfile
    ports:
      - "5000:80"

networks:
  default:
    external:
      name: nat

通过 运行 计算“docker network ls

收到以下回复

NETWORK ID          NAME                       DRIVER              SCOPE
f04966f0394c        nat                        nat                 local
3bcb5f906e01        none                       null                local
680d4b4e1a0d        webdockercoreapp_default   nat                 local

当我运行“docker network inspect webdockercoreapp_default

低于响应

[
    {
        "Name": "webdockercoreapp_default",
        "Id": "680d4b4e1a0de228329986f217735e5eb35e9925fd04321569f9c9e78508ab88",
        "Created": "2017-12-09T22:59:55.1558081+05:30",
        "Scope": "local",
        "Driver": "nat",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "windows",
            "Options": null,
            "Config": [
                {
                    "Subnet": "0.0.0.0/0",
                    "Gateway": "0.0.0.0"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {
            "com.docker.network.windowsshim.hnsid": "ad817a46-e7ff-4fc7-9bb9-d6cf17820b8a"
        },
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "webdockercoreapp"
        }
    }
]

当您运行执行命令docker inspect --format="{{.Id}}" 7003460ebe84时-您目前运行针对图像ID执行此命令,而不是容器 ID.

图像是您构建的静态资产,运行 来自其中的容器。所以你需要做的是首先将你的 image 联机,通过:

docker-compose up

现在您将能够通过以下方式查看 运行ning 容器

docker ps

找到你想要的容器;假设它是 abcd1234

现在您将能够运行针对容器的原始命令 - 而不是图像。

docker inspect --format="{{.Id}}" abcd1234

这将 return 容器的完整 SHA;并且由于您最初询问的是网络设置;你将能够 运行 类似:

docker inspect -f "{{ .NetworkSettings.Networks.your_network_here.IPAddress }}" abcd1234

如果您不确定您的网络名称到底是什么(看起来应该是 nat),只需执行完整的 docker inspect abcd1234 并查看输出并根据需要调整过滤器。


使用 Docker 文件更改命令可解决问题。下面是代码片段

  1. Since we must build our project, this first container we create is a temporary container which we will use to do just that, and then discard it at the end.

  2. Next, we copy over the .csproj files into our temporary container's '/app' directory. We do this because .csproj files contain contain a list of package references our project needs. After copying this file, dotnet will read from it and then to go out and fetch all of the dependencies and tools which our project needs.

  3. Once we've pulled down all of those dependencies, we copy it into the temporary container. We then tell dotnet to publish our application with a release configuration and specify the output path.

  4. We should have successfully compiled our project. Now we need to build our finalized container.

  5. Our base image for this final container is similar but different to the FROM command above--it doesn't have the libraries capable of building an ASP.NET app, only running.

脑震荡:

We have now successfully performed what is called a multi-stage build. We used the temporary container to build our image and then moved over the published dll into another container so that we minimized the footprint of the end result. We want this container to have the absolute minimum required dependencies to run; if we had kept with using our first image, then it would have come packaged with other layers (for building ASP.NET apps) which were not vital and therefore would increase our image size.

FROM microsoft/aspnetcore-build:1.1 AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/aspnetcore:1.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "CoreApiDockerSupport.dll"]

放入 .dockerignore 文件

bin\
obj\

注意: 如果您从 Visual Studio IDE 中创建一个 Asp.Net 核心项目并使用内置 [=39],以上设置将起作用=] 支持(使用 docker-compose.ymldocker-compose.ci.build.yml)文件或不支持文件。

Source 1 - building-sample-app

Source 2 - asp-net-core-on-linux with Docker