如何使用docker&ansible ROR部署和运行捆绑安装?

时间:2019-05-05 02:02:36

标签: ruby-on-rails docker ansible bundle bluepill

我正在将Ansible与Docker一起部署ROR应用程序,创建了Ruby Docker映像并将其推送到Docker HUB,Ansible将应用程序从私有存储库克隆到目标服务器,成功创建了Docker容器,但无法运行诸如Bundle安装,DB Migrate,Rake Task之类的Ruby命令。这些命令需要在从Ansible Code以交互模式进行部署时运行。

以及如何使用独角兽运行上述任务后如何启动应用程序。

这是要使用Ansible(以前使用Capistrano,但现在我们要从Capistrano迁移到Ansible)来自动化部署过程。

---
- hosts: Ec2-Instance
  become: true
  tasks:
   - name: Creating Deploy path
     file:
       state: directory
       path: "/var/www/"
  - name: Copying SSH Key | Temp
    copy:
      src: ~/.ssh/id_rsa
      dest: ~/.ssh/id_rsa
      owner: deployer
      group: deployer
      mode: 400
  - name: Cloning a web application to the application path
    git:
      repo: private repo
      version: master
      dest: /var/www/app-path/ 
      accept_hostkey: yes
    register: git
     - name: Remove SSH Key
       shell: "sudo rm -rf ~/.ssh/id_rsa"

  - name: Run Ruby Docker Container
    docker_container:
      name: 'container_name'
      image: 'docker-hub/ruby-2.4.1:0.1'
      tty: yes
      detach: true
      restart: yes # not required. Use with started state to force a matching container to be stopped and restarted.
      interactive: yes # not required. Keep stdin open after a container is launched, even if not attached.
      state: started
      volumes: /var/www/app-path:/var/www/app-path/
      working_dir: /var/www/app-path/
      env:
        gem_path: /var/www/app-path
        gemfile: /var/www/app-path
      bundler:
        deployment_mode: production

我希望Ansible Playbook能够与Docker / Nginx / Unicorn一起部署Ruby on Rails应用程序

1 个答案:

答案 0 :(得分:0)

您的剧本失败了,因为您在docker_container模块(docker_container module

中指定了以下参数,该参数不是有效参数
...
bundler:
      deployment_mode: production

这里的另一种方法可能是这样

在/var/www/app-path/install-ror.sh

中创建Shell脚本
#!/bin/sh

working_dir=/var/www/app-path/
cd $working_dir

export gem_path=/var/www/app-path
export gemfile=/var/www/app-path

#see https://stackoverflow.com/questions/10912614/rails-bundle-install-production-only 
gem install bundle-only

#install only production modules
bundle-only production

如下更新您的剧本

- name: Run Ruby Docker Container
    docker_container:
        name: 'container_name'
        image: 'docker-hub/ruby-2.4.1:0.1'
        tty: yes
        detach: true
        restart: yes # not required. Use with started state to force a matching container to be stopped and restarted.
        interactive: yes # not required. Keep stdin open after a container is launched, even if not attached.
        state: started
        volumes: /var/www/app-path:/var/www/app-path/
        working_dir: /var/www/app-path/
        command: /var/www/app-path/install-ror.sh

您可以按照输出进行改进。