Docker.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. =========================================
  2. A guide to Dockerfiles for building LLVM
  3. =========================================
  4. Introduction
  5. ============
  6. You can find a number of sources to build docker images with LLVM components in
  7. ``llvm/utils/docker``. They can be used by anyone who wants to build the docker
  8. images for their own use, or as a starting point for someone who wants to write
  9. their own Dockerfiles.
  10. We currently provide Dockerfiles with ``debian8`` and ``nvidia-cuda`` base images.
  11. We also provide an ``example`` image, which contains placeholders that one would need
  12. to fill out in order to produce Dockerfiles for a new docker image.
  13. Why?
  14. ----
  15. Docker images provide a way to produce binary distributions of
  16. software inside a controlled environment. Having Dockerfiles to builds docker images
  17. inside LLVM repo makes them much more discoverable than putting them into any other
  18. place.
  19. Docker basics
  20. -------------
  21. If you've never heard about Docker before, you might find this section helpful
  22. to get a very basic explanation of it.
  23. `Docker <https://www.docker.com/>`_ is a popular solution for running programs in
  24. an isolated and reproducible environment, especially to maintain releases for
  25. software deployed to large distributed fleets.
  26. It uses linux kernel namespaces and cgroups to provide a lightweight isolation
  27. inside currently running linux kernel.
  28. A single active instance of dockerized environment is called a *docker
  29. container*.
  30. A snapshot of a docker container filesystem is called a *docker image*.
  31. One can start a container from a prebuilt docker image.
  32. Docker images are built from a so-called *Dockerfile*, a source file written in
  33. a specialized language that defines instructions to be used when build
  34. the docker image (see `official
  35. documentation <https://docs.docker.com/engine/reference/builder/>`_ for more
  36. details). A minimal Dockerfile typically contains a base image and a number
  37. of RUN commands that have to be executed to build the image. When building a new
  38. image, docker will first download your base image, mount its filesystem as
  39. read-only and then add a writable overlay on top of it to keep track of all
  40. filesystem modifications, performed while building your image. When the build
  41. process is finished, a diff between your image's final filesystem state and the
  42. base image's filesystem is stored in the resulting image.
  43. Overview
  44. ========
  45. The ``llvm/utils/docker`` folder contains Dockerfiles and simple bash scripts to
  46. serve as a basis for anyone who wants to create their own Docker image with
  47. LLVM components, compiled from sources. The sources are checked out from the
  48. upstream svn repository when building the image.
  49. The resulting image contains only the requested LLVM components and a few extra
  50. packages to make the image minimally useful for C++ development, e.g. libstdc++
  51. and binutils.
  52. The interface to run the build is ``build_docker_image.sh`` script. It accepts a
  53. list of LLVM repositories to checkout and arguments for CMake invocation.
  54. If you want to write your own docker image, start with an ``example/`` subfolder.
  55. It provides an incomplete Dockerfile with (very few) FIXMEs explaining the steps
  56. you need to take in order to make your Dockerfiles functional.
  57. Usage
  58. =====
  59. The ``llvm/utils/build_docker_image.sh`` script provides a rather high degree of
  60. control on how to run the build. It allows you to specify the projects to
  61. checkout from svn and provide a list of CMake arguments to use during when
  62. building LLVM inside docker container.
  63. Here's a very simple example of getting a docker image with clang binary,
  64. compiled by the system compiler in the debian8 image:
  65. .. code-block:: bash
  66. ./llvm/utils/docker/build_docker_image.sh \
  67. --source debian8 \
  68. --docker-repository clang-debian8 --docker-tag "staging" \
  69. -p clang -i install-clang -i install-clang-resource-headers \
  70. -- \
  71. -DCMAKE_BUILD_TYPE=Release
  72. Note that a build like that doesn't use a 2-stage build process that
  73. you probably want for clang. Running a 2-stage build is a little more intricate,
  74. this command will do that:
  75. .. code-block:: bash
  76. # Run a 2-stage build.
  77. # LLVM_TARGETS_TO_BUILD=Native is to reduce stage1 compile time.
  78. # Options, starting with BOOTSTRAP_* are passed to stage2 cmake invocation.
  79. ./build_docker_image.sh \
  80. --source debian8 \
  81. --docker-repository clang-debian8 --docker-tag "staging" \
  82. -p clang -i stage2-install-clang -i stage2-install-clang-resource-headers \
  83. -- \
  84. -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release \
  85. -DBOOTSTRAP_CMAKE_BUILD_TYPE=Release \
  86. -DCLANG_ENABLE_BOOTSTRAP=ON -DCLANG_BOOTSTRAP_TARGETS="install-clang;install-clang-resource-headers"
  87. This will produce a new image ``clang-debian8:staging`` from the latest
  88. upstream revision.
  89. After the image is built you can run bash inside a container based on your image
  90. like this:
  91. .. code-block:: bash
  92. docker run -ti clang-debian8:staging bash
  93. Now you can run bash commands as you normally would:
  94. .. code-block:: bash
  95. root@80f351b51825:/# clang -v
  96. clang version 5.0.0 (trunk 305064)
  97. Target: x86_64-unknown-linux-gnu
  98. Thread model: posix
  99. InstalledDir: /bin
  100. Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
  101. Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.4
  102. Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
  103. Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
  104. Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
  105. Candidate multilib: .;@m64
  106. Selected multilib: .;@m64
  107. Which image should I choose?
  108. ============================
  109. We currently provide two images: debian8-based and nvidia-cuda-based. They
  110. differ in the base image that they use, i.e. they have a different set of
  111. preinstalled binaries. Debian8 is very minimal, nvidia-cuda is larger, but has
  112. preinstalled CUDA libraries and allows to access a GPU, installed on your
  113. machine.
  114. If you need a minimal linux distribution with only clang and libstdc++ included,
  115. you should try debian8-based image.
  116. If you want to use CUDA libraries and have access to a GPU on your machine,
  117. you should choose nvidia-cuda-based image and use `nvidia-docker
  118. <https://github.com/NVIDIA/nvidia-docker>`_ to run your docker containers. Note
  119. that you don't need nvidia-docker to build the images, but you need it in order
  120. to have an access to GPU from a docker container that is running the built
  121. image.
  122. If you have a different use-case, you could create your own image based on
  123. ``example/`` folder.
  124. Any docker image can be built and run using only the docker binary, i.e. you can
  125. run debian8 build on Fedora or any other Linux distribution. You don't need to
  126. install CMake, compilers or any other clang dependencies. It is all handled
  127. during the build process inside Docker's isolated environment.
  128. Stable build
  129. ============
  130. If you want a somewhat recent and somewhat stable build, use the
  131. ``branches/google/stable`` branch, i.e. the following command will produce a
  132. debian8-based image using the latest ``google/stable`` sources for you:
  133. .. code-block:: bash
  134. ./llvm/utils/docker/build_docker_image.sh \
  135. -s debian8 --d clang-debian8 -t "staging" \
  136. --branch branches/google/stable \
  137. -p clang -i install-clang -i install-clang-resource-headers \
  138. -- \
  139. -DCMAKE_BUILD_TYPE=Release
  140. Minimizing docker image size
  141. ============================
  142. Due to how Docker's filesystem works, all intermediate writes are persisted in
  143. the resulting image, even if they are removed in the following commands.
  144. To minimize the resulting image size we use `multi-stage Docker builds
  145. <https://docs.docker.com/develop/develop-images/multistage-build/>`_.
  146. Internally Docker builds two images. The first image does all the work: installs
  147. build dependencies, checks out LLVM source code, compiles LLVM, etc.
  148. The first image is only used during build and does not have a descriptive name,
  149. i.e. it is only accessible via the hash value after the build is finished.
  150. The second image is our resulting image. It contains only the built binaries
  151. and not any build dependencies. It is also accessible via a descriptive name
  152. (specified by -d and -t flags).