Leading spaces inside Dockerfile for readability
Asked Answered
P

2

6

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.

Paperboard answered 21/4, 2020 at 6:42 Comment(0)
C
4

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:

Constable answered 21/4, 2020 at 7:12 Comment(0)
I
2

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:

Isaac answered 21/4, 2020 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.