First ensure you have install the cli cdktf, node 20.x and have a docker running.
Then run the following command to create a folder.
mkdir learn-cdktf-docker
cd learn-cdktf-docker
Generate the code using
cdktf init --template=go --providers=kreuzwerker/docker --local
You may need to do "go get github.com/cdktf/cdktf-provider-docker-go/docker/v11" - if the scaffolding give you some error. The terraform website provides some older code which work with older version of provider. I have updated with a newer provider
Noticed that I am using github.com/cdktf/cdktf-provider-docker-go/docker/v11/container instead of "github.com/hashicorp/cdktf-provider-docker-go/docker/v3/container"
You can go and check what is the correct version to use by going to this endpoint.
https://github.com/cdktf/cdktf-provider-docker-go/blob/main/docker/go.mod#L1
Sometimes we get tripped because we think the readme file would provide all we needed.
package main
import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/cdktf/cdktf-provider-docker-go/docker/v11/container"
"github.com/cdktf/cdktf-provider-docker-go/docker/v11/image"
"github.com/hashicorp/terraform-cdk-go/cdktf"
dockerprovider "github.com/cdktf/cdktf-provider-docker-go/docker/v11/provider"
)
func NewMyStack(scope constructs.Construct, id string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &id)
dockerprovider.NewDockerProvider(stack, jsii.String("docker"), &dockerprovider.DockerProviderConfig{})
dockerImage := image.NewImage(stack, jsii.String("nginxImage"), &image.ImageConfig{
Name: jsii.String("nginx:latest"),
KeepLocally: jsii.Bool(false),
})
container.NewContainer(stack, jsii.String("nginxContainer"), &container.ContainerConfig{
Image: dockerImage.Name(),
Name: jsii.String("tutorial"),
Ports: &[]*container.ContainerPorts{{
Internal: jsii.Number(80), External: jsii.Number(8000),
}},
})
return stack
}
func main() {
app := cdktf.NewApp(nil)
NewMyStack(app, "learn-cdktf-docker")
app.Synth()
}
My go.mod file looks like this
module cdk.tf/go/stack
go 1.18
require github.com/aws/constructs-go/constructs/v10 v10.3.0
require github.com/hashicorp/terraform-cdk-go/cdktf v0.20.9
require github.com/aws/jsii-runtime-go v1.102.0
require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/cdktf/cdktf-provider-docker-go/docker/v11 v11.0.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/tools v0.23.0 // indirect
)
Next build your project
go build
And once you have done that run to deploy this image to docker.
cdktf deploy --auto-approve
Then the container deployed:
That's it!
Full code can be found here.
https://github.com/mitzenjeremywoo/my-cdktf-helloworld-fix
Comments