Home > Software engineering >  Why do I get Syntax error "}" unexpected in building Dockerfile?
Why do I get Syntax error "}" unexpected in building Dockerfile?

Time:01-22

When I try to build Dockerfile with this line of code:

RUN modifyPermissions() {if [ -n "${OWNER}" ]; then \
                           chown "${OWNER}":zagrebgroup -R /var/log/app /mnt;\
                         else\
                           chown 1001:zagrebgroup -R /var/log/app /mnt;\
                         fi\
                        }

I have this error:

/bin/sh: 1: Syntax error: "}" unexpected

Looks like the problem is in last curly bracket but I can't understand why. Please help! Tnx!

CodePudding user response:

The source of the original syntax error is that { must be followed by whitespace. This would be syntactically correct:

RUN modifyPermissions() { if [ -n "${OWNER}" ]; then \
                           chown "${OWNER}":zagrebgroup -R /var/log/app /mnt;\
                         else\
                           chown 1001:zagrebgroup -R /var/log/app /mnt;\
                         fi\
                        }

But as folks pointed out in the comments, each RUN command executes in a separate shell, so functions, variables, etc, defined in one RUN statement won't be available to others. The only way a function like this would be useful would be if you used the function within the same RUN script:

RUN modifyPermissions() { if [ -n "${OWNER}" ]; then \
                           chown "${OWNER}":zagrebgroup -R /var/log/app /mnt;\
                         else\
                           chown 1001:zagrebgroup -R /var/log/app /mnt;\
                         fi\
                        }; \
                        modifyPermissions

...and in the case, you haven't saved yourself any effort; you might as well simplify the entire thing to:

RUN chown "${OWNER:-1001}":zagrebgroup -R /var/log/app /mnt

That accomplishes the same thing in a much simpler fashion.

  •  Tags:  
  • Related