为什么选择docker
参考官方文档:https://docs.docker.com/get-started/overview/#what-can-i-use-docker-for
容器管理
Dockerfile
下面代码是一个镜像的Dockerfile文件,对里面的内容做一个简单的介绍
# FROM + 基础镜像,新镜像基于该镜像进行定制
FROM node:12-alpine
# WORKDIR指定容器内工作目录
WORKDIR /usr/src/app
# ARG/ENV 设置环境变量,方便后续调用
ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
# COPY 将当前目录或文件复制到容器工作目录下
COPY package.json /usr/src/app/
# RUN build过程中需要执行的命令
RUN npm install --registry=https://registry.npm.taobao.org
COPY . /usr/src/app
# CMD ,在docker run时执行的命令,dockerfile中只能有一个CMD
CMD [ "npm", "start" ]
ENV BIND_HOST 0.0.0.0
ENV BIND_PORT 3600
开始build镜像
➜ docker build -f=Dockerfile -t=test:0.1 .
Sending build context to Docker daemon 54.93MB
Step 1/10 : FROM node:12-alpine
12-alpine: Pulling from library/node
cbdbe7a5bc2a: Pull complete
091983d60f65: Pull complete
7bacc183740f: Pull complete
c4a5a5d270d4: Pull complete
Digest: sha256:9623cd396644f9b2e595d833dc0188a880333674488d939338ab5fde10ef7c43
Status: Downloaded newer image for node:12-alpine
---> 18f4bc975732
Step 2/10 : WORKDIR /usr/src/app
---> Running in b68feb1e9228
Removing intermediate container b68feb1e9228
---> ee7553e2086e
Step 3/10 : ARG NODE_ENV
---> Running in 5554f8f5cb85
Removing intermediate container 5554f8f5cb85
---> f277df808e93
Step 4/10 : ENV NODE_ENV $NODE_ENV
---> Running in 275d18fa60ce
Removing intermediate container 275d18fa60ce
---> e35632cba70c
Step 5/10 : COPY package.json /usr/src/app/
---> 0e7abb2cf497
Step 6/10 : RUN npm install --registry=https://registry.npm.taobao.org
---> Running in be71b8114612
npm WARN deprecated chokidar@2.1.8: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecated
> nodemon@1.19.4 postinstall /usr/src/app/node_modules/nodemon
> node bin/postinstall || exit 0
Love nodemon? You can now support the project via the open collective:
> https://opencollective.com/nodemon/donate
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN ohif-redir@1.0.0 No description
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
added 486 packages from 336 contributors in 8.447s
4 packages are looking for funding
run `npm fund` for details
Removing intermediate container be71b8114612
---> b89634a00e5b
Step 7/10 : COPY . /usr/src/app
---> ab58cc1736a5
Step 8/10 : CMD [ "npm", "start" ]
---> Running in