Skip to content

Instantly share code, notes, and snippets.

@dmiedema
Created November 26, 2017 00:15
Show Gist options
  • Save dmiedema/49289f4d26a4b066ba8e15b685cf9e0f to your computer and use it in GitHub Desktop.
Save dmiedema/49289f4d26a4b066ba8e15b685cf9e0f to your computer and use it in GitHub Desktop.
Vapor/Docker Starter Template
FROM swift:latest
# Setup Prerequisits
USER root
RUN apt-get update \
&& apt-get install -y wget
RUN /bin/bash -c "$(wget -qO- https://apt.vapor.sh)"
RUN apt-get install -y vapor
# Create App Directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Copy Source Files
COPY . /usr/src/app
ENV PORT 3000
EXPOSE 3000
RUN vapor build
CMD [ "vapor", "run", "serve" ]
.PHONY: docker clean debug docker-debug docker-cleanup build xcode
docker:
docker build -t swift-server .
docker run -p 3000:3000 -d -t swift-server:latest
docker-debug: docker-cleanup docker
docker logs --follow $$(docker ps -a -q)
docker-cleanup:
docker rm -f $$(docker ps -a -q)
build:
vapor build
clean:
vapor clean
debug:
vapor serve
xcode:
vapor xcode

Prerequisites

Create a new Vapor Project & move into that folder

$ vapor new [project name]
$ cd [project name]

Copy Makefile and Dockerfile into the project directory

Run make docker

To see server logs run make docker-debug. Note that command fails if a docker image isn't currently running because it attempts to remove currently running image under the same name. Why? I was running into the problem of the port already being bound and adding the --rm flag to the docker run command didn't seem to help so I just used the suggestion to rm all running containers to fix it. However if no containers are running it fails. ¯\(ツ)/¯ not enough of a problem for me to really care and fix it yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment