Github 用于从git克隆的建筑代码的Docker文件

Github 用于从git克隆的建筑代码的Docker文件,github,docker,cmake,dockerfile,Github,Docker,Cmake,Dockerfile,我已经从github克隆了一个副本,我正在尝试创建一个docker文件,以便在我的机器上本地开发它 目标是: 我的机器上有git代码的本地副本 我可以修改代码 我可以构建调试和发布映像(我需要创建两个单独的映像吗?) 可以访问我机器上的代码,这样我就可以使用git进行源代码控制 这是我的Dockerfile的内容: # Get base image FROM phusion/baseimage # Use baseimage-docker's init system. CMD ["/sbin/

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

目标是:

  • 我的机器上有git代码的本地副本
  • 我可以修改代码
  • 我可以构建调试和发布映像(我需要创建两个单独的映像吗?)
  • 可以访问我机器上的代码,这样我就可以使用git进行源代码控制
  • 这是我的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
    
    我尝试使用以下命令构建映像:

    docker build-标记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
    ,这样我就可以找到从哪里运行cmake命令,我惊讶地发现它是从根目录运行的

    我认为这个问题是由我使用相对路径引起的,它将由绝对路径来解决——但是在克隆等时指定
    /path/to/my/copy/freecad
    ,问题仍然存在


    如何编写Dockerfile以实现上述目标(在我问题的开头说明)?

    您应该使用运行方式安装所有依赖项,但实际生成和复制源代码文件不应发生在生成映像时,而应发生在运行容器时

    通过这种方式,您可以在任意多个构建中重用映像

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


    要与容器共享git项目,可以使用docker run-v hostpath:containerpath imagename装载本地文件。这样,hostpath中的任何文件都将对containerpath中的容器可见,反之亦然。或者,您也可以从CMD调用的脚本中进行git克隆,但随后您必须以某种方式将构建公开给您的主机(再次装载一些卷)。

    docker中的默认WORKDIR为“/”。所有docker命令都将在该目录中执行。有两个选项,您可以更改WORKDIR()或在一层中执行所有内容(在一次运行命令中)。我采取了第二种方法

    克隆和构建源代码都在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
    

    在构建映像时,而在运行容器时,不应实际构建和复制源代码文件
    ,除非我误解了你的意思-这显然是不正确的。构建像FreeCAD这样的应用程序可能需要半小时的时间。因此,启动容器需要半小时-这是不可行的解决方案。同样由于某些原因,我不理解你的答案。你能通过修改我的Dockerfile并粘贴到这里来演示一下吗,这样我就可以明白你的意思了(将来也可以帮助其他有类似问题的人)。也许我理解错了您的问题。您想反复更改代码并使用docker生成更改,对吗?在这种情况下,您不应该在dockerfile中完成所有操作。dockerfile用于所有需要一次性完成的步骤(安装依赖项……)容器运行时会重复执行需要执行的操作,并将结果公开给装载了卷的主机。@HomunculusReticulli构建映像需要半个小时,而不是启动容器。