<p>To error out the build, just add the following with your env variable in the docker-compose file.</p>

<pre><code>${ENV_VAR?Variable ENV_VAR not set}
</code></pre>

<p>example:</p>

<pre><code>image: consul:${TAG?Variable TAG not set}
</code></pre>

CC BY-SA 4.0
2022-05-15T13:56:12.317Z
Thanks! Used this and it works great. I guess OP asked about Docker and not Docker Compose. Still, this worked great for me as I am using Compose.
Stijn de Witt
CC BY-SA 4.0
2020-02-24T12:39:07.407Z
2020-02-24T12:39:07.407Z
Anurag Choudhary
<p>If the <code>if</code> command construct is very complex for you, you can always simply it like</p>
<pre><code>RUN [ -n &quot;$VAR&quot; ]
</code></pre>
<p>This asserts the variable value length is nonzero. If it's empty or not set it'll exit with 1. This is also same as running <code>RUN test -n &quot;$VAR&quot;</code></p>
<p>I see this error message on my machine</p>
<pre><code>The command '/bin/sh -c [ -n &quot;$VAR&quot; ]' returned a non-zero code: 1
</code></pre>

CC BY-SA 4.0
2020-11-25T17:40:52.583Z
2020-11-25T17:40:52.583Z
Shiplu Mokaddim
<p>An empty of non existent environment variable is the same thing, there's no reason Docker do not allow you to pass an empty variable as it could be a valid use case.</p>
<p>It's up to your responsibility to ensure needed environment variables are there and give an error message back when not.</p>
<p>Something like this:</p>
<pre><code>RUN if [ -z &quot;$AN_ENV_VAR&quot; ]; then echo 'Environment variable AN_ENV_VAR must be specified. Exiting.'; exit 1; fi
</code></pre>
<p>You can play with the exit code to automate some actions on failure around your <code>docker build</code> command.</p>

CC BY-SA 4.0
2021-09-21T09:33:48.280Z
Quotes in the RUN command are unneeded and error-prone
CharlesB
CC BY-SA 4.0
2021-09-21T13:14:27.817Z
@CharlesB  Wasn't the case if you had `;` in your RUN command 4 years ago... A bit lazy to try it again right now TBH.
Tensibai
CC BY-SA 4.0
2021-09-21T14:25:15.693Z
I had errors trying it just now, but without quotes it wet well
CharlesB
CC BY-SA 4.0
2021-09-21T15:22:47.033Z
Indeed, I didn't escape singles quotes within the command back then. Well, feel free to edit
Tensibai
CC BY-SA 4.0
2017-07-19T07:31:41.617Z
2021-09-22T15:40:46.360Z
2021-09-22T15:40:46.360Z
Tensibai
<p><a href="https://github.com/fabric8io/docker-maven-plugin/issues/434" rel="noreferrer">This github discussion</a> was found. It seems that it is allowed to build a docker image while a certain ENV does not have been set.</p>

<p>How to ensure that the docker build fails if an ENV has not been set?</p>

CC BY-SA 3.0
2017-07-17T09:24:02.933Z
`RUN 'if [ -Z $ENV ]; then exit 1; fi'` ?
Tensibai
CC BY-SA 3.0
2017-07-17T09:26:16.003Z
Yes, but I would like to see that docker build itself denies it
030
CC BY-SA 3.0
2017-07-17T09:30:18.597Z
Why that ? An empty of non existent environment variable is the same thing, there's no reason Docker do not allow you to pass an empty variable as it could be a very valid use case... It's up to your responsibility to ensure needed environment variables are there and give an error message back when not.
Tensibai
CC BY-SA 3.0
2017-07-19T07:09:56.747Z
@Tensibai perhaps you could combine both comments and post it as an answer so it could be accepted
030
<docker>
How to ensure that the docker build fails if an ENV has not been set?
030
# How to ensure that the docker build fails if an ENV has not been set?

(Asked by: username_0 on 2017-07-17)

[This github discussion](https://github.com/fabric8io/docker-maven-plugin/issues/434) was found. It seems that it is allowed to build a docker image while a certain ENV does not have been set.

How to ensure that the docker build fails if an ENV has not been set?

**Tags:** docker

> **Comments:**
> **username_1**: `RUN 'if [ -Z $ENV ]; then exit 1; fi'` ?  
> **username_0**: Yes, but I would like to see that docker build itself denies it  
> **username_1**: Why that ? An empty of non existent environment variable is the same thing, there's no reason Docker do not allow you to pass an empty variable as it could be a very valid use case... It's up to your responsibility to ensure needed environment variables are there and give an error message back when not.  
> **username_0**: @username_1 perhaps you could combine both comments and post it as an answer so it could be accepted  

---

## 3 answers

### Answer by username_1 on 2017-07-19. Score: 8 | Accepted answer

An empty of non existent environment variable is the same thing, there's no reason Docker do not allow you to pass an empty variable as it could be a valid use case.
It's up to your responsibility to ensure needed environment variables are there and give an error message back when not.
Something like this:
RUN if [ -z "$AN_ENV_VAR" ]; then echo 'Environment variable AN_ENV_VAR must be specified. Exiting.'; exit 1; fi

You can play with the exit code to automate some actions on failure around your docker build command.

> **Comments:**
> **username_5**: Quotes in the RUN command are unneeded and error-prone  
> **username_1**: @username_5  Wasn't the case if you had `;` in your RUN command 4 years ago... A bit lazy to try it again right now TBH.  
> **username_5**: I had errors trying it just now, but without quotes it wet well  
> **username_1**: Indeed, I didn't escape singles quotes within the command back then. Well, feel free to edit  

---

### Answer by username_2 on 2020-02-24. Score: 4

To error out the build, just add the following with your env variable in the docker-compose file.
${ENV_VAR?Variable ENV_VAR not set}

example:
image: consul:${TAG?Variable TAG not set}

> **Comments:**
> **username_3**: Thanks! Used this and it works great. I guess OP asked about Docker and not Docker Compose. Still, this worked great for me as I am using Compose.  

---

### Answer by username_4 on 2020-11-25. Score: 2

If the if command construct is very complex for you, you can always simply it like
RUN [ -n "$VAR" ]

This asserts the variable value length is nonzero. If it's empty or not set it'll exit with 1. This is also same as running RUN test -n "$VAR"
I see this error message on my machine
The command '/bin/sh -c [ -n "$VAR" ]' returned a non-zero code: 1


---

