使用Ansible将Docker映像部署到Dokku

图片


序幕


我最近了解到一种类似于Heroku的“口袋式” PaaS,名称相当明显-Dokku。能够将证书简单地添加到应用程序和开箱即用的功能使我非常着迷,因此我决定将docker映像传输到Dokku。没错,我很失望,在独库没有像Heroku这样的队伍


dokku container:push
dokku container:release
//    ,    :(

tags , . , image .


tags:create <app> <tag>                        # Add tag to latest running app image
tags:deploy <app> <tag>                        # Deploy tagged app image
tags:destroy <app> <tag>                       # Remove app image tag

Ansible , . dokku, , .


Playbook


playbook , , dokku. Gitlab . .


push-image.dokku.yml


push-image.dokku.yml
---
  - name: "DEPLOY APP '{{ appname }}' TO DOKKU"
    hosts: dokku_hosts
    remote_user: root
    gather_facts: false
    vars:
      tarname: "{{ appname }}__{{ image }}.tar"
      upload_dir: "/usr/local/src"
      upload_path: "{{ upload_dir }}/{{ tarname }}"
      apptag: "{{ upload_tag | default('latest') }}"
      dokku_image: "dokku/{{ appname }}:{{ apptag }}"

    tasks:
      - name: "Archive '{{ image }}' to upload"
        register: env
        delegate_to: localhost
        shell: 
          cmd: docker image save -o "./{{ tarname }}" {{ image }}

      - name: "Upload image '{{ image }}' to dokku at '{{ inventory_hostname }}'"
        register: upload
        copy:
          src: "./{{ tarname }}"
          dest: "{{ upload_path }}"

      - name: "Log - Upload result"
        debug:
          var: upload.dest

      - name: "Restore uploaded docker image"
        register: restore
        shell:
          cmd: docker image load -i "{{ upload_path }}"

      - name: "Log - Restore image"
        debug:
          var: restore.stdout_lines

      - name: "Retag image to '{{ dokku_image }}'"
        shell: 
          cmd: docker tag "{{ image }}" "{{ dokku_image }}"

      - name: "Create dokku tag for '{{ dokku_image }}'"
        shell: 
          cmd: dokku tags:create "{{ appname }}" "{{ apptag }}"

      - name: "Release '{{ appname }}'"
        register: release
        shell: 
          cmd: dokku tags:deploy "{{ appname }}" "{{ apptag }}"  

      - name: "Log - Release"
        debug:
          var: release.stdout_lines  

ansible dokku



ansible-playbook push-image.dokku.yml -i some_inventory -e "appname=DOKKU_APP_NAME image=DOCKER_IMAGE"

some_inventory


[dokku_hosts]
your.domain.example

All Articles