Skip to content

Commit

Permalink
docs: Update code snippets for services API changes (dagger#9113)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Chadwell <[email protected]>
  • Loading branch information
vikram-dagger and jedevc authored Dec 11, 2024
1 parent 796de0f commit 10a23b5
Show file tree
Hide file tree
Showing 35 changed files with 90 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"main/internal/dagger"
"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -13,9 +13,8 @@ func (m *MyModule) HttpService() *dagger.Service {
From("python").
WithWorkdir("/srv").
WithNewFile("index.html", "Hello, world!").
WithDefaultArgs([]string{"python", "-m", "http.server", "8080"}).
WithExposedPort(8080).
AsService()
AsService(dagger.ContainerAsServiceOpts{Args: []string{"python", "-m", "http.server", "8080"}})
}

// Send a request to an HTTP service and return the response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ def http_service(self) -> dagger.Service:
.from_("python")
.with_workdir("/srv")
.with_new_file("index.html", "Hello, world!")
.with_exec(["python", "-m", "http.server", "8080"])
.with_exposed_port(8080)
.as_service()
.as_service(args=["python", "-m", "http.server", "8080"])
)

@function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class MyModule {
.from("python")
.withWorkdir("/srv")
.withNewFile("index.html", "Hello, world!")
.withExec(["python", "-m", "http.server", "8080"])
.withExposedPort(8080)
.asService()
.asService({ args: ["python", "-m", "http.server", "8080"] })
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"main/internal/dagger"
"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -12,10 +12,8 @@ func (m *MyModule) Services(ctx context.Context) (*dagger.Service, error) {

svcA := dag.Container().From("nginx").
WithExposedPort(80).
WithExec([]string{"sh", "-c", `
nginx & while true; do curl svcb:80 && sleep 1; done
`}).
AsService().WithHostname("svca")
AsService(dagger.ContainerAsServiceOpts{Args: []string{"sh", "-c", `nginx & while true; do curl svcb:80 && sleep 1; done`}}).
WithHostname("svca")

_, err := svcA.Start(ctx)
if err != nil {
Expand All @@ -24,10 +22,8 @@ func (m *MyModule) Services(ctx context.Context) (*dagger.Service, error) {

svcB := dag.Container().From("nginx").
WithExposedPort(80).
WithExec([]string{"sh", "-c", `
nginx & while true; do curl svca:80 && sleep 1; done
`}).
AsService().WithHostname("svcb")
AsService(dagger.ContainerAsServiceOpts{Args: []string{"sh", "-c", `nginx & while true; do curl svca:80 && sleep 1; done`}}).
WithHostname("svcb")

svcB, err = svcB.Start(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ async def services(self) -> dagger.Service:
dag.container()
.from_("nginx")
.with_exposed_port(80)
.with_exec(
["sh", "-c", "nginx & while true; do curl svcb:80 && sleep 1; done"]
.as_service(
args=[
"sh",
"-c",
"nginx & while true; do curl svcb:80 && sleep 1; done",
]
)
.as_service()
.with_hostname("svca")
)

Expand All @@ -24,10 +27,13 @@ async def services(self) -> dagger.Service:
dag.container()
.from_("nginx")
.with_exposed_port(80)
.with_exec(
["sh", "-c", "nginx & while true; do curl svca:80 && sleep 1; done"]
.as_service(
args=[
"sh",
"-c",
"nginx & while true; do curl svca:80 && sleep 1; done",
]
)
.as_service()
.with_hostname("svcb")
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, func, Service } from "@dagger.io/dagger"
import { dag, object, func, Service } from "@dagger.io/dagger"

@object()
class MyModule {
Expand All @@ -9,12 +9,13 @@ class MyModule {
.container()
.from("nginx")
.withExposedPort(80)
.withExec([
"sh",
"-c",
`nginx & while true; do curl svcb:80 && sleep 1; done`,
])
.asService()
.asService({
args: [
"sh",
"-c",
`nginx & while true; do curl svcb:80 && sleep 1; done`,
],
})
.withHostname("svca")

await svcA.start()
Expand All @@ -23,12 +24,13 @@ class MyModule {
.container()
.from("nginx")
.withExposedPort(80)
.withExec([
"sh",
"-c",
`nginx & while true; do curl svca:80 && sleep 1; done`,
])
.asService()
.asService({
args: [
"sh",
"-c",
`nginx & while true; do curl svca:80 && sleep 1; done`,
],
})
.withHostname("svcb")

await svcB.start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import "main/internal/dagger"
import (
"dagger/my-module/internal/dagger"
)

type MyModule struct{}

Expand All @@ -10,7 +12,6 @@ func (m *MyModule) HttpService() *dagger.Service {
From("python").
WithWorkdir("/srv").
WithNewFile("index.html", "Hello, world!").
WithDefaultArgs([]string{"python", "-m", "http.server", "8080"}).
WithExposedPort(8080).
AsService()
AsService(dagger.ContainerAsServiceOpts{Args: []string{"python", "-m", "http.server", "8080"}})
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def http_service(self) -> dagger.Service:
.from_("python")
.with_workdir("/srv")
.with_new_file("index.html", "Hello, world!")
.with_exec(["python", "-m", "http.server", "8080"])
.with_exposed_port(8080)
.as_service()
.as_service(args=["python", "-m", "http.server", "8080"])
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class MyModule {
.from("python")
.withWorkdir("/srv")
.withNewFile("index.html", "Hello, world!")
.withExec(["python", "-m", "http.server", "8080"])
.withExposedPort(8080)
.asService()
.asService({ args: ["python", "-m", "http.server", "8080"] })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"context"
"main/internal/dagger"
"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"

"main/internal/dagger"
"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -15,7 +15,7 @@ func (m *MyModule) Redis(ctx context.Context) *dagger.Container {
WithExposedPort(6379).
WithMountedCache("/data", dag.CacheVolume("my-redis")).
WithWorkdir("/data").
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

redisCLI := dag.Container().
From("redis").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def redis(self) -> dagger.Container:
.with_exposed_port(6379)
.with_mounted_cache("/data", dag.cache_volume("my-redis"))
.with_workdir("/data")
.as_service()
.as_service(use_entrypoint=True)
)

# create Redis client container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyModule {
.withExposedPort(6379)
.withMountedCache("/data", dag.cacheVolume("my-redis"))
.withWorkdir("/data")
.asService()
.asService({ useEntrypoint: true })

const redisCLI = dag
.container()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"

"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -11,7 +13,7 @@ func (m *MyModule) RedisService(ctx context.Context) (string, error) {
redisSrv := dag.Container().
From("redis").
WithExposedPort(6379).
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

// create Redis client container
redisCLI := dag.Container().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class MyModule:
@function
async def redis_service(self) -> str:
"""Creates Redis service and client."""
redis_srv = dag.container().from_("redis").with_exposed_port(6379).as_service()
redis_srv = (
dag.container()
.from_("redis")
.with_exposed_port(6379)
.as_service(use_entrypoint=True)
)

# create Redis client container
redis_cli = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MyModule {
.container()
.from("redis")
.withExposedPort(6379)
.asService()
.asService({ useEntrypoint: true })

// create Redis client container
const redisCLI = dag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"

"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -11,7 +13,7 @@ func (m *MyModule) RedisService(ctx context.Context) (string, error) {
redisSrv := dag.Container().
From("redis").
WithExposedPort(6379).
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

// create Redis client container
redisCLI := dag.Container().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class MyModule:
@function
async def redis_service(self) -> str:
"""Creates Redis service and client."""
redis_srv = dag.container().from_("redis").with_exposed_port(6379).as_service()
redis_srv = (
dag.container()
.from_("redis")
.with_exposed_port(6379)
.as_service(use_entrypoint=True)
)

# create Redis client container
redis_cli = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MyModule {
.container()
.from("redis")
.withExposedPort(6379)
.asService()
.asService({ useEntrypoint: true })

// create Redis client container
const redisCLI = dag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"

"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -11,7 +13,7 @@ func (m *MyModule) RedisService(ctx context.Context) (string, error) {
redisSrv := dag.Container().
From("redis").
WithExposedPort(6379).
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

// create Redis client container
redisCLI := dag.Container().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ class MyModule:
@function
async def redis_service(self) -> str:
"""Creates Redis service and client."""
redis_srv = dag.container().from_("redis").with_exposed_port(6379).as_service()
redis_srv = (
dag.container()
.from_("redis")
.with_exposed_port(6379)
.as_service(use_entrypoint=True)
)

# create Redis client container
redis_cli = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MyModule {
.container()
.from("redis")
.withExposedPort(6379)
.asService()
.asService({ useEntrypoint: true })

// create Redis client container
const redisCLI = dag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"

"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -11,7 +13,7 @@ func (m *MyModule) RedisService(ctx context.Context) (string, error) {
redisSrv := dag.Container().
From("redis").
WithExposedPort(6379).
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

// start Redis ahead of time so it stays up for the duration of the test
redisSrv, err := redisSrv.Start(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"

"dagger/my-module/internal/dagger"
)

type MyModule struct{}
Expand All @@ -15,7 +17,7 @@ func (m *MyModule) Test(ctx context.Context) (string, error) {
WithEnvVariable("MARIADB_DATABASE", "drupal").
WithEnvVariable("MARIADB_ROOT_PASSWORD", "root").
WithExposedPort(3306).
AsService()
AsService(dagger.ContainerAsServiceOpts{UseEntrypoint: true})

// get Drupal base image
// install additional dependencies
Expand Down
Loading

0 comments on commit 10a23b5

Please sign in to comment.