Did the IBM Swift Sandbox get you hooked on Swift? Great! Here is a quick guide to creating a docker image of your very own, with Swift baked in to build the next big thing.


The Docker toolbox is all you’ll need to get started:

Docker, meet Swift!

Here are the contents of a Dockerfile that is quite similar to what is being used by the backend of the IBM Swift Sandbox. You can also access the source code Dockerfile and a sample swift application at:

FROM ubuntu:15.10

# Latest Swift Version

# Install Dependencies
RUN apt-get update && \
apt-get install -y \
clang \
libicu55 \
libpython2.7 \
wget && \
apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Install Swift keys
RUN wget -q -O - | gpg --import - && \
gpg --keyserver hkp:// --refresh-keys Swift

# Download and install Swift
SWIFT_URL=$(echo "$SWIFT_PLATFORM" | tr -d .)/swift-$SWIFT_VERSION/$SWIFT_ARCHIVE_NAME.tar.gz && \
wget $SWIFT_URL && \
wget $SWIFT_URL.sig && \
gpg --verify $SWIFT_ARCHIVE_NAME.tar.gz.sig && \
tar -xvzf $SWIFT_ARCHIVE_NAME.tar.gz -C / --strip 1 && \
rm -rf $SWIFT_ARCHIVE_NAME* /tmp/* /var/tmp/*

#Building a webserver? Expose Port 80 by uncommenting the following.
#Expose 80

#Copy our sample swift program into the Docker image
COPY fibonacci.swift /tmp/

To build that image simply run docker build -t swift ./

This will take a few minutes if docker needs to download the Ubuntu 15.10 base image and the Swift binaries, but subsequent builds will require much less patience.

We can verify that the image was built with docker images

All that is left to do is run it! We’re going attach all the output streams to the bash shell.

Just use the following command to launch into the container, where you’ll be able to invoke swift:

docker run -it swift /bin/bash


To run our sample fibonacci.swift application, run the swift command when you’re inside the swift container:

swift /tmp/fibonacci.swift

Now you can upload that shiny new image to your favorite hosting service like IBM Cloud and have an scalable environment setup in no time. Stay tuned for more on that in an upcoming blog.


Kevin Lai, Software Engineer at the IBM’s Mobile Innovation Lab in Austin, Texas

3 comments on"Running Swift within Docker"

  1. Rick Longley February 04, 2016

    Very cool. Trying this!

  2. Haris Amin March 08, 2016

    A bunch of us have gotten a swift docker container working fairly well. We’re actually in talks to get this officially supported with Docker and Swift team. Stay tuned 🙂

  3. Margherita Withnell April 01, 2016

    Everyone loves what you guys are usually up too. This type of clever work
    and exposure! Keep up the excellent works guys I’ve added you guys to my personal blogroll.

Join The Discussion

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