From f8aa24e903ac93d40cf32867d52cafd283f2d68f Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:04:06 +0200 Subject: [PATCH 01/48] Verify --shell command for many commands, run in parallel --- .github/workflows/test.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 351bb16..0c768ae 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,14 @@ jobs: command: - go - aws + - dotnet + - go + - java + - node + - perl + - php - python + - ruby - rustc - zip runs-on: ubuntu-latest From c429c3841be532075e7a0433ab6c320412008805 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:06:12 +0200 Subject: [PATCH 02/48] Remove duplicate test-case --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0c768ae..f6eed4f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,6 @@ jobs: fail-fast: true matrix: command: - - go - aws - dotnet - go From 9642dffd51cef20a111ad7e991105904f5a82add Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:08:42 +0200 Subject: [PATCH 03/48] Force alpine images for go, python, and lock digest --- .env | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.env b/.env index e5d9c36..333926a 100644 --- a/.env +++ b/.env @@ -16,7 +16,7 @@ DOLT_VERSION=0.37.9 DOTNET_VERSION=6.0 GIT_VERSION=2.32.0 GH_VERSION=2.5.2 -GO_VERSION=1.17.8 +GO_VERSION=1.17.8-alpine@sha256:9e085c25a5928df75e13f9810139b004de862ec95e1987343a37573cee2d1874 HASKELL_VERSION=9.2.2 GHCI_VERSION=${HASKELL_VERSION} HELM_VERSION=3.8.1 @@ -35,10 +35,11 @@ PROTOC_ARCH=${DEFAULT_ARCH} PROTOC_BASE=${DEFAULT_BASE} POSTGRES_VERSION=14.2 PSQL_VERSION=${POSTGRES_VERSION} +PYTHON_VERSION=3.10-alpine@sha256:318c53a8d5c0f56228f7c03a9b5108166cde24131d32ebe6e591ad08d0244ab7 PYTHON_VERSION=3.10 PYTHON2_VERSION=2.7.18 RUBY_VERSION=3.1.1 -RUST_VERSION=1.59.0 +RUST_VERSION=1.59.0-alpine@sha256:65b63b7d003f7a492cc8e550a4830aaa1f4155b74387549a82985c8efb3d0e88 RUSTC_VERSION=${RUST_VERSION} S3CMD_BASE=python:${PYTHON_VERSION}-${DEFAULT_BASE_IMAGE} S3CMD_VERSION=2.2.0 From 026df8f19130227e1becf7b6849f766eecb801b5 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:09:02 +0200 Subject: [PATCH 04/48] Run all cases --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f6eed4f..9fafc11 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,6 @@ on: jobs: ShellTest: strategy: - fail-fast: true matrix: command: - aws From c0e5cfe2f18d791f3016cd42dc5df0828dfc18ff Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:13:31 +0200 Subject: [PATCH 05/48] Use alpine3.14 for python and rust --- .env | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 333926a..5d3b72a 100644 --- a/.env +++ b/.env @@ -35,11 +35,11 @@ PROTOC_ARCH=${DEFAULT_ARCH} PROTOC_BASE=${DEFAULT_BASE} POSTGRES_VERSION=14.2 PSQL_VERSION=${POSTGRES_VERSION} -PYTHON_VERSION=3.10-alpine@sha256:318c53a8d5c0f56228f7c03a9b5108166cde24131d32ebe6e591ad08d0244ab7 +PYTHON_VERSION=3.10-alpine3.14@sha256:9257cf06acd4b9849a9ad1618372b592481898965a46718a529123f3f04540f3 PYTHON_VERSION=3.10 PYTHON2_VERSION=2.7.18 RUBY_VERSION=3.1.1 -RUST_VERSION=1.59.0-alpine@sha256:65b63b7d003f7a492cc8e550a4830aaa1f4155b74387549a82985c8efb3d0e88 +RUST_VERSION=1.59.0-alpine3.14@sha256:86d063b20c2214fe5292683e9386f178462405880fb9507c0e765482eb126789 RUSTC_VERSION=${RUST_VERSION} S3CMD_BASE=python:${PYTHON_VERSION}-${DEFAULT_BASE_IMAGE} S3CMD_VERSION=2.2.0 From 5004a3146366262038e70819771d17bb42f438fc Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:14:59 +0200 Subject: [PATCH 06/48] Disable fail fast --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9fafc11..689a328 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,6 +9,7 @@ on: jobs: ShellTest: strategy: + fail-fast: false matrix: command: - aws From 3e27e9d03db6ddf0defc0553feabc57a476b149d Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:25:57 +0200 Subject: [PATCH 07/48] Fix double python version listing --- .env | 1 - 1 file changed, 1 deletion(-) diff --git a/.env b/.env index 5d3b72a..7f6270a 100644 --- a/.env +++ b/.env @@ -36,7 +36,6 @@ PROTOC_BASE=${DEFAULT_BASE} POSTGRES_VERSION=14.2 PSQL_VERSION=${POSTGRES_VERSION} PYTHON_VERSION=3.10-alpine3.14@sha256:9257cf06acd4b9849a9ad1618372b592481898965a46718a529123f3f04540f3 -PYTHON_VERSION=3.10 PYTHON2_VERSION=2.7.18 RUBY_VERSION=3.1.1 RUST_VERSION=1.59.0-alpine3.14@sha256:86d063b20c2214fe5292683e9386f178462405880fb9507c0e765482eb126789 From f18038370381a832ca8b5f001a5d0b80998abea9 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:27:56 +0200 Subject: [PATCH 08/48] Remove digest from python --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 7f6270a..099607a 100644 --- a/.env +++ b/.env @@ -35,7 +35,7 @@ PROTOC_ARCH=${DEFAULT_ARCH} PROTOC_BASE=${DEFAULT_BASE} POSTGRES_VERSION=14.2 PSQL_VERSION=${POSTGRES_VERSION} -PYTHON_VERSION=3.10-alpine3.14@sha256:9257cf06acd4b9849a9ad1618372b592481898965a46718a529123f3f04540f3 +PYTHON_VERSION=3.10-alpine PYTHON2_VERSION=2.7.18 RUBY_VERSION=3.1.1 RUST_VERSION=1.59.0-alpine3.14@sha256:86d063b20c2214fe5292683e9386f178462405880fb9507c0e765482eb126789 From 9a357658aaf7827dac3b164e15e7e70c297e7f26 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:34:52 +0200 Subject: [PATCH 09/48] Lock dotnet --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 099607a..3b35823 100644 --- a/.env +++ b/.env @@ -13,7 +13,7 @@ DEFAULT_BASE_IMAGE_VERSION=${ALPINE_VERSION} DEFAULT_BASE=${DEFAULT_BASE_IMAGE}:${DEFAULT_BASE_IMAGE_VERSION} DOCTL_VERSION=1.70.0 DOLT_VERSION=0.37.9 -DOTNET_VERSION=6.0 +DOTNET_VERSION=6.0@sha256:a2a8f968b043349b8faa0625c5405ac33da70b3274ff9e17109430f16aa9a3ee GIT_VERSION=2.32.0 GH_VERSION=2.5.2 GO_VERSION=1.17.8-alpine@sha256:9e085c25a5928df75e13f9810139b004de862ec95e1987343a37573cee2d1874 From dc04c04b74404c51fc1cb1a3dfaf37b5386ae352 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 12 Apr 2022 00:38:39 +0200 Subject: [PATCH 10/48] Lock dotnet --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index a5da840..6b85bcd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -77,7 +77,7 @@ services: - "${HOME:-home}/.dockerized/apps/dolt:/root" entrypoint: [ "dolt" ] dotnet: - image: "mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}-alpine" + image: "mcr.microsoft.com/dotnet/sdk:${DOTNET_VERSION}" entrypoint: [ "dotnet" ] gh: image: "gh:${GH_VERSION}" From 4ce2e250b440f6537ff97970b8081d45bb56ffac Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 16:45:01 +0200 Subject: [PATCH 11/48] Lock go to older version --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 3b35823..a8e47d8 100644 --- a/.env +++ b/.env @@ -16,7 +16,7 @@ DOLT_VERSION=0.37.9 DOTNET_VERSION=6.0@sha256:a2a8f968b043349b8faa0625c5405ac33da70b3274ff9e17109430f16aa9a3ee GIT_VERSION=2.32.0 GH_VERSION=2.5.2 -GO_VERSION=1.17.8-alpine@sha256:9e085c25a5928df75e13f9810139b004de862ec95e1987343a37573cee2d1874 +GO_VERSION=1.17.8-alpine@sha256:c7c94588b6445f5254fbc34df941afa10de04706deb330e62831740c9f0f2030 HASKELL_VERSION=9.2.2 GHCI_VERSION=${HASKELL_VERSION} HELM_VERSION=3.8.1 From be87829cfe88558201c917c28c48239b2a0bec56 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 16:55:16 +0200 Subject: [PATCH 12/48] feat: add --digest and --pull options --- main.go | 32 ++++++++++++++++++---- main_test.go | 17 ++++++++++++ pkg/dockerized.go | 70 +++++++++++++++++++++++++++++++++++++++++------ pkg/help/help.go | 4 +-- 4 files changed, 108 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 50d2562..6703e89 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,9 @@ func RunCli(args []string) (err error, exitCode int) { var optionVerbose = hasKey(dockerizedOptions, "--verbose") || hasKey(dockerizedOptions, "-v") var optionShell = hasKey(dockerizedOptions, "--shell") var optionBuild = hasKey(dockerizedOptions, "--build") + var optionPull = hasKey(dockerizedOptions, "--pull") var optionVersion = hasKey(dockerizedOptions, "--version") + var optionDigest = hasKey(dockerizedOptions, "--digest") var optionPort = hasKey(dockerizedOptions, "-p") dockerizedRoot := dockerized.GetDockerizedRoot() @@ -61,7 +63,7 @@ func RunCli(args []string) (err error, exitCode int) { } if commandName == "" || optionHelp { - err := help.Help(composeFilePaths) + err := help.Help() if err != nil { return err, 1 } @@ -74,7 +76,7 @@ func RunCli(args []string) (err error, exitCode int) { if commandVersion != "" { if commandVersion == "?" { - err = dockerized.PrintCommandVersions(composeFilePaths, commandName, optionVerbose) + err = dockerized.PrintCommandVersions(commandName, optionVerbose) if err != nil { return err, 1 } else { @@ -85,7 +87,7 @@ func RunCli(args []string) (err error, exitCode int) { } } - project, err := dockerized.GetProject(composeFilePaths) + project, err := dockerized.GetProject() if err != nil { return err, 1 } @@ -142,13 +144,31 @@ func RunCli(args []string) (err error, exitCode int) { if optionVerbose { fmt.Printf("Building container image for %s...\n", commandName) } - err := dockerized.DockerComposeBuild(composeFilePaths, api.BuildOptions{ + err := dockerized.DockerComposeBuild(api.BuildOptions{ Services: []string{commandName}, }) if err != nil { return err, 1 } + } else if optionPull { + err = dockerized.Pull(commandName) + if err != nil { + return err, 1 + } + if !optionDigest { + return nil, 0 + } + } + + if optionDigest { + digest, err := dockerized.GetDigest(commandName) + + if err != nil { + return err, 0 + } + fmt.Println(digest) + return nil, 0 } if optionShell { @@ -220,11 +240,13 @@ func RunCli(args []string) (err error, exitCode int) { func parseArguments(args []string) (map[string]string, string, string, []string) { var options = []string{ - "--shell", "--build", "-h", "--help", "-p", + "--pull", + "--digest", + "--shell", "-v", "--verbose", "--version", diff --git a/main_test.go b/main_test.go index ebd6c34..6539012 100644 --- a/main_test.go +++ b/main_test.go @@ -23,6 +23,7 @@ type Context struct { } func TestHelp(t *testing.T) { + defer context().Restore() output := testDockerized(t, []string{"--help"}) assert.Contains(t, output, "Usage:") } @@ -245,6 +246,22 @@ func TestOverrideVersionWithGlobalEnvFile(t *testing.T) { assert.Contains(t, output, "3.8.0") } +func TestEnvironmentHostName(t *testing.T) { + defer context().Restore() + expectedHostName, err := os.Hostname() + assert.Nil(t, err) + assert.NotNil(t, expectedHostName) + var output = testDockerized(t, []string{"--shell", "go", "-c", "echo $HOST_HOSTNAME"}) + assert.Contains(t, output, expectedHostName) +} + +func TestShell(t *testing.T) { + defer context().Restore() + var output = testDockerized(t, []string{"--shell", "go", "--help"}) + assert.Contains(t, output, "Welcome to dockerized shell.") + assert.Contains(t, output, "BusyBox v1.33.1") +} + func capture(callback func()) string { read, write, _ := os.Pipe() os.Stdout = write diff --git a/pkg/dockerized.go b/pkg/dockerized.go index d93f240..0db0ad9 100644 --- a/pkg/dockerized.go +++ b/pkg/dockerized.go @@ -4,6 +4,7 @@ import ( "encoding/json" "github.com/compose-spec/compose-go/dotenv" "github.com/datastack-net/dockerized/pkg/util" + "github.com/docker/docker/client" "github.com/hashicorp/go-version" "io" "net/http" @@ -30,7 +31,7 @@ import ( "syscall" ) -// Determine which docker-compose file to use. Assumes .env files are already loaded. +// GetComposeFilePaths Determine which docker-compose file to use. Assumes .env files are already loaded. func GetComposeFilePaths(dockerizedRoot string) []string { var composeFilePaths []string composeFilePath := os.Getenv("COMPOSE_FILE") @@ -82,8 +83,8 @@ func getNpmPackageVersions(packageName string) ([]string, error) { return versionKeys, nil } -func PrintCommandVersions(composeFilePaths []string, commandName string, verbose bool) error { - project, err := GetProject(composeFilePaths) +func PrintCommandVersions(commandName string, verbose bool) error { + project, err := GetProject() if err != nil { return err } @@ -269,7 +270,7 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error { var envFiles []string // Default - defaultEnvFile := GetDockerizedRoot() + "/.env" + defaultEnvFile := filepath.Join(GetDockerizedRoot(), ".env") envFiles = append(envFiles, defaultEnvFile) // Global overrides @@ -282,7 +283,10 @@ func LoadEnvFiles(hostCwd string, optionVerbose bool) error { // Project overrides if projectEnvFile, err := findProjectEnvFile(hostCwd); err == nil { envFiles = append(envFiles, projectEnvFile) - os.Setenv("DOCKERIZED_PROJECT_ROOT", filepath.Dir(projectEnvFile)) + err := os.Setenv("DOCKERIZED_PROJECT_ROOT", filepath.Dir(projectEnvFile)) + if err != nil { + return err + } } envFiles = unique(envFiles) @@ -429,7 +433,7 @@ func getRawProject(composeFilePaths []string) (*types.Project, error) { return cli.ProjectFromOptions(options) } -func GetProject(composeFilePaths []string) (*types.Project, error) { +func GetProject() (*types.Project, error) { options, err := cli.NewProjectOptions([]string{}, cli.WithOsEnv, cli.WithConfigFileEnv, @@ -461,6 +465,56 @@ func dockerComposeUpNetworkOnly(backend *api.ServiceProxy, ctx context.Context, return err } +func GetDigest(serviceName string) (string, error) { + project, err := GetProject() + if err != nil { + return "", err + } + + service, err := project.GetService(serviceName) + if err != nil { + return "", err + } + + dockerCli, err := getDockerCli() + if err != nil { + return "", err + } + + ctx, _ := newSigContext() + data, _, err := dockerCli.Client().ImageInspectWithRaw(ctx, service.Image) + + if err != nil { + if client.IsErrNotFound(err) { + return "", fmt.Errorf("%w\nTry --pull", err) + } + return "", err + } + + return data.RepoDigests[0], nil +} + +func Pull(serviceName string) error { + project, err := GetProject() + + service, err := project.GetService(serviceName) + if err != nil { + return err + } + project.Services = []types.ServiceConfig{service} + + backend, err := getBackend() + if err != nil { + return err + } + + ctx, _ := newSigContext() + return backend.Pull(ctx, project, api.PullOptions{ + Quiet: false, + IgnoreFailures: false, + }) +} + func getDockerCli() (*command.DockerCli, error) { dockerCli, err := command.NewDockerCli() if err != nil { @@ -486,8 +540,8 @@ func getBackend() (*api.ServiceProxy, error) { return backend, nil } -func DockerComposeBuild(composeFilePaths []string, buildOptions api.BuildOptions) error { - project, err := GetProject(composeFilePaths) +func DockerComposeBuild(buildOptions api.BuildOptions) error { + project, err := GetProject() if err != nil { return err } diff --git a/pkg/help/help.go b/pkg/help/help.go index a1713e5..7baaf04 100644 --- a/pkg/help/help.go +++ b/pkg/help/help.go @@ -6,8 +6,8 @@ import ( "sort" ) -func Help(composeFilePaths []string) error { - project, err := dockerized.GetProject(composeFilePaths) +func Help() error { + project, err := dockerized.GetProject() if err != nil { return err } From 4f40dba69fed010c1e8ba185684a47d519f055b3 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 16:56:25 +0200 Subject: [PATCH 13/48] Lock ruby --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index a8e47d8..9193a1d 100644 --- a/.env +++ b/.env @@ -37,7 +37,7 @@ POSTGRES_VERSION=14.2 PSQL_VERSION=${POSTGRES_VERSION} PYTHON_VERSION=3.10-alpine PYTHON2_VERSION=2.7.18 -RUBY_VERSION=3.1.1 +RUBY_VERSION=3.1.1-alpine3.14@sha256:e5a66799d2e82e1434ae4565842bcfd03bc0c2fb4394811ebedcae967fc77919 RUST_VERSION=1.59.0-alpine3.14@sha256:86d063b20c2214fe5292683e9386f178462405880fb9507c0e765482eb126789 RUSTC_VERSION=${RUST_VERSION} S3CMD_BASE=python:${PYTHON_VERSION}-${DEFAULT_BASE_IMAGE} From 2016f146a80b09632d83461f1a0dc3f86c4320a5 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 16:57:14 +0200 Subject: [PATCH 14/48] Pull image before running shell test --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 689a328..75443de 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,6 +33,7 @@ jobs: env: COMMAND: "${{matrix.command}}" run: | + bin/dockerized --pull $COMMAND bin/dockerized --shell $COMMAND -c 'echo $HOST_HOSTNAME' | tee ~/shell.log grep $(hostname) ~/shell.log IntegrationTest: From 17e093d4f52bf8e03687eed2ffb8718cae968f96 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:19:04 +0200 Subject: [PATCH 15/48] feat: add option --entrypoint --- main.go | 15 +++++++++++++++ pkg/help/help.go | 2 ++ 2 files changed, 17 insertions(+) diff --git a/main.go b/main.go index 6703e89..cca91e1 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ func RunCli(args []string) (err error, exitCode int) { var optionVersion = hasKey(dockerizedOptions, "--version") var optionDigest = hasKey(dockerizedOptions, "--digest") var optionPort = hasKey(dockerizedOptions, "-p") + var optionEntrypoint = hasKey(dockerizedOptions, "--entrypoint") dockerizedRoot := dockerized.GetDockerizedRoot() dockerized.NormalizeEnvironment(dockerizedRoot) @@ -171,6 +172,10 @@ func RunCli(args []string) (err error, exitCode int) { return nil, 0 } + if optionShell && optionEntrypoint { + return fmt.Errorf("--shell and --entrypoint are mutually exclusive"), 1 + } + if optionShell { if optionVerbose { fmt.Printf("Opening shell in container for %s...\n", commandName) @@ -225,6 +230,14 @@ func RunCli(args []string) (err error, exitCode int) { } } + if optionEntrypoint { + var entrypoint = dockerizedOptions["--entrypoint"] + if optionVerbose { + fmt.Printf("Setting entrypoint to %s\n", entrypoint) + } + runOptions.Entrypoint = strings.Split(entrypoint, " ") + } + if !contains(project.ServiceNames(), commandName) { image := "r.j3ss.co/" + commandName if optionVerbose { @@ -247,6 +260,7 @@ func parseArguments(args []string) (map[string]string, string, string, []string) "--pull", "--digest", "--shell", + "--entrypoint", "-v", "--verbose", "--version", @@ -254,6 +268,7 @@ func parseArguments(args []string) (map[string]string, string, string, []string) var optionsWithParameters = []string{ "-p", + "--entrypoint", } commandName := "" diff --git a/pkg/help/help.go b/pkg/help/help.go index 7baaf04..22c1a8f 100644 --- a/pkg/help/help.go +++ b/pkg/help/help.go @@ -32,6 +32,8 @@ func Help() error { fmt.Println("Options:") fmt.Println(" --build Rebuild the container before running it.") fmt.Println(" --shell Start a shell inside the command container. Similar to `docker run --entrypoint=sh`.") + fmt.Println(" --entrypoint ") + fmt.Println(" Override the default entrypoint of the command container.") fmt.Println(" -p Exposes given port to host, e.g. -p 8080") fmt.Println(" -p : Maps host port to container port, e.g. -p 80:8080") fmt.Println(" -v, --verbose Log what dockerized is doing.") From adca8cbb0d3e7e9a7138d01696ae79b062af8d13 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:19:24 +0200 Subject: [PATCH 16/48] verbose logging while running --shell test --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75443de..dabd75f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: COMMAND: "${{matrix.command}}" run: | bin/dockerized --pull $COMMAND - bin/dockerized --shell $COMMAND -c 'echo $HOST_HOSTNAME' | tee ~/shell.log + bin/dockerized -v --shell $COMMAND -c 'echo $HOST_HOSTNAME' | tee ~/shell.log grep $(hostname) ~/shell.log IntegrationTest: runs-on: ubuntu-latest From f002d73eb223663eeef7d8dc9e65ceb261288acc Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:21:20 +0200 Subject: [PATCH 17/48] Split up test in multiple steps to verify which command breaks --- .github/workflows/test.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dabd75f..f54ac49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,13 +29,12 @@ jobs: uses: actions/checkout@v2 - name: Compile run: bin/dockerized --compile - - name: "Test: dockerized --shell ${{matrix.command}}" - env: - COMMAND: "${{matrix.command}}" - run: | - bin/dockerized --pull $COMMAND - bin/dockerized -v --shell $COMMAND -c 'echo $HOST_HOSTNAME' | tee ~/shell.log - grep $(hostname) ~/shell.log + - name: "dockerized --pull ${{matrix.command}}" + run: bin/dockerized --pull ${{matrix.command}} + - name: "dockerized --shell ${{matrix.command}}" + run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/shell.log + - name: "verify" + run: grep $(hostname) ~/shell.log IntegrationTest: runs-on: ubuntu-latest steps: From c6115f49b7748b5a03cff6941707b414828ff3f3 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:26:02 +0200 Subject: [PATCH 18/48] Try entrypoint approach --- .github/workflows/test.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f54ac49..01ec7f3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,8 +33,12 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/shell.log - - name: "verify" - run: grep $(hostname) ~/shell.log + - name: "dockerized --shell ${{matrix.command}}" + run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log + - name: "Assert" + run: | + grep $(hostname) ~/shell.log && echo "Shell test succeeded" + grep $(hostname) ~/sh.log && echo "Entrypoint test succeeded" IntegrationTest: runs-on: ubuntu-latest steps: From df08fe4e9413d56e8bed900faa83b08d120e4f41 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:28:40 +0200 Subject: [PATCH 19/48] Fix test step name --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 01ec7f3..83a8d1f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,7 +33,7 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/shell.log - - name: "dockerized --shell ${{matrix.command}}" + - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - name: "Assert" run: | From 8d313d1cfe2b521daa4ba375a3ab56dd725ea955 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 17:48:21 +0200 Subject: [PATCH 20/48] Sanity check --- .github/workflows/test.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 83a8d1f..04ae448 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,9 +36,9 @@ jobs: - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - name: "Assert" - run: | - grep $(hostname) ~/shell.log && echo "Shell test succeeded" - grep $(hostname) ~/sh.log && echo "Entrypoint test succeeded" + run: | + grep $(hostname) ~/shell.log + grep $(hostname) ~/sh.log IntegrationTest: runs-on: ubuntu-latest steps: From 0cddeaadcfd45f77f07d386ad665785d158d2877 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:12:37 +0200 Subject: [PATCH 21/48] Test printing env instead of echoing hostname --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04ae448..517497e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: - name: "dockerized --pull ${{matrix.command}}" run: bin/dockerized --pull ${{matrix.command}} - name: "dockerized --shell ${{matrix.command}}" - run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/shell.log + run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - name: "Assert" From ff52840f95f2b1060db7e922cd9c363da54507c4 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:23:52 +0200 Subject: [PATCH 22/48] Test manual shell detection --- .github/workflows/test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 517497e..0a05a57 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,10 +35,16 @@ jobs: run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log + - name: "dockerized --entrypoint sh ${{matrix.command}} (detect shell)" + run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c '$(command -v bash || command -v zsh || command -v sh || which bash || which zsh || which sh) -c env' | tee ~/detect.log - name: "Assert" run: | + echo "Test --shell" grep $(hostname) ~/shell.log + echo "Test --entrypoint sh" grep $(hostname) ~/sh.log + echo "Test --entrypoint sh (detect shell)" + grep $(hostname) ~/detect.log IntegrationTest: runs-on: ubuntu-latest steps: From e8738661dab540d1a10f75c87a6105e29b51f74f Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:35:55 +0200 Subject: [PATCH 23/48] Disable welcome message in --shell --- main.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index cca91e1..ff42960 100644 --- a/main.go +++ b/main.go @@ -178,11 +178,7 @@ func RunCli(args []string) (err error, exitCode int) { if optionShell { if optionVerbose { - fmt.Printf("Opening shell in container for %s...\n", commandName) - - if len(commandArgs) > 0 { - fmt.Printf("Passing arguments to shell: %s\n", commandArgs) - } + fmt.Printf("Setting up shell in container for %s...\n", commandName) } var ps1 = fmt.Sprintf( @@ -220,14 +216,21 @@ func RunCli(args []string) (err error, exitCode int) { var cmdPrintWelcome = fmt.Sprintf("echo '%s'", color.YellowString(welcomeMessage)) var cmdLaunchShell = fmt.Sprintf("$(%s)", strings.Join(shellDetectionCommands, " || ")) - runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) - runOptions.Entrypoint = []string{"/bin/sh"} + var shellArgs []string + var showWelcome = false + if showWelcome { + shellArgs = append(shellArgs, cmdPrintWelcome) + } if len(commandArgs) > 0 { - runOptions.Command = []string{"-c", fmt.Sprintf("%s; %s \"%s\"", cmdPrintWelcome, cmdLaunchShell, strings.Join(commandArgs, "\" \""))} + shellArgs = append(shellArgs, fmt.Sprintf("%s \"%s\"", cmdLaunchShell, strings.Join(commandArgs, "\" \""))) } else { - runOptions.Command = []string{"-c", fmt.Sprintf("%s; %s", cmdPrintWelcome, cmdLaunchShell)} + shellArgs = append(shellArgs, cmdLaunchShell) } + + runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) + runOptions.Entrypoint = []string{"/bin/sh"} + runOptions.Command = []string{"-c", strings.Join(shellArgs, "; ")} } if optionEntrypoint { @@ -238,6 +241,11 @@ func RunCli(args []string) (err error, exitCode int) { runOptions.Entrypoint = strings.Split(entrypoint, " ") } + if optionVerbose { + fmt.Printf("Entrypoint: %s\n", runOptions.Entrypoint) + fmt.Printf("Command: %s\n", runOptions.Command) + } + if !contains(project.ServiceNames(), commandName) { image := "r.j3ss.co/" + commandName if optionVerbose { From ee6d65e9cb6710b70fdd3c288c2e798a2aaaefae Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:44:17 +0200 Subject: [PATCH 24/48] Disable PS1 override in --shell option --- main.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index ff42960..1bd77b5 100644 --- a/main.go +++ b/main.go @@ -181,11 +181,11 @@ func RunCli(args []string) (err error, exitCode int) { fmt.Printf("Setting up shell in container for %s...\n", commandName) } - var ps1 = fmt.Sprintf( - "%s %s:\\w \\$ ", - color.BlueString("dockerized %s", commandName), - color.New(color.FgHiBlue).Add(color.Bold).Sprintf("\\u@\\h"), - ) + //var ps1 = fmt.Sprintf( + // "%s %s:\\w \\$ ", + // color.BlueString("dockerized %s", commandName), + // color.New(color.FgHiBlue).Add(color.Bold).Sprintf("\\u@\\h"), + //) var welcomeMessage = "Welcome to dockerized shell. Type 'exit' or press Ctrl+D to exit.\n" welcomeMessage += "Mounted volumes:\n" @@ -228,7 +228,7 @@ func RunCli(args []string) (err error, exitCode int) { shellArgs = append(shellArgs, cmdLaunchShell) } - runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) + //runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) runOptions.Entrypoint = []string{"/bin/sh"} runOptions.Command = []string{"-c", strings.Join(shellArgs, "; ")} } From a3b1bd1f20b14e317505ba744cfeabc8798671b8 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:45:01 +0200 Subject: [PATCH 25/48] See if quotes make the difference --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0a05a57..d648d53 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - name: "dockerized --entrypoint sh ${{matrix.command}} (detect shell)" - run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c '$(command -v bash || command -v zsh || command -v sh || which bash || which zsh || which sh) -c env' | tee ~/detect.log + run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c '$(command -v bash || command -v zsh || command -v sh || which bash || which zsh || which sh) "-c" "env"' | tee ~/detect.log - name: "Assert" run: | echo "Test --shell" From 30a830436507c3a55e6e08855f780001699ed551 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:52:54 +0200 Subject: [PATCH 26/48] Run once (to ensure volumes and network are created) --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d648d53..e8e5a4d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,6 +31,8 @@ jobs: run: bin/dockerized --compile - name: "dockerized --pull ${{matrix.command}}" run: bin/dockerized --pull ${{matrix.command}} + - name: "pre-run" + run: bin/dockerized -v --shell ${{matrix.command}} -c exit 0 - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "dockerized --entrypoint sh ${{matrix.command}}" From 40037536fde9843dd6783e8c286db8b6997722a5 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 18:56:23 +0200 Subject: [PATCH 27/48] Do not use full path for entrypoint --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 1bd77b5..5ad977f 100644 --- a/main.go +++ b/main.go @@ -229,7 +229,7 @@ func RunCli(args []string) (err error, exitCode int) { } //runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) - runOptions.Entrypoint = []string{"/bin/sh"} + runOptions.Entrypoint = []string{"sh"} runOptions.Command = []string{"-c", strings.Join(shellArgs, "; ")} } From e9962e0d09a4e64ed1b5b4f87aed68613992461f Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Tue, 19 Apr 2022 21:49:53 +0200 Subject: [PATCH 28/48] Determine if test order matters --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e8e5a4d..af95a88 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,12 +33,12 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "pre-run" run: bin/dockerized -v --shell ${{matrix.command}} -c exit 0 - - name: "dockerized --shell ${{matrix.command}}" - run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "dockerized --entrypoint sh ${{matrix.command}}" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - name: "dockerized --entrypoint sh ${{matrix.command}} (detect shell)" run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c '$(command -v bash || command -v zsh || command -v sh || which bash || which zsh || which sh) "-c" "env"' | tee ~/detect.log + - name: "dockerized --shell ${{matrix.command}}" + run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" run: | echo "Test --shell" From 30baa6772bd1d8d26897caa082a8e84715d03b8a Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 09:56:41 +0200 Subject: [PATCH 29/48] Explicit shells per command --- docker-compose.yml | 2 ++ main.go | 20 ++++++-------------- pkg/labels/labels.go | 3 +++ 3 files changed, 11 insertions(+), 14 deletions(-) create mode 100644 pkg/labels/labels.go diff --git a/docker-compose.yml b/docker-compose.yml index 6b85bcd..f2ee264 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -153,6 +153,8 @@ services: perl: image: perl:${PERL_VERSION} entrypoint: [ "perl" ] + labels: + net.dockerized.shell: "${PERL_SHELL:-/bin/bash}" php: image: "php:${PHP_VERSION}" psql: diff --git a/main.go b/main.go index 5ad977f..7769760 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "github.com/compose-spec/compose-go/types" dockerized "github.com/datastack-net/dockerized/pkg" "github.com/datastack-net/dockerized/pkg/help" + "github.com/datastack-net/dockerized/pkg/labels" util "github.com/datastack-net/dockerized/pkg/util" "github.com/docker/compose/v2/pkg/api" "github.com/fatih/color" @@ -200,21 +201,12 @@ func RunCli(args []string) (err error, exitCode int) { } welcomeMessage = strings.ReplaceAll(welcomeMessage, "\\", "\\\\") - shells := []string{ - "bash", - "zsh", - "sh", - } - var shellDetectionCommands []string - for _, shell := range shells { - shellDetectionCommands = append(shellDetectionCommands, "command -v "+shell) - } - for _, shell := range shells { - shellDetectionCommands = append(shellDetectionCommands, "which "+shell) - } - var cmdPrintWelcome = fmt.Sprintf("echo '%s'", color.YellowString(welcomeMessage)) - var cmdLaunchShell = fmt.Sprintf("$(%s)", strings.Join(shellDetectionCommands, " || ")) + + var cmdLaunchShell = "sh" + if service.Labels[labels.Shell] != "" { + cmdLaunchShell = service.Labels[labels.Shell] + } var shellArgs []string var showWelcome = false diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go new file mode 100644 index 0000000..2cdbe4b --- /dev/null +++ b/pkg/labels/labels.go @@ -0,0 +1,3 @@ +package labels + +const Shell = "net.dockerized.shell" From ed70a037bab89817ddc157f9380e80e720529b31 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 10:16:20 +0200 Subject: [PATCH 30/48] Remove redundant tests --- .github/workflows/test.yml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index af95a88..9ac1015 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,20 +33,12 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "pre-run" run: bin/dockerized -v --shell ${{matrix.command}} -c exit 0 - - name: "dockerized --entrypoint sh ${{matrix.command}}" - run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c 'echo $HOST_HOSTNAME' | tee ~/sh.log - - name: "dockerized --entrypoint sh ${{matrix.command}} (detect shell)" - run: bin/dockerized -v --entrypoint sh ${{matrix.command}} -c '$(command -v bash || command -v zsh || command -v sh || which bash || which zsh || which sh) "-c" "env"' | tee ~/detect.log - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" run: | echo "Test --shell" grep $(hostname) ~/shell.log - echo "Test --entrypoint sh" - grep $(hostname) ~/sh.log - echo "Test --entrypoint sh (detect shell)" - grep $(hostname) ~/detect.log IntegrationTest: runs-on: ubuntu-latest steps: From d0b8b487dfd06fd55d02b09d934fcfcbb54c949e Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 10:17:34 +0200 Subject: [PATCH 31/48] feat: add --commands to list all available commands --- main.go | 9 +++++++++ pkg/help/help.go | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 7769760..0bd976e 100644 --- a/main.go +++ b/main.go @@ -39,6 +39,7 @@ func RunCli(args []string) (err error, exitCode int) { var optionDigest = hasKey(dockerizedOptions, "--digest") var optionPort = hasKey(dockerizedOptions, "-p") var optionEntrypoint = hasKey(dockerizedOptions, "--entrypoint") + var optionCommands = hasKey(dockerizedOptions, "--commands") dockerizedRoot := dockerized.GetDockerizedRoot() dockerized.NormalizeEnvironment(dockerizedRoot) @@ -101,6 +102,13 @@ func RunCli(args []string) (err error, exitCode int) { containerCwd += "/" + hostCwdDirName } + if optionCommands { + for _, service := range project.Services { + fmt.Printf("%s\n", service.Name) + } + return nil, 0 + } + runOptions := api.RunOptions{ Service: commandName, Environment: []string{ @@ -259,6 +267,7 @@ func parseArguments(args []string) (map[string]string, string, string, []string) "-p", "--pull", "--digest", + "--commands", "--shell", "--entrypoint", "-v", diff --git a/pkg/help/help.go b/pkg/help/help.go index 22c1a8f..258c7b0 100644 --- a/pkg/help/help.go +++ b/pkg/help/help.go @@ -31,9 +31,10 @@ func Help() error { fmt.Println("Options:") fmt.Println(" --build Rebuild the container before running it.") - fmt.Println(" --shell Start a shell inside the command container. Similar to `docker run --entrypoint=sh`.") + fmt.Println(" --commands List all available commands.") fmt.Println(" --entrypoint ") fmt.Println(" Override the default entrypoint of the command container.") + fmt.Println(" --shell Start a shell inside the command container. Similar to `docker run --entrypoint=sh`.") fmt.Println(" -p Exposes given port to host, e.g. -p 8080") fmt.Println(" -p : Maps host port to container port, e.g. -p 80:8080") fmt.Println(" -v, --verbose Log what dockerized is doing.") From 6a81b7b8845769b123f299b6a86c96e9f3a99ada Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 10:21:55 +0200 Subject: [PATCH 32/48] Set configured shell as entrypoint for --shell --- main.go | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/main.go b/main.go index 0bd976e..ba8f2d0 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "github.com/datastack-net/dockerized/pkg/labels" util "github.com/datastack-net/dockerized/pkg/util" "github.com/docker/compose/v2/pkg/api" - "github.com/fatih/color" "os" "path/filepath" "strings" @@ -209,28 +208,13 @@ func RunCli(args []string) (err error, exitCode int) { } welcomeMessage = strings.ReplaceAll(welcomeMessage, "\\", "\\\\") - var cmdPrintWelcome = fmt.Sprintf("echo '%s'", color.YellowString(welcomeMessage)) - - var cmdLaunchShell = "sh" + var shell = "sh" if service.Labels[labels.Shell] != "" { - cmdLaunchShell = service.Labels[labels.Shell] - } - - var shellArgs []string - var showWelcome = false - if showWelcome { - shellArgs = append(shellArgs, cmdPrintWelcome) - } - - if len(commandArgs) > 0 { - shellArgs = append(shellArgs, fmt.Sprintf("%s \"%s\"", cmdLaunchShell, strings.Join(commandArgs, "\" \""))) - } else { - shellArgs = append(shellArgs, cmdLaunchShell) + shell = service.Labels[labels.Shell] } - //runOptions.Environment = append(runOptions.Environment, "PS1="+ps1) - runOptions.Entrypoint = []string{"sh"} - runOptions.Command = []string{"-c", strings.Join(shellArgs, "; ")} + runOptions.Entrypoint = []string{shell} + runOptions.Command = commandArgs } if optionEntrypoint { From 61b79f9a8d107449b8e69e293a0160b0f0798893 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 10:28:44 +0200 Subject: [PATCH 33/48] Compare running with docker-compose --- .github/workflows/test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ac1015..b357021 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,6 +33,10 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "pre-run" run: bin/dockerized -v --shell ${{matrix.command}} -c exit 0 + - name: "docker-compose run" + env: + DOCKERIZED_ROOT: "." + run: "docker compose run --entrypoint=//bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" From 896aecb86f95c692f887ac815460c13cb0d9a616 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 10:29:31 +0200 Subject: [PATCH 34/48] Pass args in quotes --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b357021..c2a108c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -32,7 +32,7 @@ jobs: - name: "dockerized --pull ${{matrix.command}}" run: bin/dockerized --pull ${{matrix.command}} - name: "pre-run" - run: bin/dockerized -v --shell ${{matrix.command}} -c exit 0 + run: bin/dockerized -v --shell ${{matrix.command}} -c 'exit 0' - name: "docker-compose run" env: DOCKERIZED_ROOT: "." From 29c2bf3bc8d29cebdf8e72f489cb371b1f0af5f6 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 11:43:43 +0200 Subject: [PATCH 35/48] Use bash for go --- docker-compose.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index f2ee264..ccba012 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -108,6 +108,8 @@ services: environment: GOOS: "${GOOS:-}" GOARCH: "${GOARCH:-}" + labels: + net.dockerized.shell: "/bin/bash" gofmt: <<: *go entrypoint: [ "gofmt" ] From 25d1e8cf04ee69b374ab64afe1d6db12f9264500 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 11:43:49 +0200 Subject: [PATCH 36/48] Fix test --- main_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 6539012..3ef95b3 100644 --- a/main_test.go +++ b/main_test.go @@ -257,9 +257,8 @@ func TestEnvironmentHostName(t *testing.T) { func TestShell(t *testing.T) { defer context().Restore() - var output = testDockerized(t, []string{"--shell", "go", "--help"}) - assert.Contains(t, output, "Welcome to dockerized shell.") - assert.Contains(t, output, "BusyBox v1.33.1") + var output = testDockerized(t, []string{"--shell", "go", "--version"}) + assert.Contains(t, output, "GNU bash") } func capture(callback func()) string { From 4f1d2f9d361e5a49662fbc448df2929a8a4753f8 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 11:44:21 +0200 Subject: [PATCH 37/48] Ensure networks are created, similar to docker-compose run --- .github/workflows/test.yml | 2 +- pkg/dockerized.go | 24 +++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c2a108c..de22b65 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,7 +36,7 @@ jobs: - name: "docker-compose run" env: DOCKERIZED_ROOT: "." - run: "docker compose run --entrypoint=//bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" + run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" diff --git a/pkg/dockerized.go b/pkg/dockerized.go index 0db0ad9..d0ea3e7 100644 --- a/pkg/dockerized.go +++ b/pkg/dockerized.go @@ -446,23 +446,9 @@ func GetProject() (*types.Project, error) { return cli.ProjectFromOptions(options) } -func dockerComposeUpNetworkOnly(backend *api.ServiceProxy, ctx context.Context, project *types.Project) error { +func dockerComposeUpPrepare(backend *api.ServiceProxy, ctx context.Context, project types.Project) error { project.Services = []types.ServiceConfig{} - upOptions := api.UpOptions{ - Create: api.CreateOptions{ - Services: []string{}, - RemoveOrphans: true, - Recreate: "always", - }, - } - err := backend.Up(ctx, project, upOptions) - - // docker compose up will return error if there is no service to start, but the network will have been created. - expectedErrorMessage := "no container found for project \"" + project.Name + "\": not found" - if err == nil || api.IsNotFoundError(err) && err.Error() == expectedErrorMessage { - return nil - } - return err + return backend.Create(ctx, &project, api.CreateOptions{}) } func GetDigest(serviceName string) (string, error) { @@ -590,7 +576,7 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes return err, 1 } - err = dockerComposeUpNetworkOnly(backend, ctx, project) + err = dockerComposeUpPrepare(backend, ctx, *project) if err != nil { return err, 1 } @@ -599,6 +585,10 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes exitCode, err := backend.RunOneOffContainer(ctx, project, runOptions) if err != nil { + if exitCode == 0 { + exitCode = 1 + } + return err, exitCode } if exitCode != 0 { From bc77d204f7ea7bf5cc8b59b17b218269e259789d Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 11:52:11 +0200 Subject: [PATCH 38/48] Disable compose prerun --- .github/workflows/test.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index de22b65..0227060 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,10 +33,10 @@ jobs: run: bin/dockerized --pull ${{matrix.command}} - name: "pre-run" run: bin/dockerized -v --shell ${{matrix.command}} -c 'exit 0' - - name: "docker-compose run" - env: - DOCKERIZED_ROOT: "." - run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" +# - name: "docker-compose run" +# env: +# DOCKERIZED_ROOT: "." +# run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" From 9b250a171abc48ce8799759755885df46f5dbad0 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 11:52:44 +0200 Subject: [PATCH 39/48] Disable prerun --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0227060..d3913ae 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -31,8 +31,8 @@ jobs: run: bin/dockerized --compile - name: "dockerized --pull ${{matrix.command}}" run: bin/dockerized --pull ${{matrix.command}} - - name: "pre-run" - run: bin/dockerized -v --shell ${{matrix.command}} -c 'exit 0' +# - name: "pre-run" +# run: bin/dockerized -v --shell ${{matrix.command}} -c 'exit 0' # - name: "docker-compose run" # env: # DOCKERIZED_ROOT: "." From c94acbcdc776a60397fde775b2d72da8be34a038 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 12:37:37 +0200 Subject: [PATCH 40/48] Add more logging --- main.go | 2 +- pkg/dockerized.go | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index ba8f2d0..9660bb0 100644 --- a/main.go +++ b/main.go @@ -240,7 +240,7 @@ func RunCli(args []string) (err error, exitCode int) { return dockerized.DockerRun(image, runOptions, volumes) } - return dockerized.DockerComposeRun(project, runOptions, volumes, serviceOptions...) + return dockerized.DockerComposeRun(project, runOptions, volumes, optionVerbose, serviceOptions...) } func parseArguments(args []string) (map[string]string, string, string, []string) { diff --git a/pkg/dockerized.go b/pkg/dockerized.go index d0ea3e7..d410f53 100644 --- a/pkg/dockerized.go +++ b/pkg/dockerized.go @@ -358,7 +358,7 @@ func dockerComposeRunAdHocService(service types.ServiceConfig, runOptions api.Ru service, }, WorkingDir: GetDockerizedRoot(), - }, runOptions, []types.ServiceVolumeConfig{}) + }, runOptions, []types.ServiceVolumeConfig{}, false) } func DockerRun(image string, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig) (error, int) { @@ -544,7 +544,7 @@ func DockerComposeBuild(buildOptions api.BuildOptions) error { return backend.Build(ctx, project, buildOptions) } -func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig, serviceOptions ...func(config *types.ServiceConfig) error) (error, int) { +func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes []types.ServiceVolumeConfig, verbose bool, serviceOptions ...func(config *types.ServiceConfig) error) (error, int) { err := os.Chdir(project.WorkingDir) if err != nil { return err, 1 @@ -576,6 +576,9 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes return err, 1 } + if verbose { + fmt.Println("Preparing compose environment...") + } err = dockerComposeUpPrepare(backend, ctx, *project) if err != nil { return err, 1 @@ -583,7 +586,16 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes project.Services = []types.ServiceConfig{service} + if verbose { + fmt.Println("Running one-off container...") + } + exitCode, err := backend.RunOneOffContainer(ctx, project, runOptions) + + if verbose { + fmt.Printf("Container exited with code %d.\n", exitCode) + } + if err != nil { if exitCode == 0 { exitCode = 1 From da23fe3fedb866a52d8cd4ded204f92d33df3349 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:00:42 +0200 Subject: [PATCH 41/48] Run test twice --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d3913ae..1f4becb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,6 +39,8 @@ jobs: # run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log + - name: "dockerized --shell ${{matrix.command}} (second run)" + run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" run: | echo "Test --shell" From 2018a8fed4897f1ba5f96d0884eec26778e8b658 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:09:25 +0200 Subject: [PATCH 42/48] Echo hello world --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f4becb..3e69069 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -38,7 +38,7 @@ jobs: # DOCKERIZED_ROOT: "." # run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" - name: "dockerized --shell ${{matrix.command}}" - run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log + run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo hello world' - name: "dockerized --shell ${{matrix.command}} (second run)" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" From 67d3d8f85459e5022065374e4c5cbfb8fa09b36f Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:22:38 +0200 Subject: [PATCH 43/48] Print error if available --- pkg/dockerized.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/dockerized.go b/pkg/dockerized.go index d410f53..c8ea7c3 100644 --- a/pkg/dockerized.go +++ b/pkg/dockerized.go @@ -594,6 +594,9 @@ func DockerComposeRun(project *types.Project, runOptions api.RunOptions, volumes if verbose { fmt.Printf("Container exited with code %d.\n", exitCode) + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + } } if err != nil { From 3069372f132bb1a1ed77f49302f5fa5d5d8577d2 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:31:10 +0200 Subject: [PATCH 44/48] Temporarily disable tty --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 9660bb0..6c9839e 100644 --- a/main.go +++ b/main.go @@ -115,7 +115,7 @@ func RunCli(args []string) (err error, exitCode int) { }, Command: commandArgs, AutoRemove: true, - Tty: true, + Tty: false, WorkingDir: containerCwd, } From 4bf3d0b8eae62a08447d3aef12086ae9fe7a748b Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:43:06 +0200 Subject: [PATCH 45/48] Disable double run --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3e69069..2b3b21f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,9 +37,9 @@ jobs: # env: # DOCKERIZED_ROOT: "." # run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" +# - name: "dockerized --shell ${{matrix.command}} hello world" +# run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo hello world' - name: "dockerized --shell ${{matrix.command}}" - run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo hello world' - - name: "dockerized --shell ${{matrix.command}} (second run)" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert" run: | From 6d6a36c1740a47352710809938f60bcdd32acb97 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:47:36 +0200 Subject: [PATCH 46/48] Print 'compiled dockerized' --- bin/dockerized | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bin/dockerized b/bin/dockerized index aafa2b8..c0e31d6 100755 --- a/bin/dockerized +++ b/bin/dockerized @@ -87,12 +87,13 @@ if [ "$DOCKERIZED_COMPILE" ] || [ ! -f "$DOCKERIZED_BINARY" ]; then fi if [ $? -ne 0 ]; then - echo "Failed to compile dockerized" >&2 + echo "Failed to compile dockerized." >&2 exit 1 fi + echo "Compiled dockerized." >&2 + if [ $# -eq 0 ]; then - echo "Compiled dockerized" >&2 exit 0 fi fi From 9b6d3c8a2621688065731a4135630ec1622f5208 Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 13:59:05 +0200 Subject: [PATCH 47/48] Only open tty when in terminal --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 6c9839e..948bb22 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/datastack-net/dockerized/pkg/labels" util "github.com/datastack-net/dockerized/pkg/util" "github.com/docker/compose/v2/pkg/api" + "github.com/moby/term" "os" "path/filepath" "strings" @@ -107,7 +108,6 @@ func RunCli(args []string) (err error, exitCode int) { } return nil, 0 } - runOptions := api.RunOptions{ Service: commandName, Environment: []string{ @@ -115,7 +115,7 @@ func RunCli(args []string) (err error, exitCode int) { }, Command: commandArgs, AutoRemove: true, - Tty: false, + Tty: term.IsTerminal(os.Stdout.Fd()), WorkingDir: containerCwd, } From b6ec19d45668ed5d8094d3facbbbec8f53004d3d Mon Sep 17 00:00:00 2001 From: Bouke Versteegh Date: Wed, 20 Apr 2022 14:07:55 +0200 Subject: [PATCH 48/48] Do not pull image separately --- .github/workflows/test.yml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2b3b21f..c6fb68a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,16 +29,6 @@ jobs: uses: actions/checkout@v2 - name: Compile run: bin/dockerized --compile - - name: "dockerized --pull ${{matrix.command}}" - run: bin/dockerized --pull ${{matrix.command}} -# - name: "pre-run" -# run: bin/dockerized -v --shell ${{matrix.command}} -c 'exit 0' -# - name: "docker-compose run" -# env: -# DOCKERIZED_ROOT: "." -# run: "docker compose run --entrypoint=/bin/bash --rm -e HOST_HOSTNAME=$(hostname) ${{matrix.command}} -c env" -# - name: "dockerized --shell ${{matrix.command}} hello world" -# run: bin/dockerized -v --shell ${{matrix.command}} -c 'echo hello world' - name: "dockerized --shell ${{matrix.command}}" run: bin/dockerized -v --shell ${{matrix.command}} -c env | tee ~/shell.log - name: "Assert"