简体   繁体   English

如何在Windows Container中获取Docker IP?

[英]How to get Docker IP in windows Container?

I unable to get container IP address to run it from Browser. 我无法从浏览器获取容器IP地址来运行它。

Code Snippet 代码段

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

Got Bellow ID 得到波纹管ID

sha256:7003460ebe84bdc3e8647d7f26f9038936f032de487e70fb4f1ca137f9dde737 sha256:7003460ebe84bdc3e8647d7f26f9038936f032de487e70fb4f1ca137f9dde737

If I run bellow command 如果我运行风箱命令

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

Got bellow response 吼叫回应

Template parsing error: template: :1:19: executing "" at <.NetworkSettings.Net...>: map has no entry for key "NetworkSettings" 模板解析错误:模板:: 1:19:在<.NetworkSettings.Net ...>处执行“”:映射没有键“ NetworkSettings”的条目

Docker.Compose.yml file settings Docker.Compose.yml文件设置

version: '2.1'

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

networks:
  default:
    external:
      name: nat

By runnging " docker network ls " 通过运行“ docker docker network ls

got bellow response 吼叫

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

When I run " docker network inspect webdockercoreapp_default " 当我运行“ docker network inspect webdockercoreapp_default ”时

Got below response 低于回应

[
    {
        "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"
        }
    }
]

When you're running the command docker inspect --format="{{.Id}}" 7003460ebe84 - you're currently running this against the image ID not the container ID . 当您运行命令docker inspect --format="{{.Id}}" 7003460ebe84您当前正在针对映像ID而不是容器ID运行此命令。

Images are the static asset that you build, and of which containers are run from. 图像是您构建的静态资产,容器是从中运行的。 So what you need to do is first bring your image online, via: 因此,您需要做的是首先通过以下方式使图像在线:

docker-compose up

Now you'll be able to see the running containers via: 现在您可以通过以下方式查看正在运行的容器

docker ps

Find the container you want; 找到您想要的容器; let's say it's abcd1234 假设是abcd1234

Now you'll be able to run your original command against the container - rather than the image. 现在,您将能够对容器而非图像运行原始命令。

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

This will return the full SHA of the container; 这将返回容器的完整SHA。 and since you originally asked about the network settings; 并且由于您最初询问的是网络设置; you'll be able to run something like: 您将可以运行以下内容:

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

If you're unsure exactly what your network name is (Looks like it should be nat ) just do a full docker inspect abcd1234 and look at the output and adjust the filter as needed. 如果不确定确切的网络名称(看起来应该是nat ),只需对整个docker inspect abcd1234并查看输出并根据需要调整过滤器即可。


Change in command withing Docker file solve the issue. 使用Docker文件更改命令可以解决此问题。 Bellow is the code snippet 波纹管是代码片段

  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. 接下来,我们将.csproj文件复制到临时容器的“ / app”目录中。 We do this because .csproj files contain contain a list of package references our project needs. 我们这样做是因为.csproj文件包含一个包含项目需要的程序包引用的列表。 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. 复制此文件后,dotnet将读取该文件,然后出去并获取我们项目所需的所有依赖项和工具。

  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. 然后,我们告诉dotnet使用发布配置发布我们的应用程序,并指定输出路径。

  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. 我们用于此最终容器的基本映像与上面的FROM命令相似,但有所不同-它没有能够构建ASP.NET应用程序的库,只能运行。

Concussion: 脑震荡:

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. 我们使用临时容器来构建映像,然后将已发布的dll移到另一个容器中,以便最大程度地减少最终结果的占用空间。 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. 如果我们继续使用第一个映像,那么它将与其他层(用于构建ASP.NET应用程序)打包在一起,这不是很重要,因此会增加映像大小。

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"]

Put withing .dockerignore file 放入.dockerignore文件

bin\
obj\

Note: Above settings will work if you create a Asp.Net Core Project from Visual Studio IDE with in-build docker support (using docker-compose.yml and docker-compose.ci.build.yml ) files or without it. 注意:如果您从带有docker-compose.yml支持(使用docker-compose.ymldocker-compose.ci.build.yml )文件的Visual Studio IDE创建一个Asp.Net Core项目,则可以使用上述设置,也可以不使用它。

Source 1 - building-sample-app 来源1-building-sample-app

Source 2 - asp-net-core-on-linux with Docker 源码2-带Docker的Linux上的ASP.NET核心

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM