Home >Backend Development >Golang >How to create a simple Docker container with Go utilities installed

How to create a simple Docker container with Go utilities installed

PHPz
PHPzforward
2024-02-11 16:24:11856browse

如何创建安装了 Go 实用程序的简单 Docker 容器

php editor Zimo will introduce you how to create a simple Docker container and install Go utilities in it. Docker is a popular containerization platform that helps developers package applications with their dependencies for easy deployment and running in different environments. Go is a fast, efficient programming language with many excellent utilities available. This article will show you how to use Docker to create a simple container with Go utilities installed so that you can quickly start using a Go development environment. let's start!

Question content

I have been exploring docker capabilities in order to create simple containers with some go utilities installed. I need to create an image with the gosec and govulncheck utilities installed so that I can run them on my code in the container. My little attempt produced the following results:

# syntax=docker/dockerfile:1
from golang:1.19-alpine

workdir /app
env go111module=on

# copying my code to check
copy go.mod ./
copy go.sum ./
run go mod download
copy *.go ./

run go build -o /docker-gs-ping

run apk add --no-cache git
run go install github.com/securego/gosec/v2/cmd/gosec@latest
run go install golang.org/x/vuln/cmd/govulncheck@latest

expose 8080

cmd [ "gosec ./..." ]

Running the container results in an error:

docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "gosec ./...": stat gosec ./...: no such file or directory: unknown.
ERRO[0000] error waiting for container: context canceled

Looks like I need to specify the path to the installed utility, but I can't get it to work

Workaround

This is not a path issue; it's a path issue. The problem is the syntax you are using in your dockerfile's cmd statement. You are using the json format of the cmd statement; the first parameter in the json list is the name of the command to run. You have asked docker to run a command named gosec ./... which of course does not exist.

You need to split it into multiple list items:

cmd [ "gosec", "./..." ]

Alternatively, you can use the shell form of the cmd command:

CMD  gosec ./...

When you start a container, any of them will run gosec .

The above is the detailed content of How to create a simple Docker container with Go utilities installed. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete