Easy Server Side Swift with Docker
I recently read an excellent article about server side Swift called Hello Server Side Swift.
I’m a big docker fan. I’ve written about running swift on linux using docker before, but this time I want to show you how you can utilize it to easily run server side Swift on a number of cloud platforms.
You need to have the Docker Toolkit installed if you want to follow along.
Lets start by cloning the repo from the Hello Server Side Swift article:
$ git clone https://github.com/LoganWright/swift-server-io.git
$ cd swift-server-io
Now add a file named ‘Dockerfile’ with the following contents:
ADD . /SwiftServerIO
RUN swift build --configuration release
A Dockerfile is a set of instructions which tell docker how to build our image.
FROM sets our base image. swiftdocker/swift is an Ubuntu 14.04 image with swift installed.
ADD adds the content of a local directory to a directory inside the image. We add the local directory ‘.’ (the current directory) to the image’s ‘/SwiftServerIO’ Directory.
WORKDIR sets directory from which The following RUN and ENTRYPOINT commands are run.
RUN will execute a command. Here we build the SwiftServerIO project for release.
EXPOSE informs Docker that the container listens on the specified network port at runtime. We expose port 8080 which SwiftServerIO runs on.
ENTRYPOINT is the executable that our container will run. We us ‘.build/release/SwiftServerIO’ which was built in the RUN step.
This simple Dockerfile is actually enough to run our Swift server on any host with the Docker Engine on them.
We are going to use docker-machine to provision a host with Docker Engine on Digital Ocean. Docker Machine is a tool that can provision and manage dockerized hosts both locally and on a number of different cloud providers.
If you want to learn how to setuo a local host or a host on other cloud platforms goto https://www.docker.com/products/docker-machine.
To provision on Digital Ocean you first need an API key. Head over to https://cloud.digitalocean.com/settings/api/tokens and generate an new token with read and write scope.
Now its time to setup that server. Run the following command, replacing [TOKEN] with your access token.
$ docker-machine create --driver digitalocean --digitalocean-access-token [TOKEN] swift-server
Docker Machine will boot up a new server and install the Docker Engine. You will notice your server here https://cloud.digitalocean.com/droplets.
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default virtualbox Running tcp://192.168.99.100:2376
swift-server * digitalocean Running tcp://18.104.22.168:2376 v1.10.1
swift-server should be running and active as indicated by the stereo (*). If for some reason your new machine is not the active host, you’ll need to run docker-machine env swift-server, followed by eval $(docker-machine env swift-server) to connect to it.
Now that we have a docker host running we can build our image:
$ docker build -t swift-server-image .
We can now run our image on the host with this:
$ docker run --name webserver -p 80:8080 swift-server-image
We name the service ‘webserver’, and make the image’s port 8080 available on the hosts port 80.
Get the servers ip with this command:
$ docker-machine ip swift-server
Open the ip followed by the route ‘/hello’ or ‘/hello/lars’ in your browser.
Let me know what you think, and if you find anything wrong with how i solved this please let me know!
I might do a follow up, with a simple webservice that uses redis for persistence of someone is interested. Maybe something like a counter that increases on every page load.