Deploying Server-Side Swift Apps With Docker on Heroku
In this tutorial, you’ll learn how to deploy server-side applications developed in Vapor as well as Docker images of your application into Heroku. By Natan Rolnik.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Deploying Server-Side Swift Apps With Docker on Heroku
30 mins
- Getting Started
- Building the Docker Image
- Using the Dockerfile
- Building the Image
- Running the Image Locally
- Getting to Know Heroku
- Logging in to the Heroku CLI
- Creating the Heroku App
- Adding the Postgres Add-on
- Configuring Other Environment Variables
- Setting the Heroku App Stack
- Preparing the Vapor App for Deployment
- Configuring Heroku Postgres in the App
- Setting the Database Based on the Environment
- Binding to the Correct Port
- Building and Deploying the App Image
- Container Registry
- Logging in to Heroku’s Container Registry
- Uploading and Releasing the App Image
- Reading Server Logs
- Letting Heroku Build the Image for You
- Creating the heroku.yml File
- Where to Go From Here?
Reading Server Logs
If you’ve encountered any errors or want to see your app’s logs to troubleshoot or get more information, the logs
command is very helpful. It displays logs from both Heroku and the Vapor app.
heroku logs
This displays the recent log output from your app. If you want to see live logs, pass the --tail
flag:
heroku container:push web
. Alternatively, you can also write a script that packages the instructions from this section to ease the deployment process.
Letting Heroku Build the Image for You
The process of building locally and uploading images to the registry can be tiring when deployments are frequent. But there’s good news, this task can be automated!
Creating the heroku.yml File
Heroku allows developers to add a yml configuration file at the root of the app repository. Amongst other things, this file defines how Heroku, not your machine, should perform the build and automatically release Docker images of your app.
In your text editor of choice, create a file named heroku.yml at the root of the project, and paste the three lines below:
build:
docker:
web: Dockerfile
This is enough to tell Heroku to use Docker to build an image, using the Dockerfile at the root of the project to build your app image and release it to the web process.
You must track this file in the app’s Git repository. To do so, run:
git add heroku.yml
git commit -a -m "Add heroku.yml"
To confirm the repository contains the Heroku Git remote, run git remote -v
. You’ll see a remote URL in the https://git.heroku.com/<your-app-name>.git pattern.
After certifying the Heroku Git remote is present, perform a git push
to it:
git push heroku master
As Heroku receives your new commit, it starts building the new image, and the git push
operation logs the progress:
After a few minutes, you’ll see a confirmation that the app was deployed, this time with the image originating from Heroku’s builder machines rather than your own computer!
Sometimes, if the build process is long, the SSH connection might drop, and Git will display an error message unrelated to the build. If that happens, Heroku will continue building, and you can access the app dashboard in the browser. Click the Activity tab, and then look for the build in progress:
After a few minutes, the build will complete, and your app will run the latest code you just deployed.
Where to Go From Here?
Congratulations! You’ve reached the end of this tutorial, and now you know how to create Heroku apps, configure add-ons, and build and deploy Docker containers.
Download the completed project from this tutorial using the Download Materials button at the top and bottom of this page.
If you’re looking to learn more, here are some ideas and challenges:
- Add more add-ons, such as Heroku Redis for caching, or others available in the Heroku Marketplace.
- If you own a domain, set up a custom domain for your app, which allows you to hide the
*.herokuapp.com
domain. - Automate and set up continuous delivery, either with the Heroku CI and Pipelines or with GitHub Actions.
- Deploy different environments with pipelines. For example, enable a staging environment to test your web app and its API before deploying to production.
To learn more about Server-Side Swift, Vapor and other deployment techniques, check out:
- The Server-Side Swift with Vapor book.
- The Server-Side Swift with Vapor video course.
If you have any questions or comments, please join the forum discussion below!