Reputation: 633
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
Reputation: 4504
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.
There are many Dockerfile
linters, so general answer is: always try to lint your development process.
NPM
-based dockerfile_lint by projectatomic. It's a former redhataccess project. If you have a RedHat account, you can try it onlineNode.js
-based dockerfile-linter by buddy-worksThis answer says:
- Either the Haskell Dockerfile Linter ("hadolint").
hadolint
parses the Dockerfile into an AST and performs checking and validation based on best practice Docker images rules. It also uses Shellcheck to lint the Bash code onRUN
commands.
Or dockerlinter (node.js-based).
hadolint
available online: https://hadolint.github.io/hadolint/
Yet another linter: GitHub - replicatedhq/dockerfilelint: An opinionated Dockerfile linter.
Online version: https://www.fromlatest.io
Dockerfile
with lintersFROM:latest
FROM:latest
says:
No problems or suggestions found!
hadolint
:hadolint
says:
Upvotes: 2
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