Docker Tip #17: Breaking Up Long Lines in Your Dockerfile
Breaking up long lines in your Dockerfile will make your Dockerfile much easier to skim and read. Here's how to do it.
I don’t know about you but when coding or dealing with config files, I try my best to limit things to 79 characters per line. Not only is it a standard, but it also happens to be the perfect width for me to comfortably fit 3 windows side by side in Sublime Text on my 2560x1440 monitor.
When it comes to writing out your
Dockefile, you’ll likely end up chaining together commands to make them more efficient. However, that’s going to create very long lines.
For example, look at this 122 character multi-command monstrosity:
RUN wget -O myfile.tar.gz http://example.com/myfile.tar.gz && tar -xvf myfile.tar.gz -C /usr/src/myapp && rm myfile.tar.gz
My website’s theme is smart enough to wrap the line so it doesn’t break the layout of the page but in a code editor this would all be on 1 line. Having to scroll horizontally to see the end of a line is super duper annoying.
Here’s the same command except broken into multiple lines:
RUN wget -O myfile.tar.gz http://example.com/myfile.tar.gz \ && tar -xvf myfile.tar.gz -C /usr/src/myapp \ && rm myfile.tar.gz
Notice how much easier it is to skim and read.
There’s no magic or special things happening here. Using a backslash is the standard way to break up long lines into multiple lines on Unix-like operating systems. Since most Docker images are built using some form of Linux base image, then the backslash strategy works here as well. Docker also allows for it in other
Dockerfile instructions (such as