Server Side Swift Deployment — One Approach

Fatih Nayebi
Server Side Swift
Published in
4 min readMar 27, 2018

There are many ways to deploy Server Side Swift into production. Let’s explore the topic and go through an approach that is simple and easy to maintain 😊.

Platform as a Service (PaaS)

Cloud solutions provide platform as a service so we can use them to deploy our applications without dealing with infrastructures.

Examples of these platforms are

Google Cloud Platform (GCP) App Engine

GCP App Engine custom flexible environment is suitable to deploy Server Side Swift into the cloud.

Heroku

Heroku build packs can be used to deploy Serve Side Swift into the cloud easily.

Perfect Heroku Buildpack

Vapor Heroku Buildpack

Amazon Web Services (AWS) Elastic Beanstalk

My preference would be the GCP App Engine because of its simplicity and pricing model.

Infrastructure as a Service (IaaS)

If you need more control over your environment and if you need to manage your own Virtual Machine (VM) then IaaS platforms are the solution.

Examples of these platforms are:

AWS Elastic Compute Cloud (EC2)

GCP Compute Engine

☁️ Private Cloud

If you find the cloud platforms too expensive or you have other legislative requirements that stop you from using cloud platforms then you would need to either host your own server or use a hosting solution and manage your own VMs.

What if you could not decide which approach you should follow or you know that you may need to change the cloud platform.

Portability of Server Side Swift ❓💭

Is there any solution to deploy Server Side Swift that can minimize the migration and porting costs from one platform to another?

📌 Solution

The answer as you already guessed is the Docker + nginx 💪.

Running the Server Side Swift in docker containers and using nginx as the reverse proxy is something that would work in most of platforms. Let’s see how we can achieve that.

Create docker images

The easiest way to do it, to use Perfect Assistant (PA). PA will create 2 docker files:

  • PADockerfile_build to build a docker image, containing all dependencies
  • PADockerfile_deploy to use the previously created built docker image and deploy the project.

So I use PA to build and deploy my project for Linux and create my own Dockerfiles from the ones created by PA. Unfortunately, PA doesn’t provide a CLI at this time so I need to do it manually. The rest of the process is automated using a bash script I share below:

#!/bin/bash
echo "Update project to version "$1
docker build -f Dockerfile-build -t project-build .
docker build -f Dockerfile-deploy -t project-deploy .
docker tag project-deploy:latest your-docker-registry/project:$1
docker push your-docker-registry/project:$1
echo "Done updating project to version "$1

As you ca see in the script above, I use Dockerfile-build and Dockerfile-Deploy to create the image and push it to a Private Docker Registry or Cloud Container Registry (e.g. GCP Container Registry) so I can pull and run it anytime I want. You will find these files in upcoming subsections.

To run it, I need to execute the following command in a terminal:

sh project.sh 1.0.0

🚧 Dockerfile for building

Let’s check our Dockerfile-build:

FROM perfectlysoft/perfectassistant:4.1
# Perfect-mysqlclient-Linux.git-1864881310269612043/PADockerfile
RUN apt-get -y update && apt-get install -y libmysqlclient-dev
RUN sed -i -e 's/-fabi-version=2 -fno-omit-frame-pointer//g' /usr/lib/x86_64-linux-gnu/pkgconfig/mysqlclient.pc
# Perfect-COpenSSL-Linux.git-3018179716261764243/PADockerfile
RUN apt-get -y update && apt-get install -y libssl-dev
# Perfect-libcurl.git--6009391049797615682/PADockerfile
RUN apt-get -y update && apt-get install -y libcurl4-openssl-dev
# Perfect-LinuxBridge.git-3155094712639753971/PADockerfile
RUN apt-get -y update && apt-get install -y uuid-dev
RUN rm -rf /var/lib/apt/lists/*

RUN apt-get -y update && apt-get install -y vim

This is just an example and you will have different commands in it with different dependencies.

🚢 Dockerfile for deployment

In this step, previously built image will be used to deploy our project so here is our Dockerfile-Deploy

FROM project-build:latest
COPY .build_lin/release/Project /perfect-deployed/project/
COPY webroot /perfect-deployed/project/webroot
COPY config /perfect-deployed/project/config
COPY resources /perfect-deployed/project/resources
RUN rm -rf /var/lib/apt/lists/*
CMD cd /perfect-deployed/project/ && ./Project

Definitely, you need to replace the project with your project name.

Deploy and run the container

As the image already pushed to a docker registry, you can just pull and run it in your Server. Also, you can easily automate the pulling/running process using a CI tool such as Jenkins or using cloud deployment management tools such as AWS CloudFormation and GCP Deployment Manager.

You can follow this guide for serving it with NGINX:

Conclusion

There are many ways to deploy your Server Side Swift either with Docker or without it. This post explained one approach that may not be suitable for everyone and also there are a lot of things that can be improved but as it is, it’s portable and easy to maintain 😐.

--

--

Fatih Nayebi
Server Side Swift

Data Scientist, Author of Swift Functional Programming Book, Lecturer at McGill University