簡単に次のノードのDockerFileを使用します.JSプロジェクト


Dockerは、開発者として我々の生活を容易にしました.ほんの少しのコマンドで、依存関係の退屈なインストールに対処することなく、あらゆる種類のプログラムやサービスを始めることができます.無数のDocker画像はDocker Hubで自由に利用可能です.かどうかを自分自身のオープンソースの統合&Strider , またはあなた自身のNPMのレジストリhttps://verdaccio.org/ , 可能性は無限大です.
しかし、これらは私が話していた大部分の既製のDocker画像です.しかし、あなた自身のプロジェクトのために、あなたはあなた自身のDockerイメージを構築しなければなりません.そして、この記事では、私はいくつかの単語でどのようにあなたの次のNODEJSアプリケーションをDockerイメージに置くことができるいくつかの単語を示して説明します.はい、どうぞ.
単純なDockerfile このように見えます.
# We use the latest nodejs@14 image as our base image
FROM node:14-alpine

# set the default NODE_NEV to production
ENV NODE ENV=production

# make sure everything happens inside the /app folder
WORKDIR/app

# now we cache the node_modules layer
COPY ["package.json", "package-lock.json", "./"]

# install your dependencies
RUN npm install

# copy the source
COPY /src .

# and start the app
CMD ["node", "server. js"]
しかし、もしあなたがそれを実行する前に、アプリケーションを最初に構築する必要がある場合は?を使ってこれを見ましょうmulti-stage build .
# We make use of ARG to set some variables that we
# can use in the Dockerfile
ARG node_version=14
ARG node_image=node:${node_version}-alpine

# STAGE 1: This is the "builder" stage where we build the
# application and give this step a name: "builder"
FROM $node_image as builder

ENV NODE ENV=production
WORKDIR/app

COPY ["package.json", "package-lock.json", "./"]

# compared to the first example we now install
# _all_ dependencies including devDependencies
RUN npm install

# copy the source
COPY /src .

# now build the app
RUN npm build

# STAGE 2: in this stage, we reduce the size of the
# image by only installing production dependencies
FROM $node_image as production

WORKDIR /app/

# install _only production_ dependencies to keep the
# docker image small
COPY --from=builder /app/package.json /app/package-lock.json ./
RUN npm install --production

# copy the build from the first stage (e.g. the dist folder)
COPY --from=builder /app/dist ./dist

# and start the bundled app
CMD ["node", "dist/index. js"]
これです.これで、マルチステージまたはあなたの次のプロジェクトのための単一ステージDockerFileの間で選択するオプションがあります.もちろん、我々はまだいくつかのことを最適化することができ、さまざまなアプローチを適用するが、それは多分私の別の話の一部です#90DaysOfProse challenge .

ステファン
ps :オリバーに感謝します.