Search code examples
dockerdocker-multi-stage-build

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?


Solution

  • 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.