Independent IT consultant pursuing freedom

0%

Most easy to use tutorial for deploy Rails application with Docker & Capistrano in 15 steps

TLDR: Only 15 steps, your all envorionment about Rails(local develoment and remote server production) is all setup and ready to go.

Server side

  1. Clone https://github.com/sherllochen/dockers.git. All related files is in rails directory.

  2. Port 22 and 80 port must be open in server.

  3. Install Docker and Docker compose. Docke offical install docs

  4. Change username and password of OS for deploy.

    1
    2
    3
    # nginx/Dockerfile 
    # System deploy username and password is set here. Don't forget to also change in your deploy.rb file.
    RUN useradd -rm -d /home/deploy -s /bin/bash -g root -G sudo -u 1000 your-deploy-username -p "$(openssl passwd -1your-password)"
  5. Change database name, username and password(set in docker-compose.yml). For postgres, related setting is in ‘environment’ section of backend service. For mysql, related setting is in mysql service.

  6. Edit nginx site config.

    1
    2
    # nginx/app_config
    root /your-app-deploy-path/current/public;
  7. Run docker compose and all things done.

    1
    sudo docker-compose run --service-ports production

Local development side

Step 1,2,3 is the same with server side.

  1. Run docker compose to start development environment.
    1
    sudo docker-compose run --service-ports dev
  2. Copy gits below to lib/capistrano/tasks/tasks.rb. These are customize tasks for capistrano, your more tasks can be defined here and call in deploy.rb
  1. Edit server address and username. The username must be the same as setting in server’s nginx/Dockerfile.
    1
    2
    # config/deploy/production.rb
    server "your.server.domain", user: "your-deploy-username", roles: %w{app db web}
  2. Edit deploy setting.
    1
    2
    3
    4
    5
    6
    # config/deploy.rb
    set :application, "your-app-name"
    set :repo_url, "git@github.com:your-app-repo.git"
    # Every file about config info must be set here, and copy to deply_path/shared/config manully.
    # All variable write in these files directly.
    append :linked_files, "config/database.yml", "config/master.key", "config/wechat.yml"
  3. Execute deploy command.
    1
    2
    3
    cap production deploy
    # run seed_fu task if you need.(I use SeedFu for data seed).
    cap production rails:seed_fu

    TIPS

  • All environment version change be changed in docker-compose.yml.