RUN inside a conditional statement in Dockerfile

2019-06-14 02:44发布

问题:

I have a Dockerfile which currently uses the following:

COPY ./compose/local/django/start.sh /start.sh
RUN sed -i 's/\r//' /start.sh
RUN chmod +x /start.sh

As you can see it copies the file and then makes it executable. I now need to change this depending on a argument provided when I build the image. I have tried using this:

RUN if [ "$arg" = "True" ]; then \
    COPY ./compose/local/django/new.sh /new.sh \
    RUN sed -i 's/\r//' /new.sh \
    RUN chmod +x /new.sh; else \
    COPY ./compose/local/django/start.sh /start.sh \
    RUN sed -i 's/\r//' /start.sh \
    RUN chmod +x /start.sh; fi

But this fails as it appears that I can't run COPY or RUN commands inside a conditional statement. It always fails with:

/bin/sh: 1: COPY: not found

or

/bin/sh: 1: RUN: not found

So, I think that the best course of action is to create a separate bash file which does the copies, something like:

#!/usr/bin/env bash

if [ "${arg}" = "True" ]; then
    echo "arg = ${arg}"
    cp ./compose/local/django/new.sh /new.sh
elif [ "${arg}" = "False" ]; then
    echo "arg = ${arg}"
    cp ./compose/local/django/start.sh /start.sh
fi

But I am struggling with how to make the copied bash files executable. I know I can do it by running chmod +x in the command line, but as I am not the only person who is going to be building this, I was hoping that there would be a better solution, something like I was doing previously in the original Dockerfile script.

Can anyone help me with this? Any help would be much appreciated.

回答1:

Use only one 'RUN' statement. It's a best practice as it will reduce the size taken by your image.

This is how you can pass an argument to docker build.

Dockerfile:

...
ARG NEW
COPY ./compose/local/django/new.sh /new.sh
COPY ./compose/local/django/start.sh /start.sh

RUN if [ "$NEW" = "True" ]; then \
    sed -i 's/\r//' /new.sh && \
    chmod +x /new.sh; \
  else \
    sed -i 's/\r//' /start.sh && \
    chmod +x /start.sh; \
  fi

and build it like this

docker build --build-arg NEW="True" .


标签: docker