Skip to content

Commit 6e5080a

Browse files
committed
Add docker as run option
1 parent cc69597 commit 6e5080a

File tree

8 files changed

+104
-3
lines changed

8 files changed

+104
-3
lines changed

.dockerignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
node_modules
2+
Dockerfile*
3+
docker-compose*
4+
.dockerignore
5+
.git
6+
.gitignore
7+
README.md
8+
LICENSE
9+
.vscode
10+
Makefile
11+
helm-charts
12+
.env
13+
.editorconfig
14+
.idea
15+
coverage*

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ bun.lockb
33
.obsidian
44
.vscode
55
.idea
6+
.env

Dockerfile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# use the official Bun image
2+
# see all versions at https://hub.docker.com/r/oven/bun/tags
3+
FROM oven/bun:1 as base
4+
WORKDIR /usr/src/app
5+
6+
# install dependencies into temp directory
7+
# this will cache them and speed up future builds
8+
FROM base AS install
9+
RUN mkdir -p /temp/dev
10+
COPY package.json bun.lockb /temp/dev/
11+
RUN cd /temp/dev && bun install --frozen-lockfile
12+
13+
# install with --production (exclude devDependencies)
14+
RUN mkdir -p /temp/prod
15+
COPY package.json bun.lockb /temp/prod/
16+
RUN cd /temp/prod && bun install --frozen-lockfile --production
17+
18+
# copy node_modules from temp directory
19+
# then copy all (non-ignored) project files into the image
20+
FROM base AS prerelease
21+
COPY --from=install /temp/dev/node_modules node_modules
22+
COPY . .
23+
24+
# [optional] tests & build
25+
# ENV NODE_ENV=production
26+
# RUN bun test
27+
# RUN bun run build
28+
29+
# copy production dependencies and source code into final image
30+
FROM base AS release
31+
COPY --from=install /temp/prod/node_modules node_modules
32+
COPY --from=prerelease /usr/src/app/src/index.ts src/index.ts
33+
COPY --from=prerelease /usr/src/app/package.json .
34+
35+
# run the app
36+
USER bun
37+
EXPOSE 3000/tcp
38+
ENTRYPOINT [ "bun", "run", "start" ]

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
start:
2+
bun run start
3+
dcu:
4+
sudo docker-compose rm && sudo docker-compose up --build -d
5+
dcd:
6+
sudo docker-compose down

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Stub API
12
An application for create custom response.
23

34
## Installation
@@ -11,7 +12,7 @@ cd stub
1112
bun install
1213
bun run dev
1314
```
14-
2. Docker (todo)
15+
2. Run with Docker
1516
```
1617
docker compose up -d
1718
```

compose.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
version: "0.1"
2+
name: stub
3+
4+
services:
5+
api:
6+
container_name: stub
7+
build:
8+
context: .
9+
dockerfile: Dockerfile
10+
networks:
11+
- stub-network
12+
ports:
13+
- 3000:3000
14+
environment:
15+
- REDIS_URL=redis://redis:6379
16+
depends_on:
17+
- redis
18+
restart: on-failure
19+
20+
redis:
21+
container_name: redis
22+
image: redis:7.2-alpine
23+
ports:
24+
- 6379:6379
25+
restart: always
26+
networks:
27+
- stub-network
28+
volumes:
29+
- cache:/data
30+
31+
networks:
32+
stub-network:
33+
name: stub-network
34+
35+
volumes:
36+
cache:
37+
driver: local

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"scripts": {
3-
"dev": "bun run --hot src/index.ts"
3+
"dev": "bun run --hot src/index.ts",
4+
"start": "bun run src/index.ts"
45
},
56
"dependencies": {
67
"hono": "^3.12.7",

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Hono } from "hono";
22
import { getCookie } from "hono/cookie";
33
import { createClient } from "redis";
44

5-
const client = createClient();
5+
const client = createClient({
6+
url: process.env.REDIS_URL,
7+
});
68
client.on("error", (err) => console.log("Redis Client Error", err));
79
await client.connect();
810

0 commit comments

Comments
 (0)