Docker SWARM

How to deploy to Docker Swarm with Codefresh

Codefresh can easily deploy your application to Docker Swarm using either GUI or YAML pipelines.

You will need to provide

  1. The docker-stack.yml that contains the definition of the application
  2. The host where your Docker Swarm is running
  3. An SSH key that Codefresh can use to access remotely the Docker Swarm host
  4. The stack name that will be used once the application is deployed.

All this information will be passed to the pipeline in the form of build parameters.

Example application

An example Docker Swarm application can be found at

To launch it locally you need to download Docker. If you are on Mac or Windows, Docker Compose will be automatically installed. On Linux, make sure you have the latest version of Compose.

Run in this root directory:

docker-compose up

The app will be running at http://localhost:5000, and the results will be at http://localhost:5001.

Alternately, if you want to run it on a Docker Swarm, first make sure you have a swarm. If you don’t, run:

docker swarm init

Once you have your swarm, in this directory run:

docker stack deploy --compose-file docker-stack.yml vote

The swarm master must have Python installed.

Deploy to Remote Swarm with Codefresh

First you need to setup the following environment variables in your Codefresh pipeline

RDOCKER_HOST remote Docker swarm master machine, accessible over SSH (for example, ubuntu@ec2-public-ip)
STACK_NAME is new Docker stack name (use "vote", for example)
SSH_KEY private SSH key, used to access Docker swarm master machine
SPLIT_CHAR split character, you’ve used to replace newline in SSH key. Recommendation: use , (comma character).

The SSH_KEY variable has the contents of the SSH key that can access the Docker swarm host. Currently in order to pass SSH key through Codefresh UI, you need to convert it to single line string (replacing newline with comma), like this:

SSH_KEY=$(cat ~/.ssh/my_ssh_key_file | tr '\n' ',')

The SPLIT_CHAR variable should hold the replacement character that was used for the SSH key (in the example above it is the comma character)

Docker Swarm build parameters

Docker Swarm build parameters

Deploy to Docker Swarm with a YML step

Once all the variables are set you can use the following freestyle step to deploy to your cluster.


    image: codefresh/remote-docker
    working_directory: ${{main_clone}}
      - rdocker ${{RDOCKER_HOST}} docker stack deploy --compose-file docker-stack.yml ${{STACK_NAME}}
      - SSH_KEY=${{SSH_KEY}}
          - master

Deploy to Docker Swarm using the pipeline’s UI deploy step

You can also use the GUI pipeline within Codefresh to deploy to Docker swarm.

  1. Select Codefresh’s Deploy Images in the pipeline’s and select codefresh/remote-docker:latest.
  2. As a deploy command use
    rdocker ${{RDOCKER_HOST}} docker stack deploy --compose-file docker-stack.yml ${{STACK_NAME}}.


GUI Deploy Step

Notice: The UI deploy step will run on any build. Make sure that your automated builds run only on a specific branch trigger.