本指南假設您已安裝 Docker Desktop。
Docker 是一個平台,用於將應用程式封裝並作為輕量、可攜式的容器執行,該容器封裝了所有必要的依賴項。
要容器化我們的應用程式,我們需要定義一個 Dockerfile
。此檔案包含初始化容器、將本機專案檔案複製到容器中、安裝依賴項,以及啟動應用程式的指令列表。
# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1 AS base
WORKDIR /usr/src/app
# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lock /temp/dev/
RUN cd /temp/dev && bun install --frozen-lockfile
# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json bun.lock /temp/prod/
RUN cd /temp/prod && bun install --frozen-lockfile --production
# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules
COPY . .
# [optional] tests & build
ENV NODE_ENV=production
RUN bun test
RUN bun run build
# copy production dependencies and source code into final image
FROM base AS release
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/app/index.ts .
COPY --from=prerelease /usr/src/app/package.json .
# run the app
USER bun
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "index.ts" ]
現在您已經有了 Docker 映像檔,讓我們看看 .dockerignore
,它的語法與 .gitignore
相同,您需要在這裡指定 Docker 建置的任何階段都不應包含的檔案/目錄。以下是一個忽略檔案的範例
node_modules
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
README.md
LICENSE
.vscode
Makefile
helm-charts
.env
.editorconfig
.idea
coverage*
我們現在將使用 docker build
將此 Dockerfile
轉換為 Docker 映像檔,這是一個自我包含的範本,其中包含執行應用程式所需的所有依賴項和組態。
-t
標記讓我們可以指定映像檔的名稱,而 --pull
告訴 Docker 自動下載基礎映像檔 (oven/bun
) 的最新版本。初始建置將花費較長時間,因為 Docker 將下載所有基礎映像檔和依賴項。
docker build --pull -t bun-hello-world .
[+] Building 0.9s (21/21) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 35B 0.0s
=> [internal] load metadata for docker.io/oven/bun:1 0.8s
=> [auth] oven/bun:pull token for registry-1.docker.io 0.0s
=> [base 1/2] FROM docker.io/oven/bun:1@sha256:373265748d3cd3624cb3f3ee6004f45b1fc3edbd07a622aeeec17566d2756997 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 155B 0.0s
# ...lots of commands...
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:360663f7fdcd6f11e8e94761d5592e2e4dfc8d167f034f15cd5a863d5dc093c4 0.0s
=> => naming to docker.io/library/bun-hello-world 0.0s
我們已經建置了一個新的 Docker 映像檔。現在讓我們使用該映像檔啟動一個實際運行的容器。
我們將使用 docker run
使用 bun-hello-world
映像檔啟動一個新容器。它將在分離模式 (-d
) 下運行,我們將容器的 3000 端口映射到本機的 3000 端口 (-p 3000:3000
)。
run
命令會印出代表容器 ID 的字串。
docker run -d -p 3000:3000 bun-hello-world
7f03e212a15ede8644379bce11a13589f563d3909a9640446c5bbefce993678d
容器現在在背景中運行。請造訪 localhost:3000。您應該會看到 Hello, World!
訊息。
若要停止容器,我們將使用 docker stop <container-id>
。
docker stop 7f03e212a15ede8644379bce11a13589f563d3909a9640446c5bbefce993678d
如果您找不到容器 ID,可以使用 docker ps
列出所有正在運行的容器。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7f03e212a15e bun-hello-world "bun run index.ts" 2 minutes ago Up 2 minutes 0.0.0.0:3000->3000/tcp flamboyant_cerf
就這樣!請參閱 Docker 文件 以瞭解更進階的用法。