0

My source code works but when I build a docker image and run it, it skips the line that gets user input, goes to the next line and crashes there. My dockerfile:

FROM golang

WORKDIR /usr/src/app

COPY go.mod go.sum ./

RUN go mod download && go mod verify

COPY . .

RUN go build -v -o /usr/local/bin/app ./...

CMD ["app"]

Then I run the command "docker run --env-file ./.env youtubeanalytictool" and it crashes. My code is really long so I'll just post a small snippet of where the problem is:

fmt.Println("Enter channel id: ")
fmt.Scanln(&id)

channel := getChannelInfo(service, id)

When I run the image, I get the error:

Enter channel id: 
panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.getChannelInfo(0x9a4bc0?, {0x0, 0x0})
        /usr/src/app/retrieve_data.go:47 +0x3f9
main.main()
        /usr/src/app/main.go:34 +0x25c

It skips the line where it scans for user input and goes straight to the next line. This doesn't happen when I run my source code. Am I missing something in my dockerfile that allows user input?

Alisha
  • 51
  • 3
  • 3
    If you're expecting user input you need to add the `-i` and `-t` options to your `docker run` invocation: `docker run -it ...` (see the `docker-run` man page for details on what these options do). – larsks Feb 21 '23 at 21:38
  • Note that Docker is intended to run daemons, not interactive commands. You can do it, but it doesn't suit the container model very well. – Adrian Feb 21 '23 at 21:53
  • ok it works with the -it flag – Alisha Feb 21 '23 at 23:19
  • Does this answer your question? [How do I get into a Docker container's shell?](https://stackoverflow.com/questions/30172605/how-do-i-get-into-a-docker-containers-shell) – Hi computer Feb 21 '23 at 23:51

0 Answers0