refactor: reduce docker image size

This commit is contained in:
Juan Rodriguez
2024-07-12 22:53:48 +02:00
parent 36a06ac670
commit 115bbf7366
4 changed files with 66 additions and 64 deletions
-1
View File
@@ -1,6 +1,5 @@
.git .git
/bin/ /bin/
/.shards/ /.shards/
/bruno/
/spec/ /spec/
/sqlite/ /sqlite/
+10 -3
View File
@@ -39,6 +39,15 @@ jobs:
VERSION=$(echo $VERSION | tr -d '\n\r') VERSION=$(echo $VERSION | tr -d '\n\r')
echo "RELEASE_TAG=$VERSION" >> $GITHUB_ENV 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 - name: Build and push image
id: push id: push
uses: docker/build-push-action@v5.0.0 uses: docker/build-push-action@v5.0.0
@@ -46,9 +55,7 @@ jobs:
context: . context: .
push: true push: true
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
tags: | tags: sjdonado/bit:${{ env.TAGS }}
sjdonado/bit:latest
${{ github.event_name == 'release' && env.RELEASE_TAG && 'sjdonado/bit:${{ env.RELEASE_TAG }}' || '' }}
- name: Attest - name: Attest
uses: actions/attest-build-provenance@v1 uses: actions/attest-build-provenance@v1
+19 -9
View File
@@ -1,5 +1,6 @@
FROM alpine:edge as base FROM alpine:edge AS build
ENV ENV=production
WORKDIR /usr/src/app WORKDIR /usr/src/app
RUN apk update && apk add --no-cache \ RUN apk update && apk add --no-cache \
@@ -9,20 +10,29 @@ RUN apk update && apk add --no-cache \
sqlite-dev \ sqlite-dev \
openssl-dev openssl-dev
FROM base AS build COPY . .
ENV ENV=production
COPY . .
RUN shards install RUN shards install
RUN shards build --progress RUN shards build --release --no-debug
FROM alpine:edge AS runtime
ENV ENV=production
WORKDIR /usr/src/app
RUN apk add --no-cache \
gc \
pcre2 \
libevent \
yaml \
sqlite-libs \
openssl
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/db db
COPY --from=build /usr/src/app/data data COPY --from=build /usr/src/app/data data
COPY --from=build /usr/src/app/bin /usr/local/bin COPY --from=build /usr/src/app/bin /usr/local/bin
COPY --from=build /usr/src/app/data /usr/local/data
EXPOSE 4000/tcp EXPOSE 4000/tcp
CMD ["bit"] CMD ["bit"]
+37 -51
View File
@@ -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 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/general) [![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/general) [![Docker Image Size](https://img.shields.io/docker/image-size/sjdonado/bit/latest)](https://hub.docker.com/repository/docker/sjdonado/bit)
# Benchmark # Benchmark
@@ -39,7 +39,7 @@ Average Response Time: 12.37 µs
# Self-hosted # Self-hosted
- Run via docker-compose ## Run via docker-compose
```bash ```bash
docker-compose up docker-compose up
@@ -48,7 +48,7 @@ docker-compose exec -it app migrate
docker-compose exec -it app cli --create-user=Admin docker-compose exec -it app cli --create-user=Admin
``` ```
- Run via docker cli ## Run via docker cli
```bash ```bash
docker run \ docker run \
@@ -63,7 +63,7 @@ docker exec -it bit migrate
docker exec -it bit cli --create-user=Admin docker exec -it bit cli --create-user=Admin
``` ```
- Dokku ## Dokku
```dockerfile ```dockerfile
FROM sjdonado/bit FROM sjdonado/bit
@@ -93,11 +93,9 @@ dokku run bit cli --create-user=Admin
1. **Ping the API** 1. **Ping the API**
- **Endpoint**: `/api/ping` - Endpoint: `GET /api/ping`
- **HTTP Method**: GET - Payload: None
- **Description**: Ping the API to check if it's running - Response Example
- **Payload**: -
- **Response Example**:
```json ```json
{ {
"message": "pong" "message": "pong"
@@ -106,12 +104,10 @@ dokku run bit cli --create-user=Admin
2. **Retrieve a link by its slug** 2. **Retrieve a link by its slug**
- **Endpoint**: `/:slug` - Endpoint: `GET /:slug`
- **HTTP Method**: GET - Headers: `X-Api-Key`
- **Description**: Retrieve a link by its slug - Payload: None
- **Payload**: - - Response Example
- **Headers**: `X-Api-Key`
- **Response Example**:
```json ```json
{ {
"data": { "data": {
@@ -135,12 +131,10 @@ dokku run bit cli --create-user=Admin
3. **Retrieve all links** 3. **Retrieve all links**
- **Endpoint**: `/api/links` - Endpoint: `GET /api/links`
- **HTTP Method**: GET - Headers: `X-Api-Key`
- **Description**: Retrieve all links - Payload: None
- **Payload**: - - Response Example
- **Headers**: `X-Api-Key`
- **Response Example**:
```json ```json
{ {
"data": [ "data": [
@@ -166,12 +160,10 @@ dokku run bit cli --create-user=Admin
4. **Retrieve a link by its ID** 4. **Retrieve a link by its ID**
- **Endpoint**: `/api/links/:id` - Endpoint: `GET /api/links/:id`
- **HTTP Method**: GET - Headers: `X-Api-Key`
- **Description**: Retrieve a link by its ID - Payload: None
- **Payload**: - - Response Example
- **Headers**: `X-Api-Key`
- **Response Example**:
```json ```json
{ {
"data": { "data": {
@@ -195,17 +187,15 @@ dokku run bit cli --create-user=Admin
5. **Create a new link** 5. **Create a new link**
- **Endpoint**: `/api/links` - Endpoint\*\*: `POST /api/links`
- **HTTP Method**: POST - Payload:
- **Description**: Create a new link
- **Payload**:
```json ```json
{ {
"url": "https://example.com" "url": "https://example.com"
} }
``` ```
- **Headers**: `X-Api-Key` - Headers: `X-Api-Key`
- **Response Example**: - Response Example:
```json ```json
{ {
"data": { "data": {
@@ -219,17 +209,15 @@ dokku run bit cli --create-user=Admin
6. **Update an existing link by its ID** 6. **Update an existing link by its ID**
- **Endpoint**: `/api/links/:id` - Endpoint: `PUT /api/links/:id`
- **HTTP Method**: PUT - Payload:
- **Description**: Update an existing link by its ID
- **Payload**:
```json ```json
{ {
"url": "https://newexample.com" "url": "https://newexample.com"
} }
``` ```
- **Headers**: `X-Api-Key` - Headers: `X-Api-Key`
- **Response Example**: - Response Example:
```json ```json
{ {
"data": { "data": {
@@ -243,12 +231,10 @@ dokku run bit cli --create-user=Admin
7. **Delete a link by its ID** 7. **Delete a link by its ID**
- **Endpoint**: `/api/links/:id` - Endpoint: `DELETE /api/links/:id`
- **HTTP Method**: DELETE - Payload: None
- **Description**: Delete a link by its ID - Headers: `X-Api-Key`
- **Payload**: - - Response Example:
- **Headers**: `X-Api-Key`
- **Response Example**:
```json ```json
{ {
"message": "Link deleted" "message": "Link deleted"
@@ -267,7 +253,7 @@ Options:
# Development # Development
1. **Installation** ## Installation
```bash ```bash
brew tap amberframework/micrate brew tap amberframework/micrate
@@ -279,19 +265,19 @@ shards run migrate
shards run bit shards run bit
``` ```
2. **Generate the `X-Api-Key`** ## Generate the `X-Api-Key`
```bash ```bash
shards run cli -- --create-user=Admin shards run cli -- --create-user=Admin
``` ```
# Run tests ## Run tests
```bash ```bash
ENV=test crystal spec ENV=test crystal spec
``` ```
# Contributing ## Contributing
1. Fork it (<https://github.com/sjdonado/bit/fork>) 1. Fork it (<https://github.com/sjdonado/bit/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`) 2. Create your feature branch (`git checkout -b my-new-feature`)