Search code examples
dockerdocker-swarm

Can I connect directly to a docker swarm network?


I want a shell inside a Docker Service / Swarm network. Specifically, I want to be able to connect to a database that's inside the network.

From the manager node, I tried:

# docker network ls
NETWORK ID          NAME          DRIVER              SCOPE
481c20b4039a        bridge        bridge              local
2fhe9rtim9mz        my-network    overlay             swarm

Then

docker run -it --network my-network alpine sh

But I get the error:

docker: Error response from daemon: swarm-scoped network (event-data-core-prod) is not compatible with docker create or docker run. This network can only be used by a docker service.

Is it possible to somehow start an interactive session that can connect to a network service?


Solution

  • Since Docker Engine v1.13 (like already mentioned by johnharris85) it is possible for non-service container to attach to a swarm-mode overlay networks using the --attachable commandline parameter when creating the network:

    docker network create --driver overlay --attachable my-attachable-overlay-network
    

    Regarding your followup question:

    Is there a way to change this for an extant network?

    Yes and no, like I already described in another question you can make use of the docker service update feature:

    To update an already running docker service:

    1. Create an attachable overlay network:

      docker network create --driver overlay --attachable my-attachable-overlay-network
      
    2. Remove the network stack with a disabled "attachable" overlay network (in this example called: my-non-attachable-overlay-network):

      docker service update --network-rm my-non-attachable-overlay-network myservice
      
    3. Add the network stack with an enabled "attachable" overlay network:

      docker service update --network-add my-attachable-overlay-network myservice