Get started developing Swift 3 on any platform supporting Docker. Using this you can easily develop server side Swift even on a Windows computer.
- docker
- docker-compose
$ docker-compose run app swift package init --type executable
SPM will generate a project from within docker container targeted the mounted volume. These files can now be accessed and edited from the host system.
Your root folder now looks like this.
.
├── Dockerfile
├── Package.swift
├── README.md
├── Sources
│ └── main.swift
├── Tests
└── docker-compose.yml
$ docker-compose up
Creating swiftdocker_app_1
Attaching to swiftdocker_app_1
app_1 | Compile Swift Module 'app' (1 sources)
app_1 | Linking ./.build/debug/app
app_1 | Hello, world!
swiftdocker_app_1 exited with code 0
Success!
Say we want to try out Vapor, the web framework for Swift.
First initialize a new SPM project, and confirm it all works with docker-compose up
Update your package file to look like this:
import PackageDescription
let package = Package(
name: "app",
dependencies: [
.Package(url: "https://github.com/vapor/vapor.git", majorVersion: 1, minor: 5)
]
)
Update main.swift
import Vapor
let drop = Droplet()
drop.get("/hello") { _ in
return "Hello Vapor"
}
drop.run()
By default Vapor runs on port 8080, so we will expose that to our host.
#docker-compose.yml
version: '2'
services:
app:
build: .
command: bash -c "swift build && ./.build/debug/app"
volumes:
- .:/app
ports:
- "8080:8080"
Now run
$ docker-compose up
You should see a bunch of output from the container while it fetches and compiles all dependencies.
It now boots up the server, and by visiting localhost:8080/hello
you should see activity in your console, and you will be greeted in your web browser.
From here on, start hacking in your favorite editor! Refer to docs at vapor.codes.
TBD
You probably want to build a different container with all the dependencies already isolated inside. At least build for production instead of debug. swift build --configuration release