How to get user input in Docker with Go

Viewed 176

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:

enter image description here

I want to get user input from terminal when run docker container

1 Answers

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.

Related