annotate docs/Docker.rst @ 124:4fa72497ed5d

fix
author mir3636
date Thu, 30 Nov 2017 20:04:56 +0900
parents 803732b1fca8
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 =========================================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 A guide to Dockerfiles for building LLVM
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 =========================================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 Introduction
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 ============
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 You can find a number of sources to build docker images with LLVM components in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 ``llvm/utils/docker``. They can be used by anyone who wants to build the docker
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 images for their own use, or as a starting point for someone who wants to write
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 their own Dockerfiles.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 We currently provide Dockerfiles with ``debian8`` and ``nvidia-cuda`` base images.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 We also provide an ``example`` image, which contains placeholders that one would need
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 to fill out in order to produce Dockerfiles for a new docker image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 Why?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 ----
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 Docker images provide a way to produce binary distributions of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 software inside a controlled environment. Having Dockerfiles to builds docker images
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 inside LLVM repo makes them much more discoverable than putting them into any other
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 place.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 Docker basics
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 -------------
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 If you've never heard about Docker before, you might find this section helpful
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 to get a very basic explanation of it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 `Docker <https://www.docker.com/>`_ is a popular solution for running programs in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 an isolated and reproducible environment, especially to maintain releases for
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 software deployed to large distributed fleets.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 It uses linux kernel namespaces and cgroups to provide a lightweight isolation
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 inside currently running linux kernel.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 A single active instance of dockerized environment is called a *docker
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 container*.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 A snapshot of a docker container filesystem is called a *docker image*.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 One can start a container from a prebuilt docker image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 Docker images are built from a so-called *Dockerfile*, a source file written in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 a specialized language that defines instructions to be used when build
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 the docker image (see `official
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 documentation <https://docs.docker.com/engine/reference/builder/>`_ for more
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 details). A minimal Dockerfile typically contains a base image and a number
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 of RUN commands that have to be executed to build the image. When building a new
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 image, docker will first download your base image, mount its filesystem as
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 read-only and then add a writable overlay on top of it to keep track of all
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 filesystem modifications, performed while building your image. When the build
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 process is finished, a diff between your image's final filesystem state and the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 base image's filesystem is stored in the resulting image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 Overview
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 ========
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 The ``llvm/utils/docker`` folder contains Dockerfiles and simple bash scripts to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 serve as a basis for anyone who wants to create their own Docker image with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 LLVM components, compiled from sources. The sources are checked out from the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 upstream svn repository when building the image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 Inside each subfolder we host Dockerfiles for two images:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 - ``build/`` image is used to compile LLVM, it installs a system compiler and all
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 build dependencies of LLVM. After the build process is finished, the build
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 image will have an archive with compiled components at ``/tmp/clang.tar.gz``.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 - ``release/`` image usually only contains LLVM components, compiled by the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 ``build/`` image, and also libstdc++ and binutils to make image minimally
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 useful for C++ development. The assumption is that you usually want clang to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 be one of the provided components.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 To build both of those images, use ``build_docker_image.sh`` script.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 It will checkout LLVM sources and build clang in the ``build`` container, copy results
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 of the build to the local filesystem and then build the ``release`` container using
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 those. The ``build_docker_image.sh`` accepts a list of LLVM repositories to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 checkout, and arguments for CMake invocation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 If you want to write your own docker image, start with an ``example/`` subfolder.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 It provides incomplete Dockerfiles with (very few) FIXMEs explaining the steps
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 you need to take in order to make your Dockerfiles functional.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 Usage
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 =====
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 The ``llvm/utils/build_docker_image.sh`` script provides a rather high degree of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 control on how to run the build. It allows you to specify the projects to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 checkout from svn and provide a list of CMake arguments to use during when
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 building LLVM inside docker container.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 Here's a very simple example of getting a docker image with clang binary,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 compiled by the system compiler in the debian8 image:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 .. code-block:: bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 ./llvm/utils/docker/build_docker_image.sh \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 --source debian8 \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 --docker-repository clang-debian8 --docker-tag "staging" \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 -p clang -i install-clang -i install-clang-headers \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 -- \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 -DCMAKE_BUILD_TYPE=Release
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 Note that a build like that doesn't use a 2-stage build process that
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 you probably want for clang. Running a 2-stage build is a little more intricate,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 this command will do that:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 .. code-block:: bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 # Run a 2-stage build.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 # LLVM_TARGETS_TO_BUILD=Native is to reduce stage1 compile time.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 # Options, starting with BOOTSTRAP_* are passed to stage2 cmake invocation.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 ./build_docker_image.sh \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 --source debian8 \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 --docker-repository clang-debian8 --docker-tag "staging" \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 -p clang -i stage2-install-clang -i stage2-install-clang-headers \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 -- \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 -DLLVM_TARGETS_TO_BUILD=Native -DCMAKE_BUILD_TYPE=Release \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 -DBOOTSTRAP_CMAKE_BUILD_TYPE=Release \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 -DCLANG_ENABLE_BOOTSTRAP=ON -DCLANG_BOOTSTRAP_TARGETS="install-clang;install-clang-headers"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 This will produce two images, a release image ``clang-debian8:staging`` and a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 build image ``clang-debian8-build:staging`` from the latest upstream revision.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 After the image is built you can run bash inside a container based on your
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 image like this:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 .. code-block:: bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 docker run -ti clang-debian8:staging bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 Now you can run bash commands as you normally would:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 .. code-block:: bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 root@80f351b51825:/# clang -v
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 clang version 5.0.0 (trunk 305064)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 Target: x86_64-unknown-linux-gnu
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 Thread model: posix
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 InstalledDir: /bin
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.4
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 Candidate multilib: .;@m64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 Selected multilib: .;@m64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 Which image should I choose?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 ============================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 We currently provide two images: debian8-based and nvidia-cuda-based. They
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 differ in the base image that they use, i.e. they have a different set of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 preinstalled binaries. Debian8 is very minimal, nvidia-cuda is larger, but has
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 preinstalled CUDA libraries and allows to access a GPU, installed on your
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 machine.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 If you need a minimal linux distribution with only clang and libstdc++ included,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 you should try debian8-based image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 If you want to use CUDA libraries and have access to a GPU on your machine,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 you should choose nvidia-cuda-based image and use `nvidia-docker
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 <https://github.com/NVIDIA/nvidia-docker>`_ to run your docker containers. Note
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154 that you don't need nvidia-docker to build the images, but you need it in order
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 to have an access to GPU from a docker container that is running the built
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 image.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 If you have a different use-case, you could create your own image based on
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 ``example/`` folder.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 Any docker image can be built and run using only the docker binary, i.e. you can
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 run debian8 build on Fedora or any other Linux distribution. You don't need to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 install CMake, compilers or any other clang dependencies. It is all handled
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 during the build process inside Docker's isolated environment.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 Stable build
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 ============
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 If you want a somewhat recent and somewhat stable build, use the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 ``branches/google/stable`` branch, i.e. the following command will produce a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 debian8-based image using the latest ``google/stable`` sources for you:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 .. code-block:: bash
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 ./llvm/utils/docker/build_docker_image.sh \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 -s debian8 --d clang-debian8 -t "staging" \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 --branch branches/google/stable \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 -p clang -i install-clang -i install-clang-headers \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 -- \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 -DCMAKE_BUILD_TYPE=Release
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 Minimizing docker image size
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 ============================
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 Due to Docker restrictions we use two images (i.e., build and release folders)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 for the release image to be as small as possible. It's much easier to achieve
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 that using two images, because Docker would store a filesystem layer for each
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 command in the Dockerfile, i.e. if you install some packages in one command,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 then remove those in a separate command, the size of the resulting image will
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 still be proportinal to the size of an image with installed packages.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 Therefore, we strive to provide a very simple release image which only copies
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 compiled clang and does not do anything else.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 Docker 1.13 added a ``--squash`` flag that allows to flatten the layers of the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 image, i.e. remove the parts that were actually deleted. That is an easier way
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 to produce the smallest images possible by using just a single image. We do not
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 use it because as of today the flag is in experimental stage and not everyone
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 may have the latest docker version available. When the flag is out of
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 experimental stage, we should investigate replacing two images approach with
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 just a single image, built using ``--squash`` flag.