简体   繁体   中英

Communicating with Docker Service through Published Port

I'm trying to run multiple instances of a Docker image on a single node and send requests to the node allowing Docker to load balance between the instances.

If I use the Run command shown below the container behaves as expected, I can send a request from another machine on port 80 and the request is serviced by container. However, if I try to spin up a service with the Service command shown below I do get 5 replicated tasks running but the request only returns a 404 error.

How can I communicate with the service through my exposed port?

This sample includes a ASP.Net Core 2.0 api that returns a Guid unique to the instance of the app.

Controller

using Microsoft.AspNetCore.Mvc;
using System;

namespace MinimalDockerTest.Controllers {
    [Route("api/[controller]")]
    public class NodeController : Controller {

        [HttpGet]
        public IActionResult Get() {
            return Ok(NodeId);
        }

        private static Guid NodeId {
            get;
        } = Guid.NewGuid();
    }
}

Dockerfile

#Context is binary output folder, i.e. bin/publishoutput
FROM microsoft/aspnetcore:2.0-nanoserver-sac2016
EXPOSE 80
WORKDIR /app
Copy . .
ENTRYPOINT ["dotnet", "MinimalDockerTest.dll"]

Build Command

docker build -t minimaltest .

Run Command

docker run -p 80:80 --name minimaltest minimaltest

Service Command

docker service create -p 80:80 --replicas 5 --name minimaltest minimaltest

Request

GET: http://node_ip/api/node

System

  • Windows 10 1703 Build 15063.0
  • Docker CE 17.12.0-ce-win46 (15048)

Edit

Found some good info here on SO.

I beleive you need to publish port in "host" mode (docs.microsoft.com/en-us/virtualization/windowscontainers/…‌​). Also it will be one to one port mapping between running container and host and hence you will not be able to run several containers on the same port. Routing mesh is not working on Windows yet.

Source

Now the question remains; When will mesh routing be supported?

Make sure you aren't pulling a different image from the registry. By default, docker service will resolve the image name from the registry so that it will work on multiple nodes in the cluster. While docker run will use the image as it exists on the local node. You can disable this resolution process with the --no-resolve-image option:

docker service create -p 80:80 --replicas 5 --name minimaltest \
  --no-resolve-image minimaltest 

Apparently mesh routing is not available for Windows 10 Version 1703. Mesh Routing was made the default option on Windows with version 1709.

Box Boat

MS Blog

SO

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