英文:
How to get user input in Docker with Go
问题
main.go
package main
import (
"bufio"
"os"
"time"
)
func main() {
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
panic(err)
}
time.Sleep(time.Second * 10)
}
Dockerfile
FROM golang:1.17
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["go","run","main.go"]
创建并运行 Docker 容器时出现错误,如下图所示:
我想在运行 Docker 容器时从终端获取用户输入。
英文:
main.go
package main
import (
"bufio"
"os"
"time"
)
func main() {
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
panic(err)
}
time.Sleep(time.Second * 10)
}
Dockerfile
FROM golang:1.17
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
CMD ["go","run","main.go"]
When create docker container and run it get and error like it:
I want to get user input from terminal when run docker container
答案1
得分: 1
通过使用ENTRYPOINT
而不是CMD
,您的用户可以在docker run
命令之后传递参数:
ENTRYPOINT ["echo", "Hello World"]
docker run <container-name>
> Hello World
docker run <container-name> test
> Hello World test
但是,您应该使用os.Args
来检索这些参数。
英文:
By using ENTRYPOINT
instead of CMD
your user could pass parameters after the docker run
command:
ENTRYPOINT ["echo", "Hello World"]
docker run <container-name>
> Hello World
docker run <container-name> test
> Hello World test
However you should use os.Args
to retrieve those argument.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论