Ruff is a fast Python linter and formatter designed to improve code quality and enforce style guidelines. Designed for high performance, Ruff offers an extensive set of linting rules and checks to identify common Python issues.
This tutorial shows how to run Ruff inside Docker container on Linux. Commands have been tested on Ubuntu.
First, clone the sample Python project that you want to check. For example:
git clone https://github.com/Asabeneh/30-Days-Of-Python && cd 30-Days-Of-Python
This will clone the repository and move into the project directory.
Without installing Ruff directly on the system, we can run it inside a Docker container for checking Python code. Execute the following command within the project directory:
docker run -it --rm -v ./:/code pipelinecomponents/ruff ruff check .
Explanation of the command:
docker run
- runs a new container.-it
- initiates an interactive terminal session.--rm
- deletes the container after execution to maintain a clean environment.-v ./:/code
- mounts the current directory (./
) to the/code
directory inside the container. This enables Ruff within the container to interact with local files.pipelinecomponents/ruff
- specifies the Docker image to use.ruff check .
- runs Ruff to check all Python files in the current directory and any subdirectories.
Ruff will analyze the Python files and provide warnings or errors based on coding standards. An example output may appear as follows:
03_Day_Operators/day-3.py:99:25: E712 Avoid equality comparisons to `True`; use `if True:` for truth checks
|
98 | # Boolean comparison
99 | print('True == True: ', True == True)
| ^^^^^^^^^^^^ E712
100 | print('True == False: ', True == False)
101 | print('False == False:', False == False)
|
= help: Replace with `True`
...
Leave a Comment
Cancel reply