アプリデプロイ
ConoHa CLIを使えば、Dockerfileがあるプロジェクトをコマンド一発でデプロイできます。
前提条件
- サーバーが作成済み(サーバー管理を参照)
- サーバーにDockerがインストール済み
- プロジェクトに
Dockerfileとdocker-compose.ymlがある
デプロイの流れ
app init → app deploy → app logs で確認1. アプリの初期化
サーバー上にアプリの受け口を作成します。
bash
conoha app init my-server --app-name hello-worldInitializing app "hello-world" on vm-18268c66-ae (133.88.116.147)...
==> Installing Docker...
==> Installing Docker Compose plugin...
==> Installing git...
==> Creating directories...
Initialized empty Git repository in /opt/conoha/hello-world.git/
==> Installing post-receive hook...
==> Done!
App "hello-world" initialized on vm-18268c66-ae (133.88.116.147).
Add the remote and deploy:
git remote add conoha root@133.88.116.147:/opt/conoha/hello-world.git
git push conoha mainDocker・Git のインストールとGitリポジトリの作成が自動で行われます。
app deploy を使う場合
git push の代わりに conoha app deploy でもデプロイできます。次のセクションでは app deploy を使う方法を紹介します。
2. アプリのデプロイ
プロジェクトのディレクトリで実行します。
bash
cd /path/to/your/project
conoha app deploy my-serverApp name: hello-world
Archiving current directory...
Uploading to vm-18268c66-ae (133.88.116.147)...
Building and starting containers...
Image hello-world-web Building
...
Image hello-world-web Built
Container hello-world-web-1 Creating
Container hello-world-web-1 Created
Container hello-world-web-1 Starting
Container hello-world-web-1 Started
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
hello-world-web-1 hello-world-web "/docker-entrypoint.…" web Less than a second ago Up Less than a second 0.0.0.0:80->80/tcp
Deploy complete.実行内容:
- プロジェクトファイルをtarで圧縮
- サーバーにSSHで転送
docker compose up -d --buildを実行
.dockerignore があれば、記載されたファイルは除外されます。.git/ ディレクトリは常に除外されます。
3. 動作確認
ブラウザ/curlで確認
サーバーのIPアドレスにアクセスします。
bash
curl 133.88.116.147html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello ConoHa</title>
</head>
<body>
<h1>Hello from ConoHa!</h1>
<p>Deployed with <code>conoha app deploy</code></p>
</body>
</html>IPアドレスの確認
conoha server show my-server でサーバーのIPアドレスを確認できます。
ログを見る
bash
conoha app logs <サーバー名> --app-name myappリアルタイムでフォロー:
bash
conoha app logs <サーバー名> --app-name myapp --followステータス確認
bash
conoha app status <サーバー名> --app-name myappコンテナの状態(running/stopped)が表示されます。
アプリの再デプロイ
コードを変更したら、同じコマンドで再デプロイできます:
bash
conoha app deploy <サーバー名> --app-name myappアプリの停止・再起動
bash
# 停止
conoha app stop <サーバー名> --app-name myapp
# 再起動
conoha app restart <サーバー名> --app-name myappdocker-compose.yml の例
yaml
services:
web:
build: .
ports:
- "80:3000"
restart: unless-stoppedgit push でデプロイする方法
app deploy の代わりに git push でもデプロイできます。app init 実行時に表示されるコマンドを使います。
SSH設定
git push を使うには、~/.ssh/config にサーバーのエントリが必要です。conoha keypair create で作成したキーを指定します:
Host conoha
HostName 133.88.116.147
User root
IdentityFile ~/.ssh/conoha_my-keyリモートの追加とpush
bash
git remote add conoha conoha:/opt/conoha/hello-world.git
git push conoha mainEnumerating objects: 180, done.
...
remote: ==> Checking out main...
remote: Switched to branch 'main'
To conoha:/opt/conoha/hello-world.git
* [new branch] main -> mainIPアドレス直指定は非推奨
root@133.88.116.147:/opt/conoha/... 形式では SSH鍵が自動選択されず Permission denied になることがあります。SSH config のホスト名を使いましょう。