Skip to content

Commit

Permalink
refactor: reduce docker image size
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 12, 2024
1 parent 36a06ac commit 115bbf7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.git
/bin/
/.shards/
/bruno/
/spec/
/sqlite/
13 changes: 10 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ jobs:
VERSION=$(echo $VERSION | tr -d '\n\r')
echo "RELEASE_TAG=$VERSION" >> $GITHUB_ENV
- name: Set tags
id: set_tags
run: |
if [[ "${{ github.event_name }}" == "release" ]]; then
echo "TAGS=latest,${{ env.RELEASE_TAG }}" >> $GITHUB_ENV
else
echo "TAGS=latest" >> $GITHUB_ENV
fi
- name: Build and push image
id: push
uses: docker/build-push-action@v5.0.0
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: |
sjdonado/bit:latest
${{ github.event_name == 'release' && env.RELEASE_TAG && 'sjdonado/bit:${{ env.RELEASE_TAG }}' || '' }}
tags: sjdonado/bit:${{ env.TAGS }}

- name: Attest
uses: actions/attest-build-provenance@v1
Expand Down
26 changes: 18 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FROM alpine:edge as base
FROM alpine:edge AS build

ENV ENV=production
WORKDIR /usr/src/app

RUN apk update && apk add --no-cache \
Expand All @@ -9,20 +10,29 @@ RUN apk update && apk add --no-cache \
sqlite-dev \
openssl-dev

FROM base AS build
COPY . .

RUN shards install
RUN shards build --release --no-debug

FROM alpine:edge AS runtime

ENV ENV=production
WORKDIR /usr/src/app

COPY . .
RUN apk add --no-cache \
gc \
pcre2 \
libevent \
yaml \
sqlite-libs \
openssl

RUN shards install
RUN shards build --progress
RUN mkdir -p sqlite

FROM base AS release
RUN mkdir -p /usr/src/app/sqlite
COPY --from=build /usr/src/app/db db
COPY --from=build /usr/src/app/data data
COPY --from=build /usr/src/app/bin /usr/local/bin
COPY --from=build /usr/src/app/data /usr/local/data

EXPOSE 4000/tcp
CMD ["bit"]
88 changes: 37 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Docker Pulls](https://img.shields.io/docker/pulls/sjdonado/bit.svg)](https://hub.docker.com/repository/docker/sjdonado/bit/general)
[![Docker Stars](https://img.shields.io/docker/stars/sjdonado/bit.svg)](https://hub.docker.com/repository/docker/sjdonado/bit/general)
[![Docker Image Size](https://img.shields.io/docker/image-size/sjdonado/bit/latest)](https://hub.docker.com/repository/docker/sjdonado/bit/general)
[![Docker Pulls](https://img.shields.io/docker/pulls/sjdonado/bit.svg)](https://hub.docker.com/repository/docker/sjdonado/bit)
[![Docker Stars](https://img.shields.io/docker/stars/sjdonado/bit.svg)](https://hub.docker.com/repository/docker/sjdonado/bit)
[![Docker Image Size](https://img.shields.io/docker/image-size/sjdonado/bit/latest)](https://hub.docker.com/repository/docker/sjdonado/bit)

# Benchmark

Expand Down Expand Up @@ -39,7 +39,7 @@ Average Response Time: 12.37 µs

# Self-hosted

- Run via docker-compose
## Run via docker-compose

```bash
docker-compose up
Expand All @@ -48,7 +48,7 @@ docker-compose exec -it app migrate
docker-compose exec -it app cli --create-user=Admin
```

- Run via docker cli
## Run via docker cli

```bash
docker run \
Expand All @@ -63,7 +63,7 @@ docker exec -it bit migrate
docker exec -it bit cli --create-user=Admin
```

- Dokku
## Dokku

```dockerfile
FROM sjdonado/bit
Expand Down Expand Up @@ -93,11 +93,9 @@ dokku run bit cli --create-user=Admin

1. **Ping the API**

- **Endpoint**: `/api/ping`
- **HTTP Method**: GET
- **Description**: Ping the API to check if it's running
- **Payload**: -
- **Response Example**:
- Endpoint: `GET /api/ping`
- Payload: None
- Response Example
```json
{
"message": "pong"
Expand All @@ -106,12 +104,10 @@ dokku run bit cli --create-user=Admin

2. **Retrieve a link by its slug**

- **Endpoint**: `/:slug`
- **HTTP Method**: GET
- **Description**: Retrieve a link by its slug
- **Payload**: -
- **Headers**: `X-Api-Key`
- **Response Example**:
- Endpoint: `GET /:slug`
- Headers: `X-Api-Key`
- Payload: None
- Response Example
```json
{
"data": {
Expand All @@ -135,12 +131,10 @@ dokku run bit cli --create-user=Admin

3. **Retrieve all links**

- **Endpoint**: `/api/links`
- **HTTP Method**: GET
- **Description**: Retrieve all links
- **Payload**: -
- **Headers**: `X-Api-Key`
- **Response Example**:
- Endpoint: `GET /api/links`
- Headers: `X-Api-Key`
- Payload: None
- Response Example
```json
{
"data": [
Expand All @@ -166,12 +160,10 @@ dokku run bit cli --create-user=Admin

4. **Retrieve a link by its ID**

- **Endpoint**: `/api/links/:id`
- **HTTP Method**: GET
- **Description**: Retrieve a link by its ID
- **Payload**: -
- **Headers**: `X-Api-Key`
- **Response Example**:
- Endpoint: `GET /api/links/:id`
- Headers: `X-Api-Key`
- Payload: None
- Response Example
```json
{
"data": {
Expand All @@ -195,17 +187,15 @@ dokku run bit cli --create-user=Admin

5. **Create a new link**

- **Endpoint**: `/api/links`
- **HTTP Method**: POST
- **Description**: Create a new link
- **Payload**:
- Endpoint\*\*: `POST /api/links`
- Payload:
```json
{
"url": "https://example.com"
}
```
- **Headers**: `X-Api-Key`
- **Response Example**:
- Headers: `X-Api-Key`
- Response Example:
```json
{
"data": {
Expand All @@ -219,17 +209,15 @@ dokku run bit cli --create-user=Admin

6. **Update an existing link by its ID**

- **Endpoint**: `/api/links/:id`
- **HTTP Method**: PUT
- **Description**: Update an existing link by its ID
- **Payload**:
- Endpoint: `PUT /api/links/:id`
- Payload:
```json
{
"url": "https://newexample.com"
}
```
- **Headers**: `X-Api-Key`
- **Response Example**:
- Headers: `X-Api-Key`
- Response Example:
```json
{
"data": {
Expand All @@ -243,12 +231,10 @@ dokku run bit cli --create-user=Admin

7. **Delete a link by its ID**

- **Endpoint**: `/api/links/:id`
- **HTTP Method**: DELETE
- **Description**: Delete a link by its ID
- **Payload**: -
- **Headers**: `X-Api-Key`
- **Response Example**:
- Endpoint: `DELETE /api/links/:id`
- Payload: None
- Headers: `X-Api-Key`
- Response Example:
```json
{
"message": "Link deleted"
Expand All @@ -267,7 +253,7 @@ Options:
# Development
1. **Installation**
## Installation
```bash
brew tap amberframework/micrate
Expand All @@ -279,19 +265,19 @@ shards run migrate
shards run bit
```

2. **Generate the `X-Api-Key`**
## Generate the `X-Api-Key`

```bash
shards run cli -- --create-user=Admin
```

# Run tests
## Run tests

```bash
ENV=test crystal spec
```

# Contributing
## Contributing

1. Fork it (<https://github.com/sjdonado/bit/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
Expand Down

0 comments on commit 115bbf7

Please sign in to comment.