Search code examples
dockerdockerfilelinter

Leading spaces inside Dockerfile for readability


Am I able to use indentation in a Dockerfile?

Is there anything wrong with using spaces for indenting like this?

FROM python:3.8-buster
  RUN pip --no-cache-dir install poetry gunicorn

  WORKDIR /app
    COPY poetry.toml pyproject.toml poetry.lock /app/
    RUN poetry export --dev -f requirements.txt > requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt

  WORKDIR /app/src
    COPY src /app/src
    RUN ./manage.py collectstatic --noinput --clear

  CMD ["gunicorn", "--bind", ":8000", "wsgi:application"]

Building such docker image seems to work fine.


Solution

  • You can indent lines in Dockerfile, but usually it's used only when breaking long command lines, like:

    RUN export ADMIN_USER="mark" \
        && echo $ADMIN_USER > ./mark \
        && unset ADMIN_USER
    

    You can use indenting for instructions, but i, personally, wouldn't do that -- each instruction creates new layer and it's logical to place them with equal indent. As extra indenting like:

    FROM python:3.8-buster
      RUN pip --no-cache-dir install poetry gunicorn
    

    would look like it introduces sub-layers(and Docker doesn't have such concept).

    But again, that's personal, and if you and your team agrees on that formatting standard -- there's a bunch of linters that would allow you to use any formatting standard with little(or no) tweaking: