コンテナにGoエージェントをインストール
Goエージェントのコンテナへのインストールは、基本的に標準のインストール手順と同じですが、インストールがコンテナ内で行われる点と、ベストプラクティスに従い環境変数を使用してContrastの認証情報を設定する必要がある点が異なります。
Goエージェントをコンテナにインストールする場合、環境変数を使用するのが最も安全な方法です。コンテナはQAや本番システムから移行されることが多いため、コンテナ定義に認証情報をハードコーディングしないことをお勧めします。
ヒント
DockerfileでGoエージェントを使用するサンプルアプリケーションを参照したい場合は、Go Test Benchプロジェクトをご覧ください。
開始する前に
コンテナや関連ソフトウェアの仕組みを基本的に理解している必要があります。
必要に応じて、お客様の環境に合わせて手順を調整してください。
Goアプリケーションをインストール、ビルド、実行
Goがインストールされていることを確認します。
以下のコマンドで、
contrast-goをインストールします。RUN github.com/contrast-security-oss/contrast-go-installer@latest latest
通常の
go buildコマンドをcontrast-go buildに置き換えて、アプリケーションをビルドします。このステップによって、Contrastが組み込まれた実行ファイルがビルドされます。RUN contrast-go build ./app
環境変数で、エージェントを設定します。
Dockerの例
DockerコンテナにGoアプリケーションをインストール、ビルド、および実行する方法の例として、以下を参照ください。
注記
The process to set environment variables when using a cloud provider typically involves using a secrets manager and then linking the values of those secrets to the environment variables.
# Step 1: Install Go. You can use a different base image than the one shown in # this example. FROM golang:1.21 AS builder WORKDIR /build COPY . . # Step 2: This step installs contrast-go and makes sure it's in your $PATH so # you can use it in the next step. RUN go run github.com/contrast-security-oss/contrast-go-installer@latest latest # Step 3: This step is your normal build step, but uses contrast-go instead of # go. This step doesn't replace Go; it just wraps it so that it can add # instrumentation during the build process. RUN contrast-go build ./app # Optional: Move the finished build to a new container. # Not required, but nice to have! FROM alpine:latest COPY --from=builder /build/app . # Step 4: Configure the agent using enviornment variables. ENTRYPOINT ["./app"]
環境変数の設定例
注記
Contrastエージェント設定エディタのExport(エクスポート)オプションを使用すると、Contrastの認証情報の環境変数を簡単に作成できます。
例えば、次のコマンドを使用して、コンテナを構築することができます。
docker build -t my-app-image
そして、コンテナの実行時には次のコマンドを使用します。
docker run -p 3000:3000 --name my-app-instance \ -e "CONTRAST__API__URL=<YourTsUrl>" \ -e "CONTRAST__API__API_KEY=<YourApiKey>" \ -e "CONTRAST__API__SERVICE_KEY=<YourServiceKey>" \ -e "CONTRAST__API__USER_NAME=<YourUserName>" \ my-app-image
関連項目
The following articles are on the Contrast Support Portal: