Skip to content

Commit

Permalink
support runtime mount directory (#118)
Browse files Browse the repository at this point in the history
Co-authored-by: Jinjing.Zhou <allenzhou@tensorchord.ai>
  • Loading branch information
VoVAllen and VoVAllen committed May 8, 2022
1 parent aeb846e commit 7d15da4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/midi/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var CommandUp = &cli.Command{
Aliases: []string{"u"},
Usage: "build and run the MIDI environment",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "volume",
Usage: "Mount host directory into container",
Aliases: []string{"v"},
},
&cli.StringFlag{
Name: "tag",
Usage: "Name and optionally a tag in the 'name:tag' format",
Expand Down Expand Up @@ -95,7 +100,7 @@ func up(clicontext *cli.Context) error {
return err
}
containerID, containerIP, err := dockerClient.StartMIDI(
clicontext.Context, tag, "midi", gpu, *ir.DefaultGraph, clicontext.Duration("timeout"))
clicontext.Context, tag, "midi", gpu, *ir.DefaultGraph, clicontext.Duration("timeout"), clicontext.StringSlice("volume"))
if err != nil {
return err
}
Expand Down
37 changes: 28 additions & 9 deletions pkg/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"os"
"strconv"
"strings"
"time"

"github.com/cockroachdb/errors"
Expand All @@ -43,7 +44,7 @@ type Client interface {
Load(ctx context.Context, r io.ReadCloser, quiet bool) error
// Start creates the container for the given tag and container name.
StartMIDI(ctx context.Context, tag, name string,
gpuEnabled bool, g ir.Graph, timeout time.Duration) (string, string, error)
gpuEnabled bool, g ir.Graph, timeout time.Duration, mountOptionsStr []string) (string, string, error)
StartBuildkitd(ctx context.Context, tag, name string) (string, error)
IsRunning(ctx context.Context, name string) (bool, error)
IsCreated(ctx context.Context, name string) (bool, error)
Expand Down Expand Up @@ -158,7 +159,7 @@ func (g generalClient) StartBuildkitd(ctx context.Context,

// Start creates the container for the given tag and container name.
func (c generalClient) StartMIDI(ctx context.Context, tag, name string,
gpuEnabled bool, g ir.Graph, timeout time.Duration) (string, string, error) {
gpuEnabled bool, g ir.Graph, timeout time.Duration, mountOptionsStr []string) (string, string, error) {
logger := logrus.WithFields(logrus.Fields{
"tag": tag,
"container": name,
Expand All @@ -183,19 +184,37 @@ func (c generalClient) StartMIDI(ctx context.Context, tag, name string,
base = fmt.Sprintf("/root/%s", base)
config.WorkingDir = base

mountOption := make([]mount.Mount, len(mountOptionsStr)+1)
for i, option := range mountOptionsStr {
mStr := strings.Split(option, ":")
if len(mStr) != 2 {
return "", "", errors.Wrap(err, fmt.Sprintf("Invalid mount options %s", option))
}

logger.WithFields(logrus.Fields{
"mount-path": mStr[0],
"container-path": mStr[1],
}).Debug("setting up container working directory")
mountOption[i] = mount.Mount{
Type: mount.TypeBind,
Source: mStr[0],
Target: mStr[1],
}
}
mountOption[len(mountOptionsStr)] = mount.Mount{
Type: mount.TypeBind,
Source: path,
Target: base,
}

logger.WithFields(logrus.Fields{
"mount-path": path,
"working-dir": base,
}).Debug("setting up container working directory")

hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{},
Mounts: []mount.Mount{
{
Type: mount.TypeBind,
Source: path,
Target: base,
},
},
Mounts: mountOption,
}
// TODO(gaocegege): Avoid specific logic to set the port.
if g.JupyterConfig != nil {
Expand Down

0 comments on commit 7d15da4

Please sign in to comment.