Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: docker #14

Merged
merged 10 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Build and Deploy Docker to GHCR

# This workflow runs when any of the following occur:
# - A push is made to a branch called `main`
# - A tag starting with "v" is created
on:
push:
branches:
- main
tags:
- v*
env:
IMAGE_NAME: stoic_quotes
#
jobs:
# This pushes the image to GitHub Packages.
push:
runs-on: ubuntu-latest

permissions:
packages: write
contents: read

steps:
- uses: actions/checkout@v4

- name: Build image
run: docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}"

- name: Log in to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin

- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME

# This changes all uppercase characters to lowercase.
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')

# This strips the git ref prefix from the version.
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')

# This strips the "v" prefix from the tag name.
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')

# This uses the Docker `latest` tag convention.
[ "$VERSION" == "main" ] && VERSION=latest

echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION

29 changes: 29 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Build and Test Docker Image

on:
push:
branches:
- main
pull_request: {}
workflow_dispatch: null

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build Docker image
run: docker build -t stoic_quotes .

- name: Run Docker
run: docker run -d --name stoic_quotes -p 3000:3000 stoic_quotes

- name: Display Docker container logs
run: docker logs stoic_quotes

- name: Test Docker
run: curl --fail localhost:3000/ || exit 1

48 changes: 48 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Start from the official Rust image
FROM rust:latest as builder

# Install musl-tools
RUN apt-get update && apt-get install -y musl-tools && rm -rf /var/lib/apt/lists/*

# Target the musl architecture for a fully static binary
RUN rustup target add x86_64-unknown-linux-musl

# Set the working directory
WORKDIR /usr/src/app

# Add labels for OCI annotations
LABEL org.opencontainers.image.source="https://github.com/storopoli/stoic-quotes" \
org.opencontainers.image.description="Stoic Quotes" \
org.opencontainers.image.licenses="MIT"

# Copy project's Cargo.toml file
COPY ./Cargo.toml ./

# This dummy build is to cache dependencies so they don't need to be rebuilt
# every time your source changes
RUN mkdir src/ && \
echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs && \
cargo build --release --target x86_64-unknown-linux-musl && \
rm -f target/x86_64-unknown-linux-musl/release/deps/stoic*

# Copy project's source code and other relevant folders to the Docker image
COPY ./src ./src
COPY ./assets ./assets
COPY ./data ./data
COPY ./templates ./templates

# Build application for release target musl
RUN cargo build --release --target x86_64-unknown-linux-musl

# Start a new stage from a slim version of Debian to reduce the size of the final image
FROM debian:buster-slim

# Copy the binary from the builder stage to the new stage
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/stoic-quotes /usr/local/bin/stoic-quotes

# Expose port 3000
EXPOSE 3000

# Command to run the binary
CMD ["stoic-quotes"]

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ $ curl stoicquotes.io
- Seneca
```

## Docker

You can also deploy it using Docker from the GitHub Container Registry:

```bash
docker pull ghcr.io/storopoli/stoic_quotes:latest

docker run -d --name stoic_quotes -p 443:3000 stoic_quotes
```

By default it exposes port 3000 on the `axum` server.
You can safely map to HTTP (port 80) or HTTPS (port 443).

## License

This content is licensed under a
Expand Down