README.md 2.62 KB
Newer Older
Drew's avatar
Drew committed
1 2 3 4 5
# Official repository

https://code.sealedabstract.com/drewcrawford/swift-linux

# swift-linux
Drew's avatar
Drew committed
6 7 8 9 10 11 12 13 14 15 16

This is the smallest Swift Docker image that I know how to make.  I use this image as a production base for Swift language projects.

## Rationale

The "supported" Swift configuration is Ubuntu.  However, I run Debian, and prefer more minimal Docker images in general.  So, I set out to create a "proper" minimal Debian image.

## Use

Just pull from drewcrawford/swift.  It's installed to `/usr/local`.

Drew's avatar
Drew committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
Then you can compile swift programs, for example, 

```
$ docker run -it drewcrawford/swift:latest
# echo 'print("hello world")' > test.swift
# swiftc tests.swift
# exit
$ docker cp 4afdd757d428:test .
```


Now you can build a docker image with only your program:

```
FROM drewcrawford/swift-runtime:latest
ADD test
ENTRYPOINT ["test"]
```

Drew's avatar
Drew committed
36 37
## Build

Drew's avatar
Drew committed
38 39 40 41 42 43 44 45 46 47
You can build the compiler with `docker build .`.  You may want to up the number of cores if you're using VirtualBox, because it takes a long time single-threaded.

After building the swift image, you can build the separate swift-runtime image via

```
id=$(docker create drewcrawford/swift:latest)
docker cp $id:usr/local/lib/swift - > swiftlibs.tar
docker rm -v $id
docker build -f Runtime.dockerfile .
```
Drew's avatar
Drew committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

### Source

Here's a riddle: if sourcecode exists on GitHub, and nobody compiles it, is the project open-source?

Answer: We compile it.  So I don't know.

The Swift build system is kind of convoluted, and building an installable package is not documented anywhere (that I can find anyway).  I guess we'll give clattner a pass for being awesome otherwise, but it is hairy.

I dove into the build system, and worked out what (I think) they do to build a tarball, and here it is!  This image should build, run, test, and validate Swift exactly as upstream does.  Then we just turn around and install that tarball to `/usr/local`.

Since we build from source, you could modify this image to apply your own custom patches before building if you like.  As such this Dockerfile may be useful as a reference for a workflow for developing Swift itself.

## FAQ

### Q: Can't you use Alpine?

A: [No.](https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20151228/000653.html)

### Q: How large is this image?

Drew's avatar
Drew committed
69
A: 664.6 MB, at the time of this writing.  125.1 of that is Debian.  We uninstall all the build dependencies, so I don't know how to make it smaller.
Drew's avatar
Drew committed
70

Drew's avatar
Drew committed
71
Note that the distributable `swift-runtime` image is only 187.9 MB.
Drew's avatar
Drew committed
72 73 74

### Q: Do I need this image to run Swift binaries?

Drew's avatar
Drew committed
75
A: Probably not.  You probably only need the (smaller) `swift-runtime` image for containers that are supposed to contain compiled Swift code.