1

Hello everyone i am trying to learn how to wait for user input with golang cli app in docker, her is my main.go file

package main

import (
  "bufio"
  "fmt"
  "os"
  "strings"
  "strconv"
)

func main(){
    reader := bufio.NewReader(os.Stdin)
    var errConv error
    for {
        fmt.Print("-> ")
        inputText, _ := reader.ReadBytes('\n')
        // Receive input
        text := string(inputText)
        var command []string 
        for _, word := range strings.Fields(text) {
            command = append(command,word)
        }

        if command[0] == "create_day_max"{
           //do something
        }
    }
}

this is my Dockerfile

    # Start from golang base image
    FROM golang:1.15.2
    
    # Copy the source from the current directory to the working Directory inside the container 
    COPY . /usr/src/golang-tunaiku
    
    # Move to working directory
    WORKDIR /usr/src/golang-tunaiku
    
    #install depedencies
    RUN go mod download
    
    # Build the application
    RUN go build -o golang-tunaiku
    
    #Command to run the executable
    CMD [ "./golang-tunaiku" ]

and this is my docker-compose.yml file

version: '3'
services:
  app:
    container_name: golang-linkaja
    build: .

it run fine when i do go run main.go but when i use docker-compose up --build , it just does not wait for user input, thus it create error like this

-> panic: runtime error: index out of range


which is obvious since it is forcing to read index 0 while there is no user input, How do i wait for user input on golang clip app with docker compose? Thanks in advance i am stil learning
radren
  • 154
  • 2
  • 14
  • 1
    Does it work when running Docker in interactive mode like [here](https://stackoverflow.com/a/66545301)? – xarantolus Mar 11 '21 at 07:03
  • 1
    Also note that it's going to be a little tricky to directly feed input into a program running in Compose. You might run this with `docker` directly as described in the linked question, or use a `net/http` interface to accept input over the network instead of from the console. – David Maze Mar 11 '21 at 12:21

0 Answers0