简体   繁体   中英

Docker API V1.40 how to Create Service And connect it to the swarm overlay network for communication with other stack points

Scenario is:

I already have a docker stack in a swarm, the overlay network name is xxx_default (xxx is the name of stack), I want to create a new service and let it to communicate with other services which are in stack. from the command line this task is pretty simple:

docker service create --name test_web --network xxx_default nginx:alpine

the problem is I want to create this service through DOCKER API ENGINE which is enabled on the swarm manager node. in the API reference website there is nothing about how to attach service to the network.

who knows how can I do this by the API?

OK, as Docker API reference website crashed and do not allow you to open the collapsed properties, you should download the API Json Swagger file and watch it on your system, I found my answer through this way:

{
  "Name": "web",
  "TaskTemplate": {
    "ContainerSpec": {
      "Image": "nginx:alpine"
    },
    "Placement": {},
    "Resources": {
      "Limits": {
        "MemoryBytes": 104857600
      },
      "Reservations": {}
    }
  },
  "Mode": {
    "Replicated": {
      "Replicas": 1
    }
  },
  "EndpointSpec": {
    "Ports": [
      {
        "Protocol": "tcp",
        "PublishedPort": 8080,
        "TargetPort": 80
      }
    ]
  },
      "Networks": [
        {
            "Target": "xphic8cb0trx" **<-- it can be name or id of a network**
        }
    ]
}

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