How to Use Docker with Vapor Server-Side Swift on Local Development

How to use Docker on local Vapor server for Swift backend development

I wrote about How to deploy Vapor App(Server-Side-Swift) to the AWS

This post is about setting on a local server using Docker. (Before you read this post, I highly recommend Server Side Swift – Vapor book.)

 

Step 1. Create local.Dockerfile

Look at the last command.

CMD [“serve”, “–env”, “local”, “–hostname”, “0.0.0.0”, “–port”, “8080”]

<- I set —env local.

# Build image
FROM --platform=linux/x86-64 swift:5.5-focal as build

RUN apt-get update -y \
    && apt-get install -y libsqlite3-dev

WORKDIR /build

COPY . .

RUN swift build \
    --enable-test-discovery \
    -c release \
    -Xswiftc -g


# Run image
FROM --platform=linux/x86-64 swift:5.5-focal-slim

RUN useradd --user-group --create-home --home-dir /app vapor

WORKDIR /app

COPY --from=build --chown=vapor:vapor /build/.build/release /app
COPY --from=build --chown=vapor:vapor /build/Public /app/Public

# ARS RDS Environment ARG
ARG AWS_RDS_HOST
ARG AWS_RDS_PORT
ARG AWS_RDS_USER
ARG AWS_RDS_PASS
ARG AWS_RDS_DB

# SignInWithApple Environment ARG
ARG SIWA_ID
ARG SIWA_REDIRECT_URL
ARG SIWA_JWK_ID
ARG SIWA_PRIVATE_KEY
ARG SIWA_TEAM_ID
ARG SIWA_APP_BUNDLE_ID

# Set Environment
RUN echo "SIWA_ID=${SIWA_ID}" > .env.development
RUN echo "SIWA_REDIRECT_URL=${SIWA_REDIRECT_URL}" >> .env.development
RUN echo "SIWA_JWK_ID=${SIWA_JWK_ID}" >> .env.development
RUN echo "SIWA_PRIVATE_KEY=${SIWA_PRIVATE_KEY}" >> .env.development
RUN echo "SIWA_TEAM_ID=${SIWA_TEAM_ID}" >> .env.development
RUN echo "SIWA_APP_BUNDLE_ID=${SIWA_APP_BUNDLE_ID}" >> .env.development

RUN echo "DB_HOST=${AWS_RDS_HOST}" >> .env.development
RUN echo "DB_PORT=${AWS_RDS_PORT}" >> .env.development
RUN echo "DB_USER=${AWS_RDS_USER}" >> .env.development
RUN echo "DB_PASS=${AWS_RDS_PASS}" >> .env.development
RUN echo "DB_NAME=${AWS_RDS_DB}" >> .env.development

USER vapor

EXPOSE 8080

ENTRYPOINT ["./Run"]
CMD ["serve", "--env", "local", "--hostname", "0.0.0.0", "--port", "8080"]

 

Step 2. local-docker-compose.yml

version: '3.7'
services:
  travelcrumb-beta:
    depends_on:
      - postgres
    build:
      context: .
      args:
        AWS_RDS_HOST: postgres
        AWS_RDS_PORT: 5432
        AWS_RDS_USER: shawn
        AWS_RDS_PASS: 1234
        AWS_RDS_DB: test_db
        SIWA_ID: xxxx.xxx.xxx
        SIWA_REDIRECT_URL: localhost:8080
        SIWA_JWK_ID: xxxxx
        SIWA_PRIVATE_KEY: xxxx
        SIWA_TEAM_ID: xxxx
        SIWA_APP_BUNDLE_ID: xxxxx
      dockerfile: local.Dockerfile
    ports:
      - '8080:8080'

  postgres:
    image: postgres
    environment:
      POSTGRES_USER: shawn
      POSTGRES_PASSWORD: 1234
      POSTGRES_DB: test_db

  start_dependencies:
    image: dadarek/wait-for-dependencies
    depends_on:
      - postgres
    command: postgres:5432

 

Step 3. local.sh

#!/bin/bash
echo "🐕 Build Docker"
docker-compose -f local-docker-compose.yml build
echo "🐕 Start dependencies"
docker-compose -f local-docker-compose.yml run --rm
echo "🐕 Start Vapor App"
docker-compose -f local-docker-compose.yml up travelcrumb-beta

 

Step 4. sh local.sh

e9b3b 4baee screenshot2022 08 08at11.39.56pm

Now you can set up all you need to install for running the Vapor server on your local machine. Without Docker, You have to run and stop DB every time when you run a server on your local machine.

Thanks for reading my post

Discover more from Shawn

Subscribe now to keep reading and get access to the full archive.

Continue reading