Moving from python to swift using docker

I'm running several Python programs (that feed a db) using Docker, running on Ubuntu containers. As these programs have little to no dependencies, I would like to port these to Swift programs (speed gains as a first win).

Now, I've had a paid Apple Developer account for years, but had stopped paying, so now just a normal Developer account.

Questions:

  • Can I use Xcode to develop my Swift programs? Also without paying again?
  • What target should I use in Xcode? I'm not developing for Mac?
  • Any hints on the simplest skeleton I could use to start building from?

Yes you can use Xcode to write Swift programs. Create a SwiftPM app that works on macOS and as long as you stick to (most parts of) Foundation, it will run on Linux as well. You can also use VSCode with the Swift extension.

If you want to build a CLI then swift package init --type executable is the simplest thing to start with

3 Likes

Is there a place for me to look to for a "starter" Dockerfile that I can use for my Swift app, such that Github Actions can publish an image to DockerHub for me to run?

There are a number of ways to construct the Dockerfile, but Vapor's template has a fairly battle tested 'production ready' Dockerfile

1 Like

Thank you! And wow, that's a complicated Dockerfile (comparing to something similar for Python).

Well I mean you can just do

FROM swift
COPY .
swift run

But it depends on how build a final container you want, if you have any dependencies, how secure you want it etc. The Vapor Dockerfile is designed for running highly efficient, secure web severs that can handle real prod environments. So it may be overkill for what you're trying to do.

(One big benefit of the Vapor approach from a security point of view is that the final image does not contain a compiler or the Swift runtime outside of the static binary, so even if you managed to get remote code into the image, there's no way to compile or execute it since it's physically not possible)

3 Likes

I'm specifically not trying to run a web server, but want to run swift code in a container on a VM. It's basically a large While True loop that fetches new data regularly, and processes this to be stored in a MongoDB, so that's probably my only dependency.

Conceptually there's no difference - a web server is just a big while true loop that handles incoming requests

1 Like

So, @0xTim is technically correct and is probably responding to the characterization of the Vapor boilerplate as surprisingly complicated. Please allow me to suggest, however, that it's not actually complicated:

The Vapor Dockerfile really just says, "Look, here's what you need to have in order to compile and link this project, and then, here's what you need in order to run the project," as two separate targets. Presumably, one could run the thing on the same system that built the thing, but as a general rule one doesn't do development on a production machine, right?

In reading Docker files, the rule of thumb is sort of the opposite of YouTube, right? Read the comments first!

1 Like