Dockerfile Best Practices.

Writing a good Dockerfile is important when you containerize your application.

📝Dockerfile: is a text document that contains all the commands a user could call on the command line to assemble an image.

Below, a Thread of best practices per KEYWORD.
🏠FROM

You should ALWAYS use official images as the base image.

The good rule is to never use random things on the internet (it also works for GitHub, npm, ...)
🏃RUN

Here is a matter of readability.

Dockerfiles are read by humans, so you should split long or complex RUN statements into multiple lines separated by backslashes
📢CMD

The CMD instruction should be used to run the software contained in your image, along with any arguments.

This keyword should be used in the form of

CMD ["executable", "param1", "param2"…]
🏷️LABEL

For each label, add a line beginning with LABEL,

followed by one or more key-value pairs.
🚪EXPOSE

The EXPOSE instruction indicates the ports on which a container listens for connections.

Use the common port for your application.
🌆ENV
It can be used in a good way to:

-set commonly used version numbers so that version bumps are easier to maintain

-update the PATH env variable for the software your container installs.

-provide required environment variables specific to services you wish to containerize
➕ADD or COPY

For being more transparent, COPY is preferred.

If you have multiple Dockerfile steps that use different files from your context, COPY them individually, rather than all at once.

use curl or wget instead of ADD
➡️ENTRYPOINT

The best use for ENTRYPOINT is to set the image’s main command, allowing that image to be run as though it was that command (and then use CMD as the default flags).
💿VOLUME

Use it to expose any:

- database storage area
- configuration storage
- files/folders

created by your docker container.
👩‍💻👨‍💻USER

Don't switch Users too often.

If a service can run without privileges, use USER to change to a non-root user.

Avoid installing or using sudo
📂WORKDIR

use absolute paths for your WORKDIR

use WORKDIR instead of RUN cd … && do-something
🏗️ONBUILD

Images built with ONBUILD should get a separate tag.

It is also useful for images that are going to be built FROM a given image.
You can follow @FrancescoCiull4.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.