Skip to main content

Install a Go agent in a container

Using the Agent Wizard is the simplest way to deploy a new agent by selecting Language-specific > Go > Install in a container.

Installing the Go agent in a container to AWS Fargate is also supported.

Build and install the agent

The Go agent is installed by replacing go with contrast-go in the command that builds your application. Command line arguments for Go are also available.

  • Under Build image with Agent:

    1. Install contrast-go in your Dockerfile with the following command:

      RUN go run github.com/contrast-security-oss/contrast-go-installer@latest latest
    2. Build the application by replacing go build with contrast-go build in your normal build command to embed the Contrast agent in your application binary.

      RUN contrast-go build ./app
    3. Build the image:

      docker build -t my-app-with-contrast .

      The resulting image contains an already-instrumented binary.

Docker example

The following example shows how to install, build, and run a Go application in a Docker container.

# Contrast supports the two most recent Go releases. See the supported
# technologies page for the current list.
ARG GO_VERSION=1.27

# Step 1: Start from a supported Go version. You can use a different base
# image, but both go and contrast-go must be on your PATH.
FROM golang:${GO_VERSION} AS builder
WORKDIR /build

COPY . .

# Step 2: Install contrast-go. The installer puts it in $GOBIN, or in
# $GOPATH/bin if GOBIN is not set. The official golang images already have
# that directory on the PATH.
RUN go run github.com/contrast-security-oss/contrast-go-installer@latest latest

# Step 3: Build with contrast-go instead of go. This step doesn't replace Go.
# It wraps Go so that it can add instrumentation during the build.
# CGO_ENABLED=0 creates a static binary that can run in the Alpine image below.
# Remove it if your application needs cgo, and use a final image that has the
# same C library as the build image.
RUN CGO_ENABLED=0 contrast-go build -o myapp ./cmd/myapp

# Optional: Move the finished build to a smaller container.
FROM alpine:latest
COPY --from=builder /build/myapp .

# Step 4: Do not put credentials in the image. Pass them as environment
# variables when you run the container.
ENTRYPOINT ["./myapp"]

Note

Using a cloud provider

Setting environment variables typically involves using a secrets manager and then linking the values of those secrets to the environment variables. The Export option in the Contrast agent configuration editor is an easy way create the environment variables for the Contrast credentials.

For example, you could use the following command to build your container:

docker build -t my-app-with-contrast .

Then use the following commands when you run the container:

export CONTRAST__API__TOKEN=<YourConnectionToken>

docker run -p 3000:3000 --name my-app-instance \
-e CONTRAST__API__TOKEN \
my-app-with-contrast

Configure the agent

Configure the Go agent.

A connection token is the recommended method. Set CONTRAST__API__TOKEN in the environment where you run your application.

You can also use other environment variables or the Go YAML template.

Verify the agent deployment

Interact with your application as you normally would. For example, interact with your application's web interface or send API commands.

  1. In the Agent Wizard, select View agent activity to verify that your agent appears in the agents list.

  2. Verify that you see the name of your application in the applications list.

    • In Northstar, select Applications from the left navigation.

    • In Contrast, select Applications from the header.

  3. Verify that you see the name of your local server in the servers list.

    • In Northstar, select Servers from the left navigation.

    • In Contrast, select Servers from the header.