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 imageFROM --platform=linux/x86-64 swift:5.5-focal as buildRUN apt-get update -y \ && apt-get install -y libsqlite3-devWORKDIR /buildCOPY . .RUN swift build \ --enable-test-discovery \ -c release \ -Xswiftc -g# Run imageFROM --platform=linux/x86-64 swift:5.5-focal-slimRUN useradd --user-group --create-home --home-dir /app vaporWORKDIR /appCOPY --from=build --chown=vapor:vapor /build/.build/release /appCOPY --from=build --chown=vapor:vapor /build/Public /app/Public# ARS RDS Environment ARGARG AWS_RDS_HOSTARG AWS_RDS_PORTARG AWS_RDS_USERARG AWS_RDS_PASSARG AWS_RDS_DB# SignInWithApple Environment ARGARG SIWA_IDARG SIWA_REDIRECT_URLARG SIWA_JWK_IDARG SIWA_PRIVATE_KEYARG SIWA_TEAM_IDARG SIWA_APP_BUNDLE_ID# Set EnvironmentRUN echo "SIWA_ID=${SIWA_ID}" > .env.developmentRUN echo "SIWA_REDIRECT_URL=${SIWA_REDIRECT_URL}" >> .env.developmentRUN echo "SIWA_JWK_ID=${SIWA_JWK_ID}" >> .env.developmentRUN echo "SIWA_PRIVATE_KEY=${SIWA_PRIVATE_KEY}" >> .env.developmentRUN echo "SIWA_TEAM_ID=${SIWA_TEAM_ID}" >> .env.developmentRUN echo "SIWA_APP_BUNDLE_ID=${SIWA_APP_BUNDLE_ID}" >> .env.developmentRUN echo "DB_HOST=${AWS_RDS_HOST}" >> .env.developmentRUN echo "DB_PORT=${AWS_RDS_PORT}" >> .env.developmentRUN echo "DB_USER=${AWS_RDS_USER}" >> .env.developmentRUN echo "DB_PASS=${AWS_RDS_PASS}" >> .env.developmentRUN echo "DB_NAME=${AWS_RDS_DB}" >> .env.developmentUSER vaporEXPOSE 8080ENTRYPOINT ["./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/bashecho "🐕 Build Docker"docker-compose -f local-docker-compose.yml buildecho "🐕 Start dependencies"docker-compose -f local-docker-compose.yml run --rmecho "🐕 Start Vapor App"docker-compose -f local-docker-compose.yml up travelcrumb-beta
Step 4. sh local.sh
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


