tl;dr: Technically it is possible, but would require to replicate steps performed by CLI when services and other moving parts are created.
Docker Stack is similar to Docker Compose and they both are high-level commands that use manifest file to abstract (or automate) some of the actions needed to start containers and services. This means they require multiple API calls to do their job and I assume this is why they both don’t have respective endpoints in Docker Engine API.
If we look at docker stack deploy command implementation, it performs following steps:
- validates external networks existence;
- creates stack networks;
- creates stack secrets;
- creates stack configs, and;
- deploys services.
It also generates and injects labels to allow itself to find created stack parts in future, e.g. to destroy the stack.
I haven’t dug deeper; maybe there are other “hidden” actions.
All in all, it should be possible to perform all the necessary steps via Docker Engine API and make Docker see the stack.
To verify the theory, I’ve ran following command to create a service with Docker Stack internal labels and Docker Stack style name:
docker service create \
--label com.docker.stack.image="traefik/whoami" \
--label com.docker.stack.namespace="whoami"
--mode replicated \
--name whoami_whoami \
--replicas=1 \
traefik/whoami
Executing docker stack ls then prints the following:
NAME SERVICES ORCHESTRATOR
whoami 1 Swarm
Seems to do the job.
I assume other resources (networks, secrets, configs) must be labeled too, but it still looks doable if needed.