diff --git a/Dockerfile b/Dockerfile index 570efe93..55c9074e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,81 @@ -FROM swift:4.2.4 +############################################################################################################################## +## ## +## We recommend building with buildx: ## +## ## +## // Note: you can use the standard `docker build` command, but there is no multi-CPU architecture support ## +## ## +## // Create buildx instance ## +## docker buildx create --driver docker-container --name builder --bootstrap --use ## +## ## +## // Login to Rregistry ## +## docker login [REGSITRY_ADDRESS] ## +## ## +## // Build the docker image (both x86 and amd64 are supported) ## +## docker buildx build --platform=linux/amd64,linux/arm64 --push -t [REGSITRY_ADDRESS]/REGSITRY_USERNAME/gitignore.io . ## +## ## +############################################################################################################################## + +# Build swift backend +FROM swift:5.6-focal AS swift-builder + +COPY . /gitignore.io + +WORKDIR /gitignore.io + +RUN set -ex \ + && apt update \ + && apt install libssl-dev -y \ + && swift package clean \ + && swift package update \ + && swift build -Xswiftc -static-stdlib -j $(nproc) -c release \ + && mv $(swift build -Xswiftc -static-stdlib -c release --show-bin-path)/Run /tmp/Run + +# Build node frontend +FROM node:lts AS node-builder + +COPY . /gitignore.io + +WORKDIR /gitignore.io + +RUN set -ex \ + && yarn install \ + && yarn build \ + && rm -rf node_modules + +# Build final image +FROM debian:stable-slim AS dest + WORKDIR /app + +# The environment variable is set to empty(use the default value) +ARG HOST_ORIGIN +ARG BASE_PREFIX +ARG GOOGLE_ANALYTICS_UID + +# Copy the project and remove the node frontend COPY . ./ -RUN git submodule update --init --recursive -RUN swift package clean -RUN swift build -c release -RUN mkdir /app/bin -RUN mv `swift build -c release --show-bin-path` /app/bin -EXPOSE 8080 -ENTRYPOINT ["/app/bin/release/Run", "serve", "-e", "prod", "-b", "0.0.0.0"] +COPY .git ./ + +# Install some necessary dependencies +RUN set -ex \ + && apt update \ + && apt install git ca-certificates libcurl4 dumb-init --no-install-recommends -y \ + && git submodule update --init --recursive \ + && rm -rf /app/Public /app/Resources \ + && apt autoremove -y \ + && apt autoclean -y + +# Copy all newly compiled files to the final image +COPY --from=swift-builder /tmp/Run /app/Run +COPY --from=node-builder /gitignore.io/Public /app/Public +COPY --from=node-builder /gitignore.io/Resources /app/Resources + +EXPOSE 8080/tcp + +# Add dump-init to ensure container can respond to exit signals +ENTRYPOINT ["/usr/bin/dumb-init", "--"] + +# System signals are taken over by dump-init, we can use `exec` to execute +# commands without worrying about signal forwarding and zombie processes +# See Also: https://docs.docker.com/engine/reference/builder/#cmd +CMD ["/app/Run", "serve", "-e", "prod", "-b", "0.0.0.0"] diff --git a/docker-compose.yml b/docker-compose.yml index c500a6b5..5769fe9a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.5" +version: "3.9" services: app: image: gitignore-io:latest @@ -7,4 +7,3 @@ services: dockerfile: Dockerfile ports: - 8080:8080 - command: vapor run