Search code examples
gitdockerjenkinsdocker-swarmportainer

How to clone docker service into a new one (docker swarm)


I'm using Docker Swarm and Portainer.

To test services with different images I need to clone a docker swarm service, all the hosts, mounts, labels, secrets etc must be in the new service too. This is a hard thing to do by hand, because some services could have many environment vars and Portainer don't help me so much with this.

I need to clone my whole service website_develop and run it in a different Traefik host and docker image.


Storytelling about

My docker swarm services are based on repo's branch.

E.g: website repo have the following services: website_master, website_develop.

I must be able to clone the website_develop service and execute it with a feature/ branch.

Developer A (aka D.A) is working on feature/a in the website project.

Developer B (aka D.B) is working on feature/b in the website project.

D.A just merged his feature to stagging environment (website_develop) because it's done.

D.B wants to test his feature in the stagging environment. But he can't merge to website_develop because it would be merged to master without being done and cause errors (D.B will merge develop to master soon).

GDSC will clone the service website_develop with a new name based on the branch's name with the new docker image URL and a new Traefik host.

D.B publishes his feature/a to the repository (git flow feature publish).

Git repository (gitlab, github) will trigger Jenkins with the update.

Jenkins will build the Dockerfile and then create a new service or update a created service.


Solution

  • GDSC (Go Docker Service Clone)

    I created a project to do exactly this.

    https://github.com/herlon214/gdsc

    You can simply execute:

    gdsc --from original_service_name --name new_service_name --image registry.gitlab.com/my/new-image --domain mycompany.org
    

    Download the latest binary release here: https://github.com/herlon214/gdsc/releases