This configuration can be used for hosting multiple sites on a single server, using docker and docker compose for each site. Each site needs a unique port, and will be served on a unique domain. For SSL, use Cloudflare as a proxy.
Use EC2 to create a new instance, or a similar setup with your own host.. The image should be Debian. Make sure to maintain a constant IP address. The script can then be copied directly in to the shell when you are ssh’ed in.
Once a site has been started with docker compose, exposing a port internally, use the command add-site domain-name port (e.g. add-site my.website.com 8080) to register this with the Caddy web server.
This means that any requests to my.website.com will be forwarded to your container running on the given port (e.g. 8080).
Note that the docker-compose file should include restart: always to ensure that it runs when the instance is restarted.
Use the command list-sites to list all registered sites.
If you attempt to start two Docker projects with the same IP address, docker-compose will probably complain.
However, add-site can be used to bind arbitrarily many domains to the same internal port.
In order to change the port for a given domain, simply call add-site again, and it will be overwritten.
To delete a site, delete the relevant file in /etc/caddy/sites/.
E.g. to delete my.website.com, run sudo rm /etc/caddy/sites/my.website.com.caddyfile.
Run the attached deploy script, with the following environment variables set. These variables can also be read from a .ec2-deploy file in the current directory (if not already set in the environment).
DOMAIN: The domain name that will be used to access the siteDEPLOY_HOST: The address used for connecting to the server push the changesDEPLOY_USER: The user of the server. OptionalDEPLOY_KEYPAIR: The PEM file (ssh-iflag) for connecting to the server. OptionalDEPLOY_DIR: The path on the remote server to deploy underSERVER_PORT: The port exposed by docker compose. This must be unique among VMs on the server.PORT_VARIABLE: The name of the environment variable used for passing$SERVER_PORTto the docker compose command. E.g. IfPORT_VARIABLE=APP_PORT, docker-compose.yaml could contain the entryservices.app.portsof"${APP_PORT:-5000}:5000".SOURCE_DIR: The local directory to deploy.
TODO: add support for Cloudflare certificates, and disable HTTP connections. https://developers.cloudflare.com/ssl/origin-configuration/origin-ca