从git克隆构build代码的Docker文件

我从github克隆了一个FreeCAD的副本,我正在尝试创build一个docker文件,以便我可以在本地机器上开发它。

目标是:

  1. 我有我的机器上git的代码的本地副本
  2. 我可以对代码进行修改
  3. 我可以构builddebugging和发布图像(我是否需要创build两个单独的图像?)
  4. 有权访问我的机器上的代码,以便我可以使用git进行源代码pipe理

这是我的Dockerfile的内容:

# Get base image FROM phusion/baseimage # Use baseimage-docker's init system. CMD ["/sbin/my_init"] # Get the build pre-requisites RUN apt-get update RUN apt-get install -y build-essential cmake python python-matplotlib libtool RUN apt-get install -y libcoin80-dev libsoqt4-dev RUN apt-get install -y libxerces-c-dev libboost-dev libboost-filesystem-dev RUN apt-get install -y libboost-regex-dev RUN apt-get install -y libboost-program-options-dev libboost-signals-dev RUN apt-get install -y libboost-thread-dev libboost-python-dev libqt4-dev RUN apt-get install -y libqt4-opengl-dev qt4-dev-tools python-dev RUN apt-get install -y python-pyside pyside-tools RUN apt-get install -y liboce*-dev oce-draw RUN apt-get install -y libeigen3-dev libqtwebkit-dev libshiboken-dev RUN apt-get install -y libpyside-dev libode-dev swig libzipios++-dev RUN apt-get install -y libfreetype6 libfreetype6-dev # to make Coin to support additional image file formats RUN apt-get install -y libsimage-dev # to register your installed files into your system's package manager, so yo can easily uninstall later RUN apt-get install -y checkinstall # needed for the 2D Drafting module RUN apt-get install -y python-qt4 python-pivy # doxygen and libcoin80-doc (if you intend to generate source code documentation) RUN apt-get install -y doxygen libcoin80-doc # libspnav-dev (for 3Dconnexion devices support like the Space Navigator or Space Pilot) RUN apt-get install -y libspnav-dev # CMAke related issue for compiling on Ubuntu Xenial: http://forum.freecadweb.org/viewtopic.php?f=4&t=16292 RUN apt-get install -y libmedc-dev RUN apt-get install -y libvtk6-dev RUN apt-get install -y libproj-dev # Get git RUN apt-get install -y git RUN git clone https://github.com/FreeCAD/FreeCAD.git freecad RUN cd freecad RUN mkdir freecad-debug RUN cd freecad-debug # command below is just a diagnostic to let me know wth I am (output is: /) # RUN pwd RUN cmake ../ -DFREECAD_USE_EXTERNAL_PIVY=1 -DCMAKE_BUILD_TYPE=Debug . #cmake -DFREECAD_USE_EXTERNAL_PIVY=1 -DCMAKE_BUILD_TYPE=Release . RUN make 

我尝试使用以下命令来构build图像:

docker build -tag freeCAD-my-fork .

一切工作,直到我到第一个cmake调用。 然后我得到以下错误:

 CMake Error: The source directory "/" does not appear to contain CMakeLists.txt. Specify --help for usage, or press the help button on the CMake GUI. The command '/bin/sh -c cmake ../ -DFREECAD_USE_EXTERNAL_PIVY=1 -DCMAKE_BUILD_TYPE=Debug .' returned a non-zero code: 1 

我在我的Dockerfile中放了一个RUN pwd ,所以我可以findcmake命令运行的地方,我惊奇地发现它已经从根目录运行了。

我认为这个问题是由于我使用亲戚造成的,而且会被绝对path修复 – 但是在克隆等时指定/path/to/my/copy/freecad ,问题仍然存在。

我如何编写我的Dockerfile,以达到上面列出的目标(在我的问题的开头部分)?

docker中的默认WORKDIR是“/”。所有docker命令都将在该目录中执行。有两个选项可以更改WORKDIR( https://docs.docker.com/engine/reference/builder/#workdir )或执行所有操作在一层(在一个RUN命令)。我采取了第二种方法。

克隆和构build源代码都在Docker的一层中执行。

 RUN git clone https://github.com/FreeCAD/FreeCAD.git freecad \ && cd freecad \ && mkdir freecad-debug \ && cd freecad-debug \ && cmake ../ -DFREECAD_USE_EXTERNAL_PIVY=1 -DCMAKE_BUILD_TYPE=Debug . \ && make 

您应该像使用run一样安装所有的依赖关系,但是当您构build映像但运行容器时,应该不会发生实际的源代码文件构build和复制。

这样,您可以重新使用您的图像,尽可能多的构build,只要你喜欢。

用build命令编写脚本,并将其复制到您的映像中。 然后在dockerfile的CMD部分运行该脚本。

要与容器共享git项目,可以使用docker run -v hostpath:containerpath imagename装载本地文件。 这样,hostpath中的任何文件都将在容器的containerpath中可见,反之亦然。 另外,你也可以从CMD调用的脚本中克隆克隆,但是你必须以某种方式将你的主机公开给你的主机(一些挂载的卷)。