Docker多阶段构建与不同的项目

hgu*_*ser 5 docker docker-multi-stage-build

我们目前正在处理两个项目:

1个基于C++的项目

2基于Nodejs的项目

这两个项目是分开的,这意味着它们具有不同的代码库(git repoitory)和工作目录.

C++项目将生成一个.nodeNodejs项目将使用的节点绑定文件.

我们尝试使用多阶段为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
Run Code Online (Sandbox Code Playgroud)

我将通过构建图像docker build -t xx (?) #2.

但是,如dockerfile和命令中所述,如何设置context目录(请参阅注释#2)?因为它会影响dockerfile中的路径(参见注释#1).

我应该把哪个项目放在上面dockerfile呢?

Dan*_*cht 5

您将有两个选择,因为限制因素是 Docker 只允许从与 Dockerfile 相同的目录进行复制

创建一个新的存储库

您可以创建一个新的存储库并将存储库用作子模块,也可以仅用于 Dockerfile(您必须在构建时将两个存储库复制到根文件夹中)。最后你想要实现的是以下结构:

/ (root)
|-- C-plus-plus-Repo
|-- |-- <Files>
|-- Node-Repo
|-- |-- <Files>
|-- Dockerfile
Run Code Online (Sandbox Code Playgroud)

您可以使用以下方式构建项目:

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   
Run Code Online (Sandbox Code Playgroud)

在根目录下执行:

docker build -t xx . 
Run Code Online (Sandbox Code Playgroud)

额外构建您的暂存容器

Docker 允许从外部容器复制为 stage

因此,您可以在 C++ Repo 根目录中构建 C++ 容器

from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........  
#1 copy the c++ source files
copy . .  
RUN make  
Run Code Online (Sandbox Code Playgroud)

并标记它:

# Build your C++ Container in root of the c++ repo
docker build . -t c-stage
Run Code Online (Sandbox Code Playgroud)

然后使用标签(在节点 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
Run Code Online (Sandbox Code Playgroud)

两个构建步骤都可以从各自的存储库根执行。