Docker 不同项目的多阶段构建
Docker multi-stage-build with different project
我们目前正在处理两个项目:
1 个基于 C++ 的项目
2 个基于 Nodejs 的项目
这两个项目是分开的,这意味着它们有不同的代码库(git 存储库)和工作目录。
C++ 项目将生成一个节点绑定文件 .node
,Nodejs 项目将使用该文件。
然后我们尝试为具有多阶段的 Nodejs 项目构建一个 docker 图像,如下所示:
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
copy (?) . #1 copy the c++ source codes
RUN make
from node:10
WORKDIR /app
copy (?) . #1 copy the nodejs cource codes
RUN npm install
copy --from=u /app/dist/xx.node ./lib/
node index.js
我将通过 docker build -t xx (?) #2
构建图像。
然而,如 docker 文件和命令中所述,如何设置 context
目录(参见评论 #2)?因为它会影响 docker 文件中的路径(参见注释 #1)。
还有上面的应该放在哪个项目里面呢dockerfile
?
正在使用 git 个子模块创建部署项目
如何使用 git submodules 创建部署项目?
此项目仅用于构建 docker 图像,并包含 Dockerfile 和您的两个项目作为 git 个子模块。
由于您不只是复制这两个项目,而是使用 git 管理它们,因此您始终可以使用 git submodules update --remote
使它们保持最新,但请注意 this leaves your submodule in a detached head state。但是,只要您不尝试从部署项目更新 C++ 项目或节点项目,这就不是问题。
您可以使用以下命令创建项目:
mkdir deploy_project && cd deploy_project
git init
git submodule add git@your-gitserver.com:YourName/YourCppProject.git cpp_project
git submodule add git@your-gitserver.com:YourName/YourNodeProject.git nodejs_project
然后您可以简单地将子项目的路径添加到您的 docker 文件中,并在部署项目的根目录中构建映像。
docker文件看起来像这样
FROM ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
COPY cpp_project/ . #1 copy the c++ source codes
RUN make
FROM node:10
WORKDIR /app
COPY nodejs_project/ . #1 copy the nodejs cource codes
RUN npm install
COPY --from=u /app/dist/xx.node ./lib/
您可以使用 ADD 命令(她的上下文监视 Dockerfile 放置的主机目录。它会将主机中与 Dockerfile 相同目录中的所有内容(在本例中为 cpp_app 目录的内容)复制到 docker容器。
...
ADD cpp_app /place/to/build
WORKDIR /place/to/build
RUN make
RUN mv result_file /place/where/result_file/have/to/be
WORKDIR /place/where/result_file/have/to/be
... execute your nodejs stuff
您将有两个选择,因为限制因素是 Docker 只允许 copying from the same directory as the Dockerfile:
创建新存储库
您可以创建一个新的存储库并将您的存储库用作子模块,或者仅用于 Docker 文件(这样您就必须在构建时将两个存储库复制到根文件夹中)。最后你要实现的是如下结构:
/ (root)
|-- C-plus-plus-Repo
|-- |-- <Files>
|-- Node-Repo
|-- |-- <Files>
|-- Dockerfile
您可以使用以下方法构建您的项目:
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
#1 copy the c++ source files
copy ./C-plus-plus-Repo .
RUN make
from node:10
WORKDIR /app
#1 copy the nodejs cource codes
copy ./Node-Repo .
RUN npm install
copy --from=u /app/dist/xx.node ./lib/
node index.js
在根目录执行:
docker build -t xx .
额外构建您的暂存容器
Docker 允许 copy from an external container as stage.
因此您可以在 C++ 存储库根目录中构建 C++ 容器
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
#1 copy the c++ source files
copy . .
RUN make
并标记它:
# Build your C++ Container in root of the c++ repo
docker build . -t c-stage
然后使用标签(在您的节点 Repo 根目录中)从中复制文件:
from node:10
WORKDIR /app
#1 copy the nodejs source files
copy . .
RUN npm install
# Use the Tag-Name of the already build container "c-stage"
copy --from=c-stage /app/dist/xx.node ./lib/
node index.js
两个构建步骤都可以从各自的 repo 根目录执行。
我们目前正在处理两个项目:
1 个基于 C++ 的项目
2 个基于 Nodejs 的项目
这两个项目是分开的,这意味着它们有不同的代码库(git 存储库)和工作目录。
C++ 项目将生成一个节点绑定文件 .node
,Nodejs 项目将使用该文件。
然后我们尝试为具有多阶段的 Nodejs 项目构建一个 docker 图像,如下所示:
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
copy (?) . #1 copy the c++ source codes
RUN make
from node:10
WORKDIR /app
copy (?) . #1 copy the nodejs cource codes
RUN npm install
copy --from=u /app/dist/xx.node ./lib/
node index.js
我将通过 docker build -t xx (?) #2
构建图像。
然而,如 docker 文件和命令中所述,如何设置 context
目录(参见评论 #2)?因为它会影响 docker 文件中的路径(参见注释 #1)。
还有上面的应该放在哪个项目里面呢dockerfile
?
正在使用 git 个子模块创建部署项目
如何使用 git submodules 创建部署项目?
此项目仅用于构建 docker 图像,并包含 Dockerfile 和您的两个项目作为 git 个子模块。
由于您不只是复制这两个项目,而是使用 git 管理它们,因此您始终可以使用 git submodules update --remote
使它们保持最新,但请注意 this leaves your submodule in a detached head state。但是,只要您不尝试从部署项目更新 C++ 项目或节点项目,这就不是问题。
您可以使用以下命令创建项目:
mkdir deploy_project && cd deploy_project
git init
git submodule add git@your-gitserver.com:YourName/YourCppProject.git cpp_project
git submodule add git@your-gitserver.com:YourName/YourNodeProject.git nodejs_project
然后您可以简单地将子项目的路径添加到您的 docker 文件中,并在部署项目的根目录中构建映像。
docker文件看起来像这样
FROM ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
COPY cpp_project/ . #1 copy the c++ source codes
RUN make
FROM node:10
WORKDIR /app
COPY nodejs_project/ . #1 copy the nodejs cource codes
RUN npm install
COPY --from=u /app/dist/xx.node ./lib/
您可以使用 ADD 命令(她的上下文监视 Dockerfile 放置的主机目录。它会将主机中与 Dockerfile 相同目录中的所有内容(在本例中为 cpp_app 目录的内容)复制到 docker容器。
...
ADD cpp_app /place/to/build
WORKDIR /place/to/build
RUN make
RUN mv result_file /place/where/result_file/have/to/be
WORKDIR /place/where/result_file/have/to/be
... execute your nodejs stuff
您将有两个选择,因为限制因素是 Docker 只允许 copying from the same directory as the Dockerfile:
创建新存储库
您可以创建一个新的存储库并将您的存储库用作子模块,或者仅用于 Docker 文件(这样您就必须在构建时将两个存储库复制到根文件夹中)。最后你要实现的是如下结构:
/ (root)
|-- C-plus-plus-Repo
|-- |-- <Files>
|-- Node-Repo
|-- |-- <Files>
|-- Dockerfile
您可以使用以下方法构建您的项目:
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
#1 copy the c++ source files
copy ./C-plus-plus-Repo .
RUN make
from node:10
WORKDIR /app
#1 copy the nodejs cource codes
copy ./Node-Repo .
RUN npm install
copy --from=u /app/dist/xx.node ./lib/
node index.js
在根目录执行:
docker build -t xx .
额外构建您的暂存容器
Docker 允许 copy from an external container as stage.
因此您可以在 C++ 存储库根目录中构建 C++ 容器
from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........
#1 copy the c++ source files
copy . .
RUN make
并标记它:
# Build your C++ Container in root of the c++ repo
docker build . -t c-stage
然后使用标签(在您的节点 Repo 根目录中)从中复制文件:
from node:10
WORKDIR /app
#1 copy the nodejs source files
copy . .
RUN npm install
# Use the Tag-Name of the already build container "c-stage"
copy --from=c-stage /app/dist/xx.node ./lib/
node index.js
两个构建步骤都可以从各自的 repo 根目录执行。