Hi all,
Trying to understand how to create a dependency with this docker provider and by going through other documentation & tutorials, I hit a snag and just want some assistance.
I want to create a dependency tree between the network and the container (I want the network defined first) instead of creating two constructs and hoping that the network construct gets called first. I can define two different constructs working with a main.ts:
import { Construct } from "constructs";
import { config } from "./config";
import { App, TerraformStack } from "cdktf";
import { DockerProvider } from "@cdktf/provider-docker/lib/provider";
import { LocalNetwork } from "./network";
import { LocalDatabase } from "./database";
class localPortalStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new DockerProvider(this, "docker", {});
new LocalNetwork(this, 'internalnetwork');
new LocalDatabase(this, 'dbcontainer');
}
}
const app = new App();
new localPortalStack(app, config.name);
app.synth();
with a network.ts:
import { Construct } from "constructs";
import { config } from './config';
import { Network } from "@cdktf/provider-docker/lib/network";
export class LocalNetwork extends Construct {
constructor(scope: Construct, name: string) {
super(scope, name);
new Network(this, "main", {name: config.networkName});
}
}
and a database container
import { Construct } from "constructs";
import {config} from './config';
import { Container } from "@cdktf/provider-docker/lib/container";
export class LocalDatabase extends Construct {
constructor(scope: Construct, name: string ) {
super(scope, name);
new Container(this, "postgresContainer", {
name: config.postgresContainerName,
image: config.postgresImage,
networksAdvanced: [{name:config.networkName}]
});
}
}
and using a config
const name = 'portalapi-cdktf';
const networkName = 'app-company-network'
const postgresContainerName = 'postgres_cdktf'
const postgresImage = 'postgres:14.2'
export const config = {
name,
networkName,
postgresContainerName,
postgresImage,
};
This works… but it’s two constructs both being ran at the same time. I wanna inherit the network name and put it into the container instead of using a config file on the top level. I wanna use this idea/structure to have containers wait for each other (the idea is that the db container needs to be constructed first before the application container starts)
I’ve dug through a lot of tutorials and references that hashicorp has provided but still haven’t found something that concrete. I don’t feel it’s too difficult to do something like this, I just haven’t worked with node/typescript/cdktf that much but I do know terraform dependencies and I’ve known a docker compose can use a depends_on field and I feel like constructs are able to do so as well.