Trying to return a database service initialized, I'm seeing a very odd behaviour.
I can see 2 services have been spun in the engine and both have been exposed to the host, while only one of them has been initialized.
A reproducible example:
func (m *Tests) ServiceTest(ctx context.Context) (*dagger.Service, error) {
// Define a database service
db := dag.
Container().
From("postgres:14").
WithExposedPort(5432).
WithEnvVariable("POSTGRES_PASSWORD", "postgres").
AsService(dagger.ContainerAsServiceOpts{
UseEntrypoint: true,
})
// Initialize some schema in the database
init, err := dag.Container().From("postgres:14").
WithServiceBinding("db", db).
WithEnvVariable("PGPASSWORD", "postgres").
WithEnvVariable("PG_HOST", "db").
WithExec([]string{"sh", "-c", "psql -h db -U postgres -c 'CREATE TABLE test (id SERIAL PRIMARY KEY, name TEXT)'"}).
WithEnvVariable("CACHE_BUSTER", time.Now().String()).
WithExec([]string{"echo", "Database is ready: %s", time.Now().String()}).
Stdout(ctx)
if err != nil {
return nil, err
}
fmt.Println("Initialized: " + init)
// Return the service
return db, nil
}
Then, dagger call service-test up and trying to hit the db with the new table:
$> psql -h 127.0.0.1 -U postgres -d postgres -c "SELECT * FROM test;"
id | name
----+------
(0 rows)
$> psql -h 127.0.0.1 -U postgres -d postgres -c "SELECT * FROM test;"
ERROR: relation "test" does not exist
LINE 1: SELECT * FROM test;
Seems like 2 different instances of the service have been spun, and I get redirected to one of them each time.
