0

Inside a container I build a (C++) app. The source code directory is shared with --volume. If docker runs on Linux, the shared directory runs at full speed, but if docker runs on mac, docker has to bridge the share which results in speed drop. Therefore I have to copy the whole source directory to the container before starting compiling. But this copy step is necessary on non-Linux hosts only.

How can I detect if the share is "natively" shared?

Can I get information about the host os from inside the container?

Update

The idea behind this workflow is to setup an image for a defined environment to cross-build the product for multiple platforms (win, mac, linux). Otherwise each developer has a different Linux OS/compilers/components etc installed.

As a docker newbie I thought that this image (with all required 3rdParty components/compilers) can be used to build the app within a container when it is launched.

Macintron
  • 97
  • 1
  • 10

3 Answers3

0

One workaround I can think of is that you can use a special networking feature which is available in both Mac and Windows hosts, but not in Linux.

It is a special dns entry you can use to get the ip of the host from inside container - host.docker.internal. Read more here and here.

Now you just need a command to get a boolean value if it resolves or not. Since I don’t know which shell you are using, I cant say for sure but something like this should help you.

Milan Markovic
  • 1,250
  • 13
  • 18
  • Thank you for the idea, I also thought about this. The idea is to use the image on any machine, just with docker installed and without any services on the host. – Macintron Nov 04 '20 at 11:01
0

In my opinion you are looking at the issue from the wrong perspective.

First of all the compilation should be done at build time, not at runtime. If you do it in the container then it means that you are shipping an image with build tools, not to say that user of the image would need the source code to run the image. For this reason it is a good practice to compile at build time and only ship an image with the binary to run.

Secondly, compiling at build time is fast because the source code is sent to the docker daemon and accessed directly from there, no need for volumes.

Lastly, to answer your last question, it is you who runs the container. So you can tell it everything about the host where it is running by just adding and environment variable (for example). It is over complicated to just run the container and let it guess where it is running, when you already have that information at the moment yuo start the container.

Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Thank you for your input. In respect of my updated question, would you create a base image setup with the environment and let the developer build another image which compiles the sources when run? If the sources change I than have to create another image... – Macintron Nov 04 '20 at 10:56
  • ... and build the whole project again, can't use [ccache](https://ccache.dev/). – Macintron Nov 04 '20 at 11:51
  • You can always create a development base image. In there you install all the dependencies you need. And this is something you can distribute. You can then extend that image and either mount or copy the source code. Since that is the only thing you would do in the new image then it becomes trivial and it really doesn't matter the host OS because the user/developer it in charge to choose the optimal solution for them. – Mihai Nov 04 '20 at 12:49
0

I used the --env DO_COPY=1 when creating the container.

Macintron
  • 97
  • 1
  • 10