| 1 | use std::{ |
| 2 | borrow::Cow, |
| 3 | convert, |
| 4 | fmt, |
| 5 | }; |
| 6 | |
| 7 | /// The `ADD` instruction copies new files, directories or remote file URLs from `<src>` and adds |
| 8 | /// them to the filesystem of the image at the path `<dest>`. |
| 9 | /// |
| 10 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#add). |
| 11 | pub struct Add(Cow<'static, str>); |
| 12 | |
| 13 | impl Add { |
| 14 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 15 | Add(val.into()) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl fmt::Display for Add { |
| 20 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 21 | write!(f, "ADD {}\n", &self.0) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /// The `ARG` instruction defines a variable that users can pass at build-time to the builder with |
| 26 | /// the `docker build` command using the `--build-arg <varname>=<value>` flag. |
| 27 | /// |
| 28 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#arg). |
| 29 | pub struct Arg(Cow<'static, str>); |
| 30 | |
| 31 | impl Arg { |
| 32 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 33 | Arg(val.into()) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl fmt::Display for Arg { |
| 38 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 39 | write!(f, "ARG {}\n", &self.0) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// The main purpose of a `CMD` is to provide defaults for an executing container. |
| 44 | /// |
| 45 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#cmd). |
| 46 | pub struct Cmd(Cow<'static, str>); |
| 47 | |
| 48 | impl Cmd { |
| 49 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 50 | Cmd(val.into()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl fmt::Display for Cmd { |
| 55 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 56 | write!(f, "CMD {}\n", &self.0) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// The `COPY` instruction copies new files or directories from `<src>` and adds them to the |
| 61 | /// filesystem of the container at the path `<dest>`. |
| 62 | /// |
| 63 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#copy). |
| 64 | pub struct Copy(Cow<'static, str>); |
| 65 | |
| 66 | impl Copy { |
| 67 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 68 | Copy(val.into()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl fmt::Display for Copy { |
| 73 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 74 | write!(f, "COPY {}\n", &self.0) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Parser directives are optional, and affect the way in which subsequent lines in a `Dockerfile` |
| 79 | /// are handled. |
| 80 | /// |
| 81 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#parser-directives). |
| 82 | pub struct Directive(Cow<'static, str>); |
| 83 | |
| 84 | impl Directive { |
| 85 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 86 | Directive(val.into()) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl fmt::Display for Directive { |
| 91 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 92 | write!(f, "# {}\n", &self.0) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /// An `ENTRYPOINT` allows you to configure a container that will run as an executable. |
| 97 | /// |
| 98 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#entrypoint). |
| 99 | pub struct Entrypoint(Cow<'static, str>); |
| 100 | |
| 101 | impl Entrypoint { |
| 102 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 103 | Entrypoint(val.into()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl fmt::Display for Entrypoint { |
| 108 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 109 | write!(f, "ENTRYPOINT {}\n", &self.0) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// The `ENV` instruction sets the environment variable `<key>` to the value `<value>`. |
| 114 | /// |
| 115 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#env). |
| 116 | pub struct Env(Cow<'static, str>); |
| 117 | |
| 118 | impl Env { |
| 119 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 120 | Env(val.into()) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | impl fmt::Display for Env { |
| 125 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 126 | write!(f, "ENV {}\n", &self.0) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// The `EXPOSE` instruction informs Docker that the container listens on the specified network |
| 131 | /// ports at runtime. |
| 132 | /// |
| 133 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#expose). |
| 134 | pub struct Expose(Cow<'static, str>); |
| 135 | |
| 136 | impl Expose { |
| 137 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 138 | Expose(val.into()) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl fmt::Display for Expose { |
| 143 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 144 | write!(f, "EXPOSE {}\n", &self.0) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// The `FROM` instruction initializes a new build stage and sets the base image for subsequent |
| 149 | /// instructions. |
| 150 | /// |
| 151 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#from). |
| 152 | pub struct From(Cow<'static, str>); |
| 153 | |
| 154 | impl From { |
| 155 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 156 | From(val.into()) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | impl fmt::Display for From { |
| 161 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 162 | write!(f, "FROM {}\n", &self.0) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// The `HEALTHCHECK` instruction tells Docker how to test a container to check that it is still |
| 167 | /// working. |
| 168 | /// |
| 169 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#healthcheck). |
| 170 | pub struct Healthcheck(Cow<'static, str>); |
| 171 | |
| 172 | impl Healthcheck { |
| 173 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 174 | Healthcheck(val.into()) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | impl fmt::Display for Healthcheck { |
| 179 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 180 | write!(f, "HEALTHCHECK {}\n", &self.0) |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// The `LABEL` instruction adds metadata to an image. |
| 185 | /// |
| 186 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#label). |
| 187 | pub struct Label(Cow<'static, str>); |
| 188 | |
| 189 | impl Label { |
| 190 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 191 | Label(val.into()) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl fmt::Display for Label { |
| 196 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 197 | write!(f, "LABEL {}\n", &self.0) |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /// The `ONBUILD` instruction adds to the image a trigger instruction to be executed at a later |
| 202 | /// time, when the image is used as the base for another build. The trigger will be executed in |
| 203 | /// the context of the downstream build, as if it had been inserted immediately after the `FROM` |
| 204 | /// instruction in the downstream `Dockerfile`. |
| 205 | /// |
| 206 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#onbuild). |
| 207 | pub struct Onbuild(Cow<'static, str>); |
| 208 | |
| 209 | impl Onbuild { |
| 210 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 211 | Onbuild(val.into()) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | impl fmt::Display for Onbuild { |
| 216 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 217 | write!(f, "ONBUILD {}\n", &self.0) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// The `RUN` instruction will execute any commands in a new layer on top of the current image and |
| 222 | /// commit the results. |
| 223 | /// |
| 224 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#run). |
| 225 | pub struct Run(Cow<'static, str>); |
| 226 | |
| 227 | impl Run { |
| 228 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 229 | Run(val.into()) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | impl fmt::Display for Run { |
| 234 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 235 | write!(f, "RUN {}\n", &self.0) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /// The `SHELL` instruction allows the default shell used for the shell form of commands to be |
| 240 | /// overridden. |
| 241 | /// |
| 242 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#shell). |
| 243 | pub struct Shell(Cow<'static, str>); |
| 244 | |
| 245 | impl Shell { |
| 246 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 247 | Shell(val.into()) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | impl fmt::Display for Shell { |
| 252 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 253 | write!(f, "SHELL {}\n", &self.0) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /// The `STOPSIGNAL` instruction sets the system call signal that will be sent to the container |
| 258 | /// to exit. |
| 259 | /// |
| 260 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#stopsignal). |
| 261 | pub struct Stopsignal(Cow<'static, str>); |
| 262 | |
| 263 | impl Stopsignal { |
| 264 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 265 | Stopsignal(val.into()) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | impl fmt::Display for Stopsignal { |
| 270 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 271 | write!(f, "STOPSIGNAL {}\n", &self.0) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /// The `USER` instruction sets the user name (or UID) and optionally the user group (or GID) to |
| 276 | /// use when running the image and for any `RUN`, `CMD` and `ENTRYPOINT` instructions that follow |
| 277 | /// it in the `Dockerfile`. |
| 278 | /// |
| 279 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#user). |
| 280 | pub struct User(Cow<'static, str>); |
| 281 | |
| 282 | impl User { |
| 283 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 284 | User(val.into()) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | impl fmt::Display for User { |
| 289 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 290 | write!(f, "USER {}\n", &self.0) |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | /// The `VOLUME` instruction creates a mount point with the specified name and marks it as holding |
| 295 | /// externally mounted volumes from native host or other containers. |
| 296 | /// |
| 297 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#volume). |
| 298 | pub struct Volume(Cow<'static, str>); |
| 299 | |
| 300 | impl Volume { |
| 301 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 302 | Volume(val.into()) |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | impl fmt::Display for Volume { |
| 307 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 308 | write!(f, "VOLUME {}\n", &self.0) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /// The `WORKDIR` instruction sets the working directory for any `RUN`, `CMD`, `ENTRYPOINT`, |
| 313 | /// `COPY` and `ADD` instructions that follow it in the `Dockerfile`. |
| 314 | /// |
| 315 | /// [See the docs here](https://docs.docker.com/engine/reference/builder/#workdir). |
| 316 | pub struct Workdir(Cow<'static, str>); |
| 317 | |
| 318 | impl Workdir { |
| 319 | pub fn new<T: Into<Cow<'static, str>>>(val: T) -> Self { |
| 320 | Workdir(val.into()) |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | impl fmt::Display for Workdir { |
| 325 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 326 | write!(f, "WORKDIR {}\n", &self.0) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 331 | // Instruction Enum ////////////////////////////////////////////////////////////////////////////// |
| 332 | |
| 333 | /// An enum representing all of the different types of Dockerfile instructions available. |
| 334 | /// |
| 335 | /// This should be the full list according to the latest |
| 336 | /// [Dockerfile spec here](https://docs.docker.com/engine/reference/builder/). If you notice any |
| 337 | /// missing instructions, please |
| 338 | /// [open an issue here](https://github.com/thedodd/dockerfile/issues/new). |
| 339 | pub enum Instruction { |
| 340 | Add(Add), |
| 341 | Arg(Arg), |
| 342 | Cmd(Cmd), |
| 343 | Copy(Copy), |
| 344 | Directive(Directive), |
| 345 | Entrypoint(Entrypoint), |
| 346 | Env(Env), |
| 347 | Expose(Expose), |
| 348 | From(From), |
| 349 | Healthcheck(Healthcheck), |
| 350 | Label(Label), |
| 351 | Onbuild(Onbuild), |
| 352 | Run(Run), |
| 353 | Shell(Shell), |
| 354 | Stopsignal(Stopsignal), |
| 355 | User(User), |
| 356 | Volume(Volume), |
| 357 | Workdir(Workdir), |
| 358 | } |
| 359 | |
| 360 | impl fmt::Display for Instruction { |
| 361 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 362 | match self { |
| 363 | Instruction::Add(inst) => write!(f, "{}", inst), |
| 364 | Instruction::Arg(inst) => write!(f, "{}", inst), |
| 365 | Instruction::Cmd(inst) => write!(f, "{}", inst), |
| 366 | Instruction::Copy(inst) => write!(f, "{}", inst), |
| 367 | Instruction::Directive(inst) => write!(f, "{}", inst), |
| 368 | Instruction::Entrypoint(inst) => write!(f, "{}", inst), |
| 369 | Instruction::Env(inst) => write!(f, "{}", inst), |
| 370 | Instruction::Expose(inst) => write!(f, "{}", inst), |
| 371 | Instruction::From(inst) => write!(f, "{}", inst), |
| 372 | Instruction::Healthcheck(inst) => write!(f, "{}", inst), |
| 373 | Instruction::Label(inst) => write!(f, "{}", inst), |
| 374 | Instruction::Onbuild(inst) => write!(f, "{}", inst), |
| 375 | Instruction::Run(inst) => write!(f, "{}", inst), |
| 376 | Instruction::Shell(inst) => write!(f, "{}", inst), |
| 377 | Instruction::Stopsignal(inst) => write!(f, "{}", inst), |
| 378 | Instruction::User(inst) => write!(f, "{}", inst), |
| 379 | Instruction::Volume(inst) => write!(f, "{}", inst), |
| 380 | Instruction::Workdir(inst) => write!(f, "{}", inst), |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | impl convert::From<Add> for Instruction { |
| 386 | fn from(inst: Add) -> Self { |
| 387 | |
| 388 | Instruction::Add(inst) |
| 389 | } |
| 390 | } |
| 391 | impl convert::From<Arg> for Instruction { |
| 392 | fn from(inst: Arg) -> Self { |
| 393 | |
| 394 | Instruction::Arg(inst) |
| 395 | } |
| 396 | } |
| 397 | impl convert::From<Cmd> for Instruction { |
| 398 | fn from(inst: Cmd) -> Self { |
| 399 | |
| 400 | Instruction::Cmd(inst) |
| 401 | } |
| 402 | } |
| 403 | impl convert::From<Copy> for Instruction { |
| 404 | fn from(inst: Copy) -> Self { |
| 405 | |
| 406 | Instruction::Copy(inst) |
| 407 | } |
| 408 | } |
| 409 | impl convert::From<Directive> for Instruction { |
| 410 | fn from(inst: Directive) -> Self { |
| 411 | |
| 412 | Instruction::Directive(inst) |
| 413 | } |
| 414 | } |
| 415 | impl convert::From<Entrypoint> for Instruction { |
| 416 | fn from(inst: Entrypoint) -> Self { |
| 417 | |
| 418 | Instruction::Entrypoint(inst) |
| 419 | } |
| 420 | } |
| 421 | impl convert::From<Env> for Instruction { |
| 422 | fn from(inst: Env) -> Self { |
| 423 | |
| 424 | Instruction::Env(inst) |
| 425 | } |
| 426 | } |
| 427 | impl convert::From<Expose> for Instruction { |
| 428 | fn from(inst: Expose) -> Self { |
| 429 | |
| 430 | Instruction::Expose(inst) |
| 431 | } |
| 432 | } |
| 433 | impl convert::From<From> for Instruction { |
| 434 | fn from(inst: From) -> Self { |
| 435 | |
| 436 | Instruction::From(inst) |
| 437 | } |
| 438 | } |
| 439 | impl convert::From<Healthcheck> for Instruction { |
| 440 | fn from(inst: Healthcheck) -> Self { |
| 441 | |
| 442 | Instruction::Healthcheck(inst) |
| 443 | } |
| 444 | } |
| 445 | impl convert::From<Label> for Instruction { |
| 446 | fn from(inst: Label) -> Self { |
| 447 | |
| 448 | Instruction::Label(inst) |
| 449 | } |
| 450 | } |
| 451 | impl convert::From<Onbuild> for Instruction { |
| 452 | fn from(inst: Onbuild) -> Self { |
| 453 | |
| 454 | Instruction::Onbuild(inst) |
| 455 | } |
| 456 | } |
| 457 | impl convert::From<Run> for Instruction { |
| 458 | fn from(inst: Run) -> Self { |
| 459 | |
| 460 | Instruction::Run(inst) |
| 461 | } |
| 462 | } |
| 463 | impl convert::From<Shell> for Instruction { |
| 464 | fn from(inst: Shell) -> Self { |
| 465 | |
| 466 | Instruction::Shell(inst) |
| 467 | } |
| 468 | } |
| 469 | impl convert::From<Stopsignal> for Instruction { |
| 470 | fn from(inst: Stopsignal) -> Self { |
| 471 | |
| 472 | Instruction::Stopsignal(inst) |
| 473 | } |
| 474 | } |
| 475 | impl convert::From<User> for Instruction { |
| 476 | fn from(inst: User) -> Self { |
| 477 | |
| 478 | Instruction::User(inst) |
| 479 | } |
| 480 | } |
| 481 | impl convert::From<Volume> for Instruction { |
| 482 | fn from(inst: Volume) -> Self { |
| 483 | |
| 484 | Instruction::Volume(inst) |
| 485 | } |
| 486 | } |
| 487 | impl convert::From<Workdir> for Instruction { |
| 488 | fn from(inst: Workdir) -> Self { |
| 489 | |
| 490 | Instruction::Workdir(inst) |
| 491 | } |
| 492 | } |