user1424739
user1424739

Reputation: 13645

How to terminate a dockerfile?

I use # to comment out the commands that I don't need in a Dockerfile. Does anybody know if there is a command (something like exit) to terminate a Dockfile so that all the lines below it won't be run? Thanks.

Upvotes: 4

Views: 3001

Answers (3)

Dana Leaman
Dana Leaman

Reputation: 125

For debugging purposes, you can halt the execution of the Dockerfile by adding a call that produces an error. I like to use RUN exit 1 to have the shell produce a nonzero exit status:

# Lines you want to execute
FROM ubuntu
RUN echo "Hello world!" > /hello

# Halt execution
RUN exit 1

# Lines that will not be executed:
RUN rm -rf /

On my system this results in the output:

$ docker build .
(several lines skipped)
 => [2/4] RUN echo "Hello world!" > /hello                                 2.7s
 => ERROR [3/4] RUN exit 1                                                 0.7s
------
 > [3/4] RUN exit 1:
------
Dockerfile:6
--------------------
   4 |
   5 |     # Halt execution
   6 | >>> RUN exit 1
   7 |
   8 |     # Lines that will not be executed:
--------------------
ERROR: failed to solve: process "/bin/sh -c exit 1" did not complete successfully: exit code: 1

$

Upvotes: 2

HAltos
HAltos

Reputation: 755

I'll provide my use case, as the original poster has not responded. The use case is for debugging purpose, and I may be making iterative changes. Say I have a 100-line Dockerfile, and I want to run till line 20 and then quit. I could do it in bash script (by adding statement exit 0). So does Dockerfile support something like exit? This feature is definitely only nice-to-have, as I know I can simply make a copy and remove the rest, but it is just additional steps I would like to avoid. So this is try to reduce edit-copy-run to edit-run I am doing many trial-and-error iterations.

Upvotes: 1

Artur Karbone
Artur Karbone

Reputation: 1658

Technically I see coupe of options:

  1. Comments # are the way to go.
  2. Create two separate docker files. Like Dockerfile.dev or Dockerfile.prod and specify them via build (which I personally do not like and do not practice)

Pls describe your initial intent (why do you need it) and maybe we will figure out an appropriate solution.

Upvotes: 3

Related Questions