Dockerfile RUN command mangled. How do I escape it properly?

Using ubuntu:latest docker image…

This command works fine at the command line:

if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

[email protected]:/# if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi
it worked

But in a Dockerfile RUN command, it doesn’t:

FROM ubuntu
RUN if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi

sh: 1: [: hi there: unexpected operator

I’ve tried escaping ", backtick, [ & ]. I’ve tried using # escape=… I’ve tried JSON syntax:

RUN ["sh", "-c", "if [ "`echo hi there`" == "hi there" ]; then echo it worked; else echo nope; fi"]

…Just can’t seem to find the right way to do it. Where am I going wrong?

Source: StackOverflow