author | wenzelm |
Tue, 23 Nov 2021 12:29:09 +0100 | |
changeset 74832 | c299abcf7081 |
parent 74727 | fa15929bdf83 |
child 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
66790 | 1 |
/* Title: Pure/Tools/build_docker.scala |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
3 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
4 |
Build docker image from Isabelle application bundle for Linux. |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
6 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
8 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
9 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
10 |
object Build_Docker |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
11 |
{ |
64942 | 12 |
private val default_base = "ubuntu" |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
13 |
private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
14 |
|
74654
b67d1d72516b
support linux_arm as well, e.g. native Docker on Apple Silicon;
wenzelm
parents:
74492
diff
changeset
|
15 |
private val Isabelle_Name = """^.*?(Isabelle[^/\\:]+)_linux(?:_arm)?\.tar\.gz$""".r |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
16 |
|
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
17 |
val packages: List[String] = |
74492 | 18 |
List("curl", "less", "libfontconfig1", "libgomp1", "pwgen", "unzip") |
64894 | 19 |
|
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
20 |
val package_collections: Map[String, List[String]] = |
64895 | 21 |
Map("X11" -> List("libx11-6", "libxext6", "libxrender1", "libxtst6", "libxi6"), |
69958 | 22 |
"latex" -> |
23 |
List("texlive-fonts-extra", "texlive-font-utils", "texlive-latex-extra", "texlive-science")) |
|
64894 | 24 |
|
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
25 |
def build_docker(progress: Progress, |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
26 |
app_archive: String, |
64942 | 27 |
base: String = default_base, |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
28 |
logic: String = default_logic, |
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
29 |
no_build: Boolean = false, |
64942 | 30 |
entrypoint: Boolean = false, |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
31 |
output: Option[Path] = None, |
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
32 |
more_packages: List[String] = Nil, |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
33 |
tag: String = "", |
73340 | 34 |
verbose: Boolean = false): Unit = |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
35 |
{ |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
36 |
val isabelle_name = |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
37 |
app_archive match { |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
38 |
case Isabelle_Name(name) => name |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
39 |
case _ => error("Cannot determine Isabelle distribution name from " + app_archive) |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
40 |
} |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
41 |
val is_remote = Url.is_wellformed(app_archive) |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
42 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
43 |
val dockerfile = |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
44 |
"""## Dockerfile for """ + isabelle_name + """ |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
45 |
|
64942 | 46 |
FROM """ + base + """ |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
47 |
SHELL ["/bin/bash", "-c"] |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
48 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
49 |
# packages |
69958 | 50 |
ENV DEBIAN_FRONTEND=noninteractive |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
51 |
RUN apt-get -y update && \ |
64941 | 52 |
apt-get install -y """ + Bash.strings(packages ::: more_packages) + """ && \ |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
53 |
apt-get clean |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
54 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
55 |
# user |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
56 |
RUN useradd -m isabelle && (echo isabelle:isabelle | chpasswd) |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
57 |
USER isabelle |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
58 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
59 |
# Isabelle |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
60 |
WORKDIR /home/isabelle |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
61 |
""" + |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
62 |
(if (is_remote) |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
63 |
"RUN curl --fail --silent " + Bash.string(app_archive) + " > Isabelle.tar.gz" |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
64 |
else "COPY Isabelle.tar.gz .") + |
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
65 |
""" |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
66 |
RUN tar xzf Isabelle.tar.gz && \ |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
67 |
mv """ + isabelle_name + """ Isabelle && \ |
74727 | 68 |
sed -i -e 's,ISABELLE_HOME_USER=.*,ISABELLE_HOME_USER="\$USER_HOME/.isabelle",g;' Isabelle/etc/settings && \ |
69 |
sed -i -e 's,ISABELLE_LOGIC=.*,ISABELLE_LOGIC=""" + logic + """,g;' Isabelle/etc/settings && \ |
|
71580 | 70 |
Isabelle/bin/isabelle build -o system_heaps -b """ + logic + """ && \ |
71 |
rm Isabelle.tar.gz""" + |
|
64942 | 72 |
(if (entrypoint) """ |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
73 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
74 |
ENTRYPOINT ["Isabelle/bin/isabelle"] |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
75 |
""" |
64942 | 76 |
else "") |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
77 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
78 |
output.foreach(File.write(_, dockerfile)) |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
79 |
|
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
80 |
if (!no_build) { |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
81 |
Isabelle_System.with_tmp_dir("docker")(tmp_dir => |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
82 |
{ |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
83 |
File.write(tmp_dir + Path.explode("Dockerfile"), dockerfile) |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
84 |
|
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
85 |
if (is_remote) { |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
86 |
if (!Url.is_readable(app_archive)) |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
87 |
error("Cannot access remote archive " + app_archive) |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
88 |
} |
73317 | 89 |
else Isabelle_System.copy_file(Path.explode(app_archive), |
90 |
tmp_dir + Path.explode("Isabelle.tar.gz")) |
|
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
91 |
|
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
92 |
val quiet_option = if (verbose) "" else " -q" |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
93 |
val tag_option = if (tag == "") "" else " -t " + Bash.string(tag) |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
94 |
progress.bash("docker build" + quiet_option + tag_option + " " + File.bash_path(tmp_dir), |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
95 |
echo = true).check |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
96 |
}) |
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
97 |
} |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
98 |
} |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
99 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
100 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
101 |
/* Isabelle tool wrapper */ |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
102 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
103 |
val isabelle_tool = |
72763 | 104 |
Isabelle_Tool("build_docker", "build Isabelle docker image", |
105 |
Scala_Project.here, args => |
|
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
106 |
{ |
64942 | 107 |
var base = default_base |
108 |
var entrypoint = false |
|
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
109 |
var logic = default_logic |
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
110 |
var no_build = false |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
111 |
var output: Option[Path] = None |
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
112 |
var more_packages: List[String] = Nil |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
113 |
var verbose = false |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
114 |
var tag = "" |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
115 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
116 |
val getopts = |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
117 |
Getopts(""" |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
118 |
Usage: isabelle build_docker [OPTIONS] APP_ARCHIVE |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
119 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
120 |
Options are: |
64942 | 121 |
-B NAME base image (default """ + quote(default_base) + """) |
72525 | 122 |
-E set Isabelle/bin/isabelle as entrypoint |
64894 | 123 |
-P NAME additional Ubuntu package collection (""" + |
64895 | 124 |
package_collections.keySet.toList.sorted.map(quote(_)).mkString(", ") + """) |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
125 |
-l NAME default logic (default ISABELLE_LOGIC=""" + quote(default_logic) + """) |
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
126 |
-n no docker build |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
127 |
-o FILE output generated Dockerfile |
64894 | 128 |
-p NAME additional Ubuntu package |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
129 |
-t TAG docker build tag |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
130 |
-v verbose |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
131 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
132 |
Build Isabelle docker image with default logic image, using a standard |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
133 |
Isabelle application archive for Linux (local file or remote URL). |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
134 |
""", |
64942 | 135 |
"B:" -> (arg => base = arg), |
136 |
"E" -> (_ => entrypoint = true), |
|
64894 | 137 |
"P:" -> (arg => |
138 |
package_collections.get(arg) match { |
|
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
139 |
case Some(ps) => more_packages :::= ps |
64894 | 140 |
case None => error("Unknown package collection " + quote(arg)) |
141 |
}), |
|
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
142 |
"l:" -> (arg => logic = arg), |
64906
49549acbf025
added option -n, e.g. useful to generate Dockerfile only;
wenzelm
parents:
64905
diff
changeset
|
143 |
"n" -> (_ => no_build = true), |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
144 |
"o:" -> (arg => output = Some(Path.explode(arg))), |
64899
749d3a86c6a3
clarified signature: packages may be accessed in Isabelle/Scala;
wenzelm
parents:
64897
diff
changeset
|
145 |
"p:" -> (arg => more_packages ::= arg), |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
146 |
"t:" -> (arg => tag = arg), |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
147 |
"v" -> (_ => verbose = true)) |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
148 |
|
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
149 |
val more_args = getopts(args) |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
150 |
val app_archive = |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
151 |
more_args match { |
64905
5e2eb9b14bbe
support for remote APP_ARCHIVE, with self-contained Dockerfile (e.g. for Docker Hub);
wenzelm
parents:
64903
diff
changeset
|
152 |
case List(arg) => arg |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
153 |
case _ => getopts.usage() |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
154 |
} |
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
155 |
|
64942 | 156 |
build_docker(new Console_Progress(), app_archive, base = base, logic = logic, |
157 |
no_build = no_build, entrypoint = entrypoint, output = output, |
|
158 |
more_packages = more_packages, tag = tag, verbose = verbose) |
|
66790 | 159 |
}) |
64890
d8ccbd5305bf
build docker image from Isabelle application bundle for Linux;
wenzelm
parents:
diff
changeset
|
160 |
} |