Help us understand the problem. What is going on with this article?

docker + capistrano で deploy の動作確認をする

More than 1 year has passed since last update.

ssh接続サーバーをdockerで構成

公式チュートリアルを参考に ssh 接続可能なサーバーを立てておく。
この例ではrootユーザーでパスワード認証する。

Dockerfile
FROM ubuntu:16.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

https://docs.docker.com/engine/examples/running_ssh_service/#build-an-eg_sshd-image

イメージ作成

$ docker image build . -t ssh_server

port を指定してコンテナ実行

$ docker run -d -p 10000:22 ssh_server

docker コンテナから Github に接続できるように、秘密鍵を置いておく。

Githubには手元から公開鍵でssh接続できる前提。

方法は何でも良い。この例では docker cp コマンドで、直接手元からコピーする。

$ docker cp ~/.ssh/id_rsa_github_aiming コンテナID:/root/.ssh

capstrano を インストール

$ gem install capistrano
$ cap install

capistrano の設定変更

適当なレポジトリを指定する。
この例では https://github.com/github/hub を使う。

ちなみにレポジトリの中身は何でも良い。
(レポジトリ側にcapistrano関係のファイルは必要ない)

deploy.rb
set :repo_url, "git@github.com:github/hub.git"

手元からサーバー ( docker コンテナ ) に繋ぐための設定を書く。

deploy/production.rb
# The server-based syntax can be used to override options:
# ------------------------------------
server "localhost",
  user: "root",
  roles: %w{web app},
  ssh_options: {
    forward_agent: false,
    auth_methods: %w(password),
    port: 10000,
    password: "screencast"
  }

デプロイを実行

$ cap production deploy

dockerコンテナに deploy されているのを確認

root@8af7b375dc67:/# ls -la /var/www/my_app_name/
total 24
drwxr-xr-x 5 root root 4096 May 11 06:52 .
drwxr-xr-x 3 root root 4096 May 11 06:52 ..
lrwxrwxrwx 1 root root   44 May 11 06:52 current -> /var/www/my_app_name/releases/20180511065207
drwxr-xr-x 3 root root 4096 May 11 06:52 releases
drwxr-xr-x 7 root root 4096 May 11 06:52 repo
-rw-r--r-- 1 root root  106 May 11 06:52 revisions.log
drwxr-xr-x 2 root root 4096 May 11 06:52 shared

環境

  • Capistrano Version: 3.10.2 (Rake Version: 12.0.0)
  • Mac OS X Sierra
  • Docker version 18.03.0-ce, build 0520e24
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした