Run SQLFluff Inside Docker Container on Linux

Run SQLFluff Inside Docker Container on Linux

SQLFluff is an open-source SQL linter and formatter designed to enforce SQL style consistency and catch errors in SQL code. It supports multiple SQL dialects, including ANSI, PostgreSQL, MySQL, and more. SQLFluff helps developers maintain clean, readable, and standardized SQL code by detecting syntax issues, and enforcing best practices.

This tutorial shows how to run SQLFluff inside Docker container on Linux. Commands have been tested on Ubuntu.

For testing purposes, create and navigate to a directory:

mkdir sqls && cd sqls

Create a SQL file:

echo 'SELECT a  +  b FROM tbl;' > test.sql

Rather than installing SQLFluff manually, you can use a pre-built Docker image. Simply run the following command:

docker run -it --rm -v ./:/sql sqlfluff/sqlfluff lint . --dialect ansi

Explanation of the command:

  • docker run - runs a new container.
  • -it - initiates an interactive terminal session.
  • --rm - deletes the container after execution, ensuring a clean environment.
  • -v ./:/sql mounts the current directory (./) to /sql inside the container. This enables SQLFluff within the container to interact with local files.
  • sqlfluff/sqlfluff - specifies the Docker image to use.
  • lint . - tells checking all SQL files in the current directory.
  • --dialect ansi - specifies the SQL dialect to use (ANSI is a generic SQL standard).

SQLFluff will analyze SQL files and return warnings/errors if it finds formatting issues, such as unnecessary spaces. Output:

== [test.sql] FAIL
L:   1 | P:   9 | LT01 | Expected only single space before binary operator '+'.
                       | Found '  '. [layout.spacing]
L:   1 | P:  12 | LT01 | Expected only single space before naked identifier.
                       | Found '  '. [layout.spacing]

Leave a Comment

Cancel reply

Your email address will not be published.