62 lines
1.5 KiB
Python
Executable File
62 lines
1.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import shutil
|
|
import subprocess
|
|
|
|
|
|
DOCKERFILE_TEXT = """
|
|
FROM busybox
|
|
COPY . /build-context
|
|
WORKDIR /build-context
|
|
CMD find .
|
|
"""
|
|
|
|
def dockerignore_check(args):
|
|
ignore_file = Path(args.file)
|
|
|
|
if not ignore_file.exists():
|
|
print(f"Dockerignore file {ignore_file} doesn't exist.")
|
|
|
|
with tempfile.TemporaryDirectory() as td:
|
|
dpath = Path(td)
|
|
|
|
docker_path = dpath / "Dockerfile"
|
|
ignore_path = dpath / "Dockerfile.dockerignore"
|
|
docker_path.write_text(DOCKERFILE_TEXT)
|
|
shutil.copyfile(ignore_file, ignore_path)
|
|
|
|
env = os.environ.copy()
|
|
env["DOCKER_BUILDKIT"] = "1"
|
|
subprocess.run(
|
|
f"docker image build --no-cache -t dockerignore-build-context -f {docker_path} .".split(),
|
|
env=env,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
|
|
subprocess.run(
|
|
"docker run -it --rm dockerignore-build-context".split(),
|
|
env=env,
|
|
)
|
|
|
|
subprocess.run(
|
|
"docker image rm dockerignore-build-context".split(),
|
|
env=env,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
parser = argparse.ArgumentParser(
|
|
",di",
|
|
description="Shows all files in a project seen by Docker after applying an ignore file.",
|
|
)
|
|
parser.add_argument("file")
|
|
args = parser.parse_args()
|
|
dockerignore_check(args)
|