Deploying with Docker Compose

While Docker Compose is mainly known and used in a development environment, it can be used in production too. This is especially suitable for prototyping or small-scale deployments, where the robustness (and the associated complexity) of Kubernetes is not required.

Docker Compose は主に開発環境で知られ、使用されていますが、運用環境でも使用できます。これは、Kubernetes の堅牢性 (および関連する複雑さ) が必要とされない、プロトタイピングまたは小規模な展開に特に適しています。

API Platform provides Docker images and a Docker Compose definition optimized for production usage. In this tutorial, we will learn how to deploy our Symfony application on a single server using Docker Compose.

API プラットフォームは、本番環境での使用に最適化された Docker イメージと Docker Compose 定義を提供します。このチュートリアルでは、Docker Compose を使用して単一サーバーに Symfony アプリケーションをデプロイする方法を学習します。

Note: this tutorial has been adapted from the Symfony Docker documentation.

注: このチュートリアルは、Symfony Docker のドキュメントを基にしています。

Preparing a Server

To deploy your application in production, you need a server. In this tutorial, we will use a virtual machine provided by DigitalOcean, but any Linux server can work. If you already have a Linux server with Docker Compose installed, you can skip straight to the next section.

アプリケーションを本番環境にデプロイするには、サーバーが必要です。このチュートリアルでは、DigitalOcean が提供する仮想マシンを使用しますが、任意の Linux サーバーが機能します。Docker Compose がインストールされた Linux サーバーが既にある場合は、直接スキップして、次のセクション。

Otherwise, use this affiliate link to get $100 of free credit, create an account, then click on "Create a Droplet". Then, click on the "Marketplace" tab under the "Choose an image" section and search for the app named "Docker". This will provision an Ubuntu server with the latest versions of Docker and Docker Compose already installed!

それ以外の場合は、このアフィリエイト リンクを使用して 100 ドルの無料クレジットを取得し、アカウントを作成してから、[Create a Droplet] をクリックします。次に、[Choose an image] セクションの下にある [Marketplace] タブをクリックし、「Choose an image」セクションの下にある「Marketplace」タブをクリックして、「」という名前のアプリを検索します。これにより、最新バージョンの Docker と Docker Compose が既にインストールされている Ubuntu サーバーがプロビジョニングされます。

For test purposes, the cheapest plan will be enough. For real production usage, you'll probably want to pick a plan in the "general purpose" section that will fit your needs.

テスト目的では、最も安いプランで十分です。実際の本番環境で使用する場合は、ニーズに合った「汎用」セクションのプランを選択することをお勧めします。

Deploying a Symfony app on DigitalOcean with Docker Compose

You can keep the defaults for other settings or tweak them according to your needs. Don't forget to add your SSH key or to create a password, then press the "Finalize and create" button.

他の設定はデフォルトのままにしておくことも、必要に応じて微調整することもできます。SSH キーを追加するか、パスワードを作成することを忘れないでください。その後、[最終化して作成] ボタンを押してください。

Then, wait a few seconds while your Droplet is provisioning. When your Droplet is ready, use SSH to connect:

次に、ドロップレットがプロビジョニングされるまで数秒待ちます。ドロップレットの準備ができたら、SSH を使用して接続します。

ssh root@<droplet-ip>

Configuring a Domain Name

In most cases, you'll want to associate a domain name with your website. If you don't own a domain name yet, you'll have to buy one through a registrar. Use this affiliate link to redeem a 20% discount at Gandi.net.

ほとんどの場合、ドメイン名を Web サイトに関連付けます。ドメイン名をまだ所有していない場合は、レジストラーから購入する必要があります。このアフィリエイト リンクを使用して、 Gandi.net.

Then create a DNS record of type A for your domain name pointing to the IP address of your server.

次に、サーバーの IP アドレスを指すドメイン名のタイプ A の DNS レコードを作成します。

Example:

例:

your-domain-name.example.com.  IN  A     207.154.233.113
````

Example in Gandi's UI:

![Creating a DNS record at Gandi.net](gandi-dns.png)

Note: Let's Encrypt, the service used by default by API Platform to automatically generate a TLS certificate, doesn't support using bare IP addresses.
Using a domain name is mandatory to use Let's Encrypt.

## Deploying

Copy your project on the server using `git clone`, `scp` or any other tool that may fit your needs.
If you use GitHub, you may want to use [a deploy key](https://docs.github.com/en/developers/overview/managing-deploy-keys#deploy-keys).
Deploy keys are also [supported by GitLab](https://docs.gitlab.com/ee/user/project/deploy_keys/).

Example with Git:

```console
git clone git@github.com:<username>/<project-name>.git

Go into the directory containing your project (<project-name>), and start the app in production mode:

プロジェクト () を含むディレクトリに移動し、アプリを運用モードで起動します。

SERVER_NAME=your-domain-name.example.com \
APP_SECRET=ChangeMe \
POSTGRES_PASSWORD=ChangeMe \
CADDY_MERCURE_JWT_SECRET=ChangeMe \
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Be sure to replace your-domain-name.example.com with your actual domain name and to set the values of APP_SECRET, CADDY_MERCURE_JWT_SECRET to cryptographically secure random values.

your-domain-name.example.com を実際のドメイン名に置き換え、APP_SECRET、CADDY_MERCURE_JWT_SECRET の値を暗号的に安全なランダム値に設定してください。

Your server is up and running, and a Let's Encrypt HTTPS certificate has been automatically generated for you. Go to https://your-domain-name.example.com and enjoy!

サーバーが稼働中で、Let's Encrypt HTTPS 証明書が自動的に生成されました。https://your-domain-name.example.com にアクセスしてお楽しみください!

Deploying on Multiple Nodes

If you want to deploy your app on a cluster of machines, we recommend using Kubernetes. You can use Docker Swarm, which is compatible with the provided Compose files.

マシンのクラスターにアプリをデプロイする場合は、Kubernetes を使用することをお勧めします。提供されている Compose ファイルと互換性のある Docker Swarm を使用できます。