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?

1

There are 1 answers

0
Arash On

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**
        }
    ]
}