dockerkitura

Use Docker + Kitura, but not on a Mac


I have a perfectly fine Swift-Docker-Kitura project on a Mac ...

enter image description here

You work on the code using Xcode, and then in terminal you docker build. It builds. You docker run and you can even see the web page locally on localhost. You can then docker push and it goes to the main AWS cloud and is hosted.

That's all great.

But what if I want to use Swift-Docker-Kitura "not on a Mac"?

Can you "build" and "push" such a swift project - on some sort of shell on AWS (or a similar service)?


Solution

  • Your options are limited. First of all Swift is only certified at this moment on Ubuntu. So the host you want to develop on either must run Ubuntu or alternatively must run Docker under which you can run Ubuntu.

    In theory you can also compile swift on your target host. I did this for an older version of Swift once for Debain (Jessie) and although I managed to get it compiled it was certainly not stable. Things may have improved since then, I haven't checked that.

    So the easiest way is to rely on Docker. This also allows you to still develop on your mac by editing in Xcode and then on the command line you have the options to compile it manually.

    I use the following bash function to open my Linux environment:

    SwiftDocker2 ()
    {
        name=`perl -e 'open IN, "</usr/share/dict/words";rand($.) \
            < 1 && ($n=$_) while <IN>;print $n'`;
        docker run -i -t --name=${name} -h ${name} --log-driver=json-file \
            -w /project -v $(pwd):/project -p 9000:9000 ibmcom/swift-ubuntu /bin/bash;
        echo "Created image ${name}";
        echo "Stopping image";
        docker stop ${name};
        echo "Removing image";
        docker rm ${name}
    }
    

    In the above example I open port 9000 which may not be required by your application.

    The last point I would like to make is the use of the build directory. I use a Makefile to do the build and in the Makefile I have the following section:

    BUILD_LINUX = "./.build-linux"
    BUILD_MACOS = "./.build-macos"
    UNAME = $(shell uname)
    
    ifeq (${UNAME}, Darwin)
    BUILD_PATH = ${BUILD_MACOS}
    else
    BUILD_PATH = ${BUILD_LINUX}
    endif
    
    xcode:
        @swift package --build-path ${BUILD_PATH} -Xlinker -L/usr/local/lib generate-xcodeproj
    
    build:
        rm -f Package.pins
        swift build --build-path ${BUILD_PATH} -Xlinker "-L/usr/local/lib"
    

    By using this construction the MacOS environment will use the directory .build-macos for the build and Linux will use the file .build-Linux. The file Package.pins is removed during build to ensure that some memory about versions to use is not transferred between the two environments.