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.
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.
Note: this tutorial has been adapted from the Symfony Docker documentation.
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.
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!
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.
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.
Then, wait a few seconds while your Droplet is provisioning. When your Droplet is ready, use SSH to connect:
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.
Then create a DNS record of type
A for your domain name pointing to the IP address of your server.
your-domain-name.example.com. IN A 126.96.36.199
Example in Gandi's UI:
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.
Example with Git:
git clone firstname.lastname@example.org:<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
CADDY_MERCURE_JWT_SECRET to cryptographically secure random values.
Your server is up and running, and a Let's Encrypt HTTPS certificate has been automatically generated for you.
https://your-domain-name.example.com and enjoy!