6

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?

mohan08p
  • 5,002
  • 1
  • 28
  • 36
hguser
  • 35,079
  • 54
  • 159
  • 293

3 Answers3

5

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.

Daniel Habenicht
  • 2,133
  • 1
  • 15
  • 36
  • Ok, sounds like putting all the related project together is the only way. – hguser Sep 04 '18 at 09:25
  • Well, with the second variant you can do it without putting them together. But they will have a dependency on each other that is not clear in the first place. So putting them together might be a good solution. – Daniel Habenicht Sep 04 '18 at 09:35
3

Creating a deploy project with git submodules

How about creating a deploy project using git submodules?

This project would only exist for building the docker image and contains the Dockerfile and both of your projects as git submodules.

Since you not just copy the two projects, but manage them with git, you can always keep them up to date with git submodules update --remote, but note that this leaves your submodule in a detached head state. However this is not a problem as long as you do not try to update your C++ project or the node project from the deploy project.

You can create the project with the following commands:

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

Then you can simply add the paths to the subprojects to your dockerfile and build the image in the root directory of the deploy project.

The dockerfile would look like this

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/
kalehmann
  • 4,821
  • 6
  • 26
  • 36
1

You can use ADD command (her context watching to host directory where Dockerfile placed. It will copy everything placed in same directory as Dockerfile in host machine (in this case content of cpp_app directory) into the docker container.

...
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
Igor Skobelev
  • 339
  • 1
  • 6