Make sure to pass any file param to build

pull/11164/head
Anders F Björklund 2021-03-10 15:16:10 +01:00
parent 844c6965cc
commit c94a6d7968
7 changed files with 41 additions and 18 deletions

View File

@ -225,7 +225,7 @@ var buildImageCmd = &cobra.Command{
}
img = tmp
}
if err := machine.BuildImage(img, tag, []*config.Profile{profile}); err != nil {
if err := machine.BuildImage(img, dockerFile, tag, []*config.Profile{profile}); err != nil {
exit.Error(reason.GuestImageBuild, "Failed to build image", err)
}
if tmp != "" {
@ -242,6 +242,6 @@ func init() {
loadImageCmd.Flags().BoolVar(&imgRemote, "remote", false, "Cache image from remote registry")
imageCmd.AddCommand(listImageCmd)
buildImageCmd.Flags().StringVarP(&tag, "tag", "t", "", "Tag to apply to the new image (optional)")
buildImageCmd.Flags().StringVarP(&dockerFile, "file", "f", "Dockerfile", "Path to the Dockerfile to use")
buildImageCmd.Flags().StringVarP(&dockerFile, "file", "f", "", "Path to the Dockerfile to use (optional)")
imageCmd.AddCommand(buildImageCmd)
}

View File

@ -279,8 +279,19 @@ func (r *Containerd) RemoveImage(name string) error {
}
// BuildImage builds an image into this runtime
func (r *Containerd) BuildImage(path string, tag string) error {
klog.Infof("Building image: %s", path)
func (r *Containerd) BuildImage(dir string, file string, tag string) error {
if file != "" {
if !path.IsAbs(file) {
file = path.Join(dir, file)
}
// copy to standard path for Dockerfile
df := path.Join(dir, "Dockerfile")
cmd := exec.Command("sudo", "cp", "-f", file, df)
if _, err := r.Runner.RunCmd(cmd); err != nil {
return err
}
}
klog.Infof("Building image: %s", dir)
opt := ""
if tag != "" {
// add default tag if missing
@ -291,8 +302,8 @@ func (r *Containerd) BuildImage(path string, tag string) error {
}
c := exec.Command("sudo", "buildctl", "build",
"--frontend", "dockerfile.v0",
"--local", fmt.Sprintf("context=%s", path),
"--local", fmt.Sprintf("dockerfile=%s", path),
"--local", fmt.Sprintf("context=%s", dir),
"--local", fmt.Sprintf("dockerfile=%s", dir),
"--output", fmt.Sprintf("type=image%s", opt))
c.Stdout = os.Stdout
c.Stderr = os.Stderr

View File

@ -199,13 +199,19 @@ func (r *CRIO) RemoveImage(name string) error {
}
// BuildImage builds an image into this runtime
func (r *CRIO) BuildImage(path string, tag string) error {
klog.Infof("Building image: %s", path)
func (r *CRIO) BuildImage(dir string, file string, tag string) error {
klog.Infof("Building image: %s", dir)
args := []string{"podman", "build"}
if file != "" {
if !path.IsAbs(file) {
file = path.Join(dir, file)
}
args = append(args, "-f", file)
}
if tag != "" {
args = append(args, "-t", tag)
}
args = append(args, path)
args = append(args, dir)
c := exec.Command("sudo", args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr

View File

@ -98,7 +98,7 @@ type Manager interface {
// Pull an image to the runtime from the container registry
PullImage(string) error
// Build an image idempotently into the runtime on a host
BuildImage(string, string) error
BuildImage(string, string, string) error
// ImageExists takes image name and image sha checks if an it exists
ImageExists(string, string) bool

View File

@ -219,13 +219,19 @@ func (r *Docker) RemoveImage(name string) error {
}
// BuildImage builds an image into this runtime
func (r *Docker) BuildImage(path string, tag string) error {
klog.Infof("Building image: %s", path)
func (r *Docker) BuildImage(dir string, file string, tag string) error {
klog.Infof("Building image: %s", dir)
args := []string{"build"}
if file != "" {
if !path.IsAbs(file) {
file = path.Join(dir, file)
}
args = append(args, "-f", file)
}
if tag != "" {
args = append(args, "-t", tag)
}
args = append(args, path)
args = append(args, dir)
c := exec.Command("docker", args...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr

View File

@ -38,7 +38,7 @@ import (
var buildRoot = path.Join(vmpath.GuestPersistentDir, "build")
// BuildImage builds image to all profiles
func BuildImage(path string, tag string, profiles []*config.Profile) error {
func BuildImage(path string, file string, tag string, profiles []*config.Profile) error {
api, err := NewAPIClient()
if err != nil {
return errors.Wrap(err, "api")
@ -80,7 +80,7 @@ func BuildImage(path string, tag string, profiles []*config.Profile) error {
if err != nil {
return err
}
err = transferAndBuildImage(cr, c.KubernetesConfig, path, tag)
err = transferAndBuildImage(cr, c.KubernetesConfig, path, file, tag)
if err != nil {
failed = append(failed, m)
klog.Warningf("Failed to build image for profile %s. make sure the profile is running. %v", pName, err)
@ -97,7 +97,7 @@ func BuildImage(path string, tag string, profiles []*config.Profile) error {
}
// transferAndBuildImage transfers and builds a single image
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, tag string) error {
func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src string, file string, tag string) error {
r, err := cruntime.New(cruntime.Config{Type: k8s.ContainerRuntime, Runner: cr})
if err != nil {
return errors.Wrap(err, "runtime")
@ -135,7 +135,7 @@ func transferAndBuildImage(cr command.Runner, k8s config.KubernetesConfig, src s
return err
}
err = r.BuildImage(context, tag)
err = r.BuildImage(context, file, tag)
if err != nil {
return errors.Wrapf(err, "%s build %s", r.Name(), dst)
}

View File

@ -56,7 +56,7 @@ minikube image build .
### Options
```
-f, --file string Path to the Dockerfile to use (default "Dockerfile")
-f, --file string Path to the Dockerfile to use (optional)
-t, --tag string Tag to apply to the new image (optional)
```