#How to build a Docker image without publishing it to a repository?

1 messages ยท Page 1 of 1 (latest)

stoic moss
#

Let's suppose there's a Dockerfile that looks like this:

FROM alpine:3.17

I can use docker to build a docker image like this:

docker build -t example_image .

Resulting image is available for use inside docker. It is visible in:

docker image list

Now, I want to replicate this inside Dagger. I want to build an image so that it is available locally. I don't want to publish my image to docker repository. I have this code in golang:

package main

import (
    "context"
    "fmt"
    "os"

    "dagger.io/dagger"
)

func main() {
    ctx := context.Background()
    client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
    if err != nil {
        panic(err)
    }
    defer client.Close()

    out, err := client.Container().
        From("alpine:3.17").
        Publish(ctx, "example_image")

    if err != nil {
        panic(err)
    }
    fmt.Println(out)
}

When I run it, I get:

$  go run build.go
#1 resolve image config for docker.io/library/alpine:3.17
#1 DONE 1.7s

#2 docker-image://docker.io/library/alpine:3.17
#2 resolve docker.io/library/alpine:3.17
#2 resolve docker.io/library/alpine:3.17 0.5s done
#2 DONE 0.5s

#3 exporting to image
#3 exporting layers done
#3 exporting manifest sha256:d6950d19877063d96b772fa5fa9355321f44c97a02549ffcf57831d9a73a5da1 done
#3 exporting config sha256:82834596a05078e7e9b7853d4d30edb038fb7f42591beb933b8c6b24cec307aa done
#3 pushing layers
panic: input:1: container.from.publish failed to solve: failed to push example_image: server message: insufficient_scope: authorization failed

Please visit https://dagger.io/help#go for troubleshooting guidance.

goroutine 1 [running]:
main.main()
        /Users/miro/no_cloud_tmp/dagger_test/build.go:25 +0x374
exit status 2

I could not find a solution for this. I looked into Dagger Docs, golang Dagger library, GitHub issues, googled... Does anyone know how to make this work?

open valley
hidden pivot
tribal drum
#

For the explanation, the SDK runs into the engine, which already runs inside docker. That's why, by default, you don't have access to the local image "registry".

stoic moss
#

Thanks everyone! ๐Ÿ˜Š

snow parcel
#

To add on, if you did decide to run a local image registry (for testing or whatever), I found that you actually have to spin up the registry in the same network as the dagger engine if you want to access it within the dagger context. One way you can do this with automation is to use docker command line to list the networks and grep for dagger, then specify that network when you run the docker command to spin up the registry

snow parcel
#

Since I came upon my code, I remembered this thread and thought I would share the docker command I used to do it that greps for dagger:

docker run -d  --restart=always --name registry --network=container:$(docker ps | grep -o "dagger-engine-[a-zA-Z0-9]*") registry
turbid canopy
#

Hey guys, just for the clarity, as OP asked, how to build image without pushing it, I have couple of questions. I can successfully build and push the image, following this example https://docs.dagger.io/544174/multistage-build#example.

  1. If I call Publish it all runs, image builds and then gets pushed to registry. Build also runs when I call for example Export or Entrypoint instead of publishing the image. When I don't call anything on the container, nothing gets build. Does that mean that I only prepared the pipeline/instructions how to build the image?

  2. I simply want to test if the solution I want to containerize can be built without errors. No pushing or loading it to any registry. Is there any method I can call on the *dagger.Container that just builds it without doing anything else? Or is there better way to test this? I wanted to utilize same code to try build and publish only when some flag is set to true.

I'm fetching remote repository with .NET project and building it, so it's different from the example, but the code structure almost the same.

open valley
turbid canopy
#

I don't like it but it's better than Export and does the job