5

I am new to docker, I am looking for a way to execute a command in docker container depends on the environment.

In Dockerfile, I have 2 commands, command_a and command_b. If the env = 'prod' run command_a, else command_b. How can I achieve this?

I tried like below:

RUN if [ $env = "prod" ] ; then echo command_a; else echo cpmmand_b; fi;

How can I achieve the desired behaviour?

PS: I know that echo should not be there.

Pradeepb
  • 2,564
  • 3
  • 25
  • 46
  • 1
    I think you've got the right way to do that. Docker doesn't provide conditional statements – gcharbon Aug 16 '18 at 13:12
  • 1
    And what happened when you tried that? – Henry Aug 16 '18 at 13:13
  • Well, it evaluates the condition in the RUN statement, and everything goes like in bash or shell – gcharbon Aug 16 '18 at 13:14
  • @Henry - It ofcourse just printed :D It did not execute that. – Pradeepb Aug 16 '18 at 13:14
  • 2
    So basically it worked as intended. Now just replace the `echo command_a` with the command you want to run. – Henry Aug 16 '18 at 13:16
  • If you want to template your Dockerfile like a boss, you can use python with an `ini` file, `configparser` module, and then `jinja2`. You can replace the config file with any other way to parse configuration. This is how I do when I have a lot of conditional statements in my Dockerfile. And this way I can include blocks and not only evaluate a condition in a RUN statement – gcharbon Aug 16 '18 at 13:16
  • @Henry - I had already tried that but did not work. But what worked for me is setting up the commands in variables and then replacing `echo command_a` with a variable name, worked like a charm. I will post it as a answer after sometime if I don't find any other alternatives. – Pradeepb Aug 16 '18 at 13:36
  • Possible duplicate of https://stackoverflow.com/questions/43654656/dockerfile-if-else-condition-with-external-arguments – Meiyappan Kannappa Aug 16 '18 at 13:44
  • Possible duplicate of [Dockerfile if else condition with external arguments](https://stackoverflow.com/questions/43654656/dockerfile-if-else-condition-with-external-arguments) – David Maze Aug 16 '18 at 14:17
  • Just to note, best practice is generally to run the same image in all environments and control “dev” or “prod” with environment variables. – David Maze Aug 16 '18 at 14:18
  • Why would you link to those 2 questions? Even if `env` is given as build argument, user can still refer to it as `$env` and test its value with `if [ "$env" = "foo" ] ; then ... ; else ... ; fi ;`. I don't see what changes being in Dockerfile implies? Could you explain it a little ? – gcharbon Aug 16 '18 at 14:41
  • Put the logic into a shell script file, copy it into the image and run it there. – axiac Aug 19 '18 at 19:02

1 Answers1

3

Docker 17.05 and later supports a kind of conditionals using multi-stage build and build args. Have a look at https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae

From the blog post:

ARG BUILD_VERSION=1
FROM alpine AS base
RUN ...
FROM base AS branch-version-1
RUN touch version1
FROM base AS branch-version-2
RUN touch version2
FROM branch-version-${BUILD_VERSION} AS after-condition
FROM after-condition 
RUN ...

And then use docker build --build-arg BUILD_VERSION=value ...

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57