Paweł Smolak
Paweł Smolak

Reputation: 633

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.

Upvotes: 6

Views: 4670

Answers (2)

Yasen
Yasen

Reputation: 4504

Format of Dockerfile

Dockerfile format requirements are pretty concise:

Here is the format of the Dockerfile:

# Comment
INSTRUCTION arguments

The instruction is not case-sensitive. However, convention is for them to be UPPERCASE to distinguish them from arguments more easily.

There isn't any suggestions on line indentation.

Use linters

There are many Dockerfile linters, so general answer is: always try to lint your development process.

This answer says:

Let's check your Dockerfile with linters

FROM:latest

FROM:latest says:

No problems or suggestions found!

hadolint:

hadolint says:

Upvotes: 2

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5573

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:

Upvotes: 4

Related Questions