简体   繁体   中英

How to get Docker IP in windows Container?

I unable to get container IP address to run it from Browser.

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

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"

Docker.Compose.yml file settings

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 "

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 "

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 .

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

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; 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.


Change in command withing Docker file solve the issue. 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. 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.

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

Put withing .dockerignore file

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.

Source 1 - building-sample-app

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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