Skip to content

Commit a243b1b

Browse files
committed
feat(agents): filter dockerignore files from entrypoint match
1 parent 0cb6b70 commit a243b1b

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/livekit/protocol v1.39.4-0.20250718180328-5934235d53ec
1717
github.com/livekit/server-sdk-go/v2 v2.9.2-0.20250721202815-cc45e29e93d6
1818
github.com/moby/buildkit v0.22.0
19+
github.com/moby/patternmatcher v0.6.0
1920
github.com/pion/rtcp v1.2.15
2021
github.com/pion/rtp v1.8.20
2122
github.com/pion/webrtc/v4 v4.1.3
@@ -130,7 +131,6 @@ require (
130131
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
131132
github.com/moby/docker-image-spec v1.3.1 // indirect
132133
github.com/moby/locker v1.0.1 // indirect
133-
github.com/moby/patternmatcher v0.6.0 // indirect
134134
github.com/moby/sys/signal v0.7.1 // indirect
135135
github.com/morikuni/aec v1.0.0 // indirect
136136
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect

pkg/agentfs/docker.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"strings"
2626

2727
"github.com/charmbracelet/huh"
28+
"github.com/moby/patternmatcher"
29+
"github.com/moby/patternmatcher/ignorefile"
2830
"github.com/pkg/errors"
2931

3032
"github.com/livekit/livekit-cli/v2/pkg/util"
@@ -72,7 +74,7 @@ func CreateDockerfile(dir string, settingsMap map[string]string) error {
7274

7375
// TODO: (@rektdeckard) support Node entrypoint validation
7476
if projectType.IsPython() {
75-
dockerfileContent, err = validateEntrypoint(dir, dockerfileContent, projectType, settingsMap)
77+
dockerfileContent, err = validateEntrypoint(dir, dockerfileContent, dockerIgnoreContent, projectType, settingsMap)
7678
if err != nil {
7779
return err
7880
}
@@ -91,16 +93,32 @@ func CreateDockerfile(dir string, settingsMap map[string]string) error {
9193
return nil
9294
}
9395

94-
func validateEntrypoint(dir string, dockerfileContent []byte, projectType ProjectType, settingsMap map[string]string) ([]byte, error) {
96+
func validateEntrypoint(dir string, dockerfileContent []byte, dockerignoreContent []byte, projectType ProjectType, settingsMap map[string]string) ([]byte, error) {
9597
valFile := func(fileName string) (string, error) {
9698
// NOTE: we need to recurse to find entrypoints which may exist in src/ or some other directory.
9799
// This could be a lot of files, so we omit any files in .dockerignore, since they cannot be
98100
// used as entrypoints.
101+
102+
reader := bytes.NewReader(dockerignoreContent)
103+
patterns, err := ignorefile.ReadAll(reader)
104+
if err != nil {
105+
return "", err
106+
}
107+
matcher, err := patternmatcher.New(patterns)
108+
if err != nil {
109+
return "", err
110+
}
111+
99112
var fileList []string
100113
if err := filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
101114
if err != nil {
102115
return err
103116
}
117+
if ignored, err := matcher.MatchesOrParentMatches(path); ignored {
118+
return nil
119+
} else if err != nil {
120+
return err
121+
}
104122
if !d.IsDir() && strings.HasSuffix(d.Name(), ".py") {
105123
fileList = append(fileList, path)
106124
}
@@ -129,8 +147,7 @@ func validateEntrypoint(dir string, dockerfileContent []byte, projectType Projec
129147
),
130148
)
131149

132-
err := form.Run()
133-
if err != nil {
150+
if err := form.Run(); err != nil {
134151
return "", err
135152
}
136153

@@ -153,7 +170,9 @@ func validateEntrypoint(dir string, dockerfileContent []byte, projectType Projec
153170
line := lines[i]
154171
trimmedLine := bytes.TrimSpace(line)
155172

156-
if bytes.HasPrefix(trimmedLine, []byte("ENTRYPOINT")) {
173+
if bytes.HasPrefix(trimmedLine, []byte("ARG PROGRAM_MAIN")) {
174+
result.WriteString(fmt.Sprintf("ARG PROGRAM_MAIN=\"%s\"", newEntrypoint))
175+
} else if bytes.HasPrefix(trimmedLine, []byte("ENTRYPOINT")) {
157176
// Extract the current entrypoint file
158177
parts := bytes.Fields(trimmedLine)
159178
if len(parts) < 2 {

pkg/agentfs/examples/python.pip.Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ FROM python:${PYTHON_VERSION}-slim
77
# the application crashes without emitting any logs due to buffering.
88
ENV PYTHONUNBUFFERED=1
99

10+
# Define the program entrypoint file where your agent is started
11+
ARG PROGRAM_MAIN="src/agent.py"
12+
1013
# Create a non-privileged user that the app will run under.
1114
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
1215
ARG UID=10001
@@ -39,10 +42,10 @@ RUN python -m pip install --user --no-cache-dir -r requirements.txt
3942
COPY . .
4043

4144
# ensure that any dependent models are downloaded at build-time
42-
RUN python main.py download-files
45+
RUN python "$PROGRAM_MAIN" download-files
4346

4447
# expose healthcheck port
4548
EXPOSE 8081
4649

4750
# Run the application.
48-
CMD ["python", "main.py", "start"]
51+
CMD ["python", "$PROGRAM_MAIN", "start"]

pkg/agentfs/examples/python.uv.Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim
1010
# the application crashes without emitting any logs due to buffering.
1111
ENV PYTHONUNBUFFERED=1
1212

13+
# Define the program entrypoint file where your agent is started
14+
ARG PROGRAM_MAIN="src/agent.py"
15+
1316
# Create a non-privileged user that the app will run under.
1417
# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user
1518
ARG UID=10001
@@ -61,7 +64,7 @@ RUN uv sync --locked
6164
# Pre-download any ML models or files the agent needs
6265
# This ensures the container is ready to run immediately without downloading
6366
# dependencies at runtime, which improves startup time and reliability
64-
RUN uv run src/agent.py download-files
67+
RUN uv run "$PROGRAM_MAIN" download-files
6568

6669
# Expose the healthcheck port
6770
# This allows Docker and orchestration systems to check if the container is healthy
@@ -70,4 +73,4 @@ EXPOSE 8081
7073
# Run the application using UV
7174
# UV will activate the virtual environment and run the agent
7275
# The "start" command tells the worker to connect to LiveKit and begin waiting for jobs
73-
CMD ["uv", "run", "src/agent.py", "start"]
76+
CMD ["uv", "run", "$PROGRAM_MAIN", "start"]

0 commit comments

Comments
 (0)