Table of Contents
Build a Docker image containing your Python script, then start it with docker run --rm. Use a bind mount only if the script needs host files. The example below sorts copies of test files in a dedicated folder. Changes made inside a writable mount affect the actual files on your computer.
Create the Python script
Create a project folder and save this as organize_files.py. It processes regular files at the top level of the mounted directory, skips symbolic links, and avoids overwriting existing destinations. Run one instance at a time.
from pathlib import Path
import shutil
source = Path("/files")
for path in sorted(source.iterdir()):
if path.is_symlink() or not path.is_file():
continue
extension = path.suffix.lower().lstrip(".")
folder = source / (extension or "no_extension")
if folder.is_symlink() or (folder.exists() and not folder.is_dir()):
print(f"Skipping {path.name}: invalid destination folder")
continue
target = folder / path.name
if target.exists() or target.is_symlink():
print(f"Skipping {path.name}: destination already exists")
continue
folder.mkdir(exist_ok=True)
shutil.move(str(path), str(target))
print(f"Moved {path.name} to {folder.name}/")
Write the Dockerfile and build the image
Save a file named Dockerfile next to the script. This example uses an official Python image tagged to a specific minor release. It needs no third-party Python packages.
FROM python:3.13-slim
WORKDIR /app
COPY organize_files.py .
CMD ["python", "organize_files.py"]

With Docker running, build from the directory containing both files. The final dot sets the build context to the current directory.
docker build -t file-organizer .

Run the container with a test folder
In Bash or another compatible shell, run the following from the project folder. For PowerShell, create the same three test files and use an absolute path in place of $(pwd)/sample_files.
mkdir sample_files
touch sample_files/test.txt sample_files/image.jpg sample_files/data.csv
docker run --rm --mount "type=bind,source=$(pwd)/sample_files,target=/files" file-organizer

Open sample_files in your file manager and verify there are now txt, jpg, and csv folders with the expected files. The --rm flag removes the stopped container. Your image and the moved host files remain.

Troubleshoot the mount
If Docker says the source path does not exist, create it on the Docker host before running. If it reports a permission error, check the folder’s host permissions and Docker Desktop file sharing where applicable. On Linux, you may need to run the container with your user ID and group ID to keep created files writable by your account. For scripts that only read input, use a readonly bind mount. See the Docker bind mount reference and TipsMake’s Docker course. To extend the example to an API, try the Python containers and deployment walkthrough.
Reader Comments 0
Sign in with email or Google to join the discussion.