Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker multi-stage-build with different project

We are working with two project at the moment:

1 C++ based project

2 Nodejs based project

These two projectes are separated which means they have different codebase(git repoitory) and working directory.

C++ project will produce a node binding file .node which will be used by Nodejs project.

And we try to build an docker image for the Nodejs project with multi-stage like this:

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

And I will build the image by docker build -t xx (?) #2.

However as commented in the dockerfile and the command, how to setup the context directory(see comment #2)? Since it will affect the path in the dockerfile (see comment #1).

Also which project should I put inside for the above dockerfile?

like image 540
hguser Avatar asked Aug 24 '18 03:08

hguser


People also ask

Can you have 2 Dockerfiles?

Introduction. Docker is a handy tool for containerization. It's so useful that sometimes, we want to have more than one Dockerfile in the project. Unfortunately, this goes against the straightforward convention of naming all Dockerfiles just “Dockerfile”.

How does Docker multi stage build work?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

What is the advantage of multi stage builds?

Multistage builds let the developer automate the creation process of applications that require some amount of compilation. Developers can create versions that target different OS versions or any other process dependency, which is a big benefit of the approach.

Should you use the same Dockerfile for Dev staging and production builds?

Multiple DockerfilesStaging and production should be using the same image, built from the same Dockerfile to guarantee that they are as-similar-as-possible.


1 Answers

You will have two options on this, as the limiting factor is that Docker only allows copying from the same directory as the Dockerfile:

Create a new Repository

You can either create a new repo and use your repos as submodules or just for the Dockerfile (Than you would have to copy both repos into the root folder at build time). In the End what you want to achieve is the following structure:

/ (root)
|-- C-plus-plus-Repo
|-- |-- <Files>
|-- Node-Repo
|-- |-- <Files>
|-- Dockerfile

Than you can build your project with:

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   

In the root Directory execute:

docker build -t xx . 

Build your staging containers extra

Docker allows to copy from an external container as stage.

So you can build the C++ container in your C++ Repo root

from ubuntu:18.04 as u
WORKDIR /app
RUN apt-get........  
#1 copy the c++ source files
copy . .  
RUN make  

and Tag it:

# Build your C++ Container in root of the c++ repo
docker build . -t c-stage

then copy the files from it, using the tag (in your node Repo root):

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

Both build steps can be executed from their respective repo roots.

like image 164
Daniel Habenicht Avatar answered Oct 03 '22 01:10

Daniel Habenicht