Swift@IBM Logo

We are excited to announce that in addition to the Docker image for Swift development that we released over a year ago, ibmcom/swift-ubuntu, we have recently made available an additional Docker image for Swift enthusiasts, ibmcom/swift-ubuntu-runtime. This new image is much smaller in size than the ibmcom/swift-ubuntu image and is tailored for provisioning your Swift applications to cloud environments, such as Bluemix. By not including in the ibmcom/swift-ubuntu-runtime image any of the build tools (e.g. SwiftPM, clang, etc.) that are required for compiling, linking and testing your Swift applications, we were able to create an image with a size of roughly 300 MB. The ibmcom/swift-ubuntu-runtime image contains only those shared objects (.so files) from the Swift binaries that are required for executing your Swift applications. Therefore, this Docker image is not suited for application development, testing, or debugging but exclusively for running Swift applications.

We can take the ibmcom/swift-ubuntu-runtime image for a test drive by extending it, adding the code for the Kitura-Starter application (a Bluemix starter application for the Kitura web framework and HTTP server), and running this sample application in a Docker container. Let’s start by cloning the Kitura-Starter application:

$ git clone https://github.com/IBM-Bluemix/Kitura-Starter
Cloning into 'Kitura-Starter'...
remote: Counting objects: 639, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 639 (delta 4), reused 0 (delta 0), pack-reused 622
Receiving objects: 100% (639/639), 116.52 KiB | 0 bytes/s, done.
Resolving deltas: 100% (340/340), done.

After cloning Kitura-Starter, let’s create a backup of the Dockerfile found in your local repository of the Kitura-Starter sample application (i.e. rename it to Dockerfile.original):

$ mv Dockerfile Dockerfile.original
$ ls -la
total 128
drwxr-xr-x  17 olivieri  staff    578 Feb 13 13:20 .
drwxr-xr-x  16 olivieri  staff    544 Feb 13 13:17 ..
-rw-r--r--   1 olivieri  staff    189 Feb 13 13:17 .cfignore
drwxr-xr-x  13 olivieri  staff    442 Feb 13 13:17 .git
-rw-r--r--   1 olivieri  staff    139 Feb 13 13:17 .gitignore
-rw-r--r--   1 olivieri  staff      0 Feb 13 13:17 .gitmodules
-rw-r--r--   1 olivieri  staff      6 Feb 13 13:17 .swift-version
-rw-r--r--   1 olivieri  staff   1561 Feb 13 13:17 .travis.yml
-rw-r--r--   1 olivieri  staff   1287 Feb 13 13:17 Dockerfile.original
-rw-r--r--   1 olivieri  staff  11324 Feb 13 13:17 LICENSE
-rw-r--r--   1 olivieri  staff   1200 Feb 13 13:17 Package.swift
-rw-r--r--   1 olivieri  staff  18179 Feb 13 13:17 README.md
drwxr-xr-x   3 olivieri  staff    102 Feb 13 13:17 Sources
drwxr-xr-x   3 olivieri  staff    102 Feb 13 13:17 ci
-rw-r--r--   1 olivieri  staff    217 Feb 13 13:17 docker-compose.yml
-rw-r--r--   1 olivieri  staff    163 Feb 13 13:17 manifest.yml
drwxr-xr-x   6 olivieri  staff    204 Feb 13 13:17 public

Let’s now compile the Kitura-Starter application. However, executing swift build directly on your macOS won’t be quite useful. Instead, we need to compile the application on a system that runs Ubuntu 14.04. If you are wondering how you can do this, that’s what our development image for Swift (ibmcom/swift-ubuntu) is for! Make sure you have the latest version of this image on your local system:

$ docker pull ibmcom/swift-ubuntu:latest
latest: Pulling from ibmcom/swift-ubuntu
c60055a51d74: Already exists 
755da0cdb7d2: Already exists 
969d017f67e6: Already exists 
37c9a9113595: Already exists 
a3d9f8479786: Already exists 
33d721a2c9ee: Pull complete 
218060c9f1e9: Pull complete 
7baf37364ea1: Pull complete 
174817f7b6e2: Pull complete 
89c45f744f23: Pull complete 
Digest: sha256:b4c38381c7be4a6d80092ed099b4d9419e65a865225c6fe994fd7d80ca1dea32
Status: Downloaded newer image for ibmcom/swift-ubuntu:latest

With the latest version of the ibmcom/swift-ubuntu image available on your local development system, you can create a new Docker container, mount the folder that contains the Kitura-Starter repository, and compile the sample application in release mode (i.e. swift build --configuration release) instead of debug mode:

$ docker run -i -t -v /Users/olivieri/git/Kitura-Starter/:/root/Kitura-Starter ibmcom/swift-ubuntu:latest
root@c528db75744e:~# cd Kitura-Starter/
root@c528db75744e:~/Kitura-Starter# swift build --clean
root@c528db75744e:~/Kitura-Starter# swift build --configuration release
Compile Swift Module 'Socket' (3 sources)
Compile Swift Module 'LoggerAPI' (1 sources)
Compile Swift Module 'KituraTemplateEngine' (1 sources)
Compile Swift Module 'SwiftyJSON' (2 sources)
Compile Swift Module 'HeliumLogger' (2 sources)
Compile Swift Module 'CloudFoundryEnv' (7 sources)
Compile Swift Module 'SSLService' (1 sources)
Compile CHTTPParser utils.c
Compile CHTTPParser http_parser.c
Linking CHTTPParser
Compile Swift Module 'KituraNet' (35 sources)
Compile Swift Module 'CloudFoundryDeploymentTracker' (1 sources)
Compile Swift Module 'Kitura' (42 sources)
Compile Swift Module 'Kitura_Starter' (2 sources)
Linking ./.build/release/Kitura-Starter
root@c528db75744e:~/Kitura-Starter# 

When you compile a Swift application in release mode, the binary generated is optimized for a production deployment and debugging information is not included in the binaries. For further details on how to use the ibmcom/swift-ubuntu for developing server side Swift projects, we invite you to checkout the README file of our GitHub repository for our Docker images.

Once the Kitura-Starter code is compiled, let’s create a new Dockerfile with the contents shown below:

# Builds a Docker image for running the Kitura-Starter sample application.

FROM ibmcom/swift-ubuntu-runtime:latest
MAINTAINER IBM Swift Engineering at IBM Cloud
LABEL Description="Docker image for running the Kitura-Starter sample application."

USER root

# Expose default port for Kitura
EXPOSE 8080

# Binaries should have been compiled against the correct platform (i.e. Ubuntu 14.04).
COPY .build/release/Kitura-Starter /root/Kitura-Starter/.build/release/Kitura-Starter
COPY .build/release/*.so /root/Kitura-Starter/.build/release/
COPY public /root/Kitura-Starter/public
CMD [ "sh", "-c", "/root/Kitura-Starter/.build/release/Kitura-Starter" ]

The above Dockerfile extends the new ibmcom/swift-ubuntu-runtime image, exposes port 8080, and adds the necessary project files (i.e. public folder) and generated binaries for the Kitura-Starter application. You should know that the public folder contains static HTML content that is served by the sample server. Therefore, we include this folder along with the generated binaries in the new Docker image. We can now create a runtime image (kitura-starter-runner) using the new Dockerfile by executing the following command in the root folder of the Kitura-Starter repo:

$ $ docker build -t kitura-starter-runner:latest .
Sending build context to Docker daemon 37.55 MB
Step 1/8 : FROM ibmcom/swift-ubuntu-runtime:latest
 ---> 6952fdbe713f
Step 2/8 : MAINTAINER IBM Swift Engineering at IBM Cloud
 ---> Using cache
 ---> 57cd5e55842e
Step 3/8 : LABEL Description "Docker image for running the Kitura-Starter sample application."
 ---> Using cache
 ---> 5a3f43462ecf
Step 4/8 : USER root
 ---> Using cache
 ---> a1383a501f00
Step 5/8 : EXPOSE 8080
 ---> Using cache
 ---> a6b1788b215d
Step 6/8 : RUN mkdir /root/Kitura-Starter
 ---> Using cache
 ---> 4015340cba88
Step 7/8 : COPY . /root/Kitura-Starter
 ---> Using cache
 ---> ac6c3098b3f1
Step 8/8 : CMD sh -c /root/Kitura-Starter/.build/release/Kitura-Starter
 ---> Using cache
 ---> c2975f3aa91c
Successfully built c2975f3aa91c

Now that we have a new image, kitura-starter-runner, that extends the ibmcom/swift-ubuntu-runtime image and contains the binaries for the Kitura-Starter application, let’s create a new container to execute the sample application.

 docker run -p 8080:8080 -i -t kitura-starter-runner:latest
[2017-02-13T21:33:41.647Z] [INFO] [main.swift:29 Kitura_Starter] Server will be started on 'http://localhost:8080'.
[2017-02-13T21:33:41.652Z] [INFO] [HTTPServer.swift:88 listen(on:)] Listening on port 8080

Once the application is running in the Docker container, you can access it by pointing your browser to http://localost:8080:

Kitura-Starter welcome page

You have now successfully used the new runtime image for Swift, ibmcom/swift-ubuntu-runtime, to execute a Swift application inside a Docker container! You can use this new runtime image for running and making your applications available to your customers on Bluemix. For details on how to publish your Swift applications using containers, feel free to check out 10 Steps To Running a Swift App in an IBM Container.

For more information on server-side Swift, visit the Swift@IBM Developer Center.

Ricardo Olivieri, Senior Software Engineer, IBM Swift Engineering at IBM Cloud

Join The Discussion

Your email address will not be published. Required fields are marked *