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:https://www.docker.com/docker-toolbox
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: github.com/IBM-MIL/Samples/
FROM ubuntu:15.10 # Latest Swift Version ENV SWIFT_VERSION 2.2-SNAPSHOT-2015-12-01-b ENV SWIFT_PLATFORM ubuntu15.10 # 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 - https://swift.org/keys/all-keys.asc | gpg --import - && \ gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys Swift # Download and install Swift RUN SWIFT_ARCHIVE_NAME=swift-$SWIFT_VERSION-$SWIFT_PLATFORM && \ SWIFT_URL=https://swift.org/builds/$(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
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/bashTo run our sample fibonacci.swift application, run the swift command when you’re inside the swift container:
Now you can upload that shiny new image to your favorite hosting service like Bluemix 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