Deploy to Docker SWARM

Deploy to Docker Swarm with Codefresh

Codefresh can easily deploy your application to Docker Swarm using Codefresh 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

For an example Docker Swarm application, see https://github.com/codefreshdemo/example-voting-app

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

Run in this root directory:

docker-compose up

The app runs at http://localhost:5000, and the results are 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

NOTE
The swarm master must have Python installed.

Deploy to Remote Swarm with Codefresh

First you need to set up 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 YAML step

Once you have defined all the variables, deploy to your cluster using the following freestyle step.

codefresh.yml

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

You can also pass custom credentials like this:

codefresh.yml

deploy_to_swarm:
    image: codefresh/remote-docker
    working_directory: ${{main_clone}}
    commands:
      - rdocker ${{RDOCKER_HOST}} docker login ${{MY_REGISTRY}} -u ${{MY_REGISTRY_USER}} -p ${{MY_REGISTRY_PASSWORD}} \&\& docker stack deploy --compose-file docker-compose.yml --with-registry-auth ${{STACK_NAME}}
    environment:
      - SSH_KEY=${{SSH_KEY}}
    when:
      branch:
        only:
          - master

Create a CI/CD pipeine for Docker Swarm

Here is the complete pipeline:

Docker Swarm pipeline

Docker Swarm pipeline

And here is the pipeline definition:

codefresh.yml

version: '1.0'
stages:
  - prepare   
  - build
  - deploy
steps:
  main_clone:
    title: Cloning main repository...
    stage: prepare
    type: git-clone
    repo: 'codefreshdemo/example-voting-app'
    revision: master
    git: github-1    
  MyResultDockerImage:
    title: Building Result Docker Image
    stage: build
    type: build
    image_name: resultApp
    working_directory: ./result/
    tag: master
    dockerfile: Dockerfile  
  MyVoteDockerImage:
    title: Building Vote Docker Image
    stage: build
    type: build
    image_name: voteApp
    working_directory: ./vote/
    tag: master
    dockerfile: Dockerfile  
  MyWorkerDockerImage:
    title: Building Worker Docker Image
    stage: build
    type: build
    image_name: workedApp
    working_directory: ./worker/
    tag: master
    dockerfile: Dockerfile 
  DeployToSwarmNow:
    image: codefresh/remote-docker
    working_directory: ${{main_clone}}
    stage: deploy
    commands:
      - rdocker ${{RDOCKER_HOST}} docker login ${{MY_REGISTRY}} -u ${{MY_REGISTRY_USER}} -p ${{MY_REGISTRY_PASSWORD}} \&\& docker stack deploy --compose-file docker-compose.yml --with-registry-auth ${{STACK_NAME}}
    environment:
      - SSH_KEY=${{SSH_KEY}}

The values of MY_REGISTRY, MY_REGISTRY_USER and MY_REGISTRY_PASSWORD depend upon the type of your connected registry.

CD pipeline examples
Codefresh YAML for pipeline definitions
Steps in pipelines
Creating pipelines
How Codefresh pipelines work