Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .dockerignore ignoring the .cog directory we produce #1962

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions pkg/docker/build.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,60 @@
package docker

import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"path"
"runtime"
"strings"

"github.com/replicate/cog/pkg/config"

"github.com/replicate/cog/pkg/util"
"github.com/replicate/cog/pkg/util/console"
"github.com/replicate/cog/pkg/util/files"
)

func sanitiseDockerIgnore(dir string) error {
dockerIgnorePath := path.Join(dir, ".dockerignore")
fileExists, err := files.Exists(dockerIgnorePath)
if err != nil {
return err
}
if !fileExists {
return nil
}

console.Debug("Sanitising .dockerignore.")
file, err := os.Open(dockerIgnorePath)
if err != nil {
log.Fatal(err)
}

scanner := bufio.NewScanner(file)
newContents := ""
for scanner.Scan() {
line := scanner.Text()
if line == ".cog" {
continue
}
newContents += line + "\n"
}
file.Close()
if err := scanner.Err(); err != nil {
log.Fatal(err)
}

return os.WriteFile(dockerIgnorePath, []byte(newContents), 0o644)
}

func Build(dir, dockerfile, imageName string, secrets []string, noCache bool, progressOutput string, epoch int64) error {
var args []string

sanitiseDockerIgnore(dir)

args = append(args,
"buildx", "build",
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.cog
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
python_version: "3.8"
predict: "predict.py:Predictor"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from cog import BasePredictor


class Predictor(BasePredictor):
def predict(self, s: str) -> str:
return "hello " + s
23 changes: 23 additions & 0 deletions test-integration/test_integration/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,26 @@ def test_precompile(docker_image):
capture_output=True,
)
assert build_process.returncode == 0


def test_dockerignore_dot_cog(docker_image):
project_dir = Path(__file__).parent / "fixtures/dockerignore-cog-project"
# We rewrite the .dockerignore file so preserve its contents across the test.
dockerignore_path = os.path.join(project_dir, ".dockerignore")
dockerignore_contents = ""
with open(dockerignore_path, encoding="utf-8") as handle:
dockerignore_contents = handle.read()
build_process = subprocess.run(
[
"cog",
"build",
"-t",
docker_image,
],
cwd=project_dir,
capture_output=True,
check=False,
)
with open(dockerignore_path, "w", encoding="utf-8") as handle:
handle.write(dockerignore_contents)
assert build_process.returncode == 0
Loading