#go service exits with code 0 immidiately

27 messages · Page 1 of 1 (latest)

hazy stone
#

Hello everyone, I have a problem with my service. It exits with code: 0 but when I manually start the service it is all fine. What do you think could be the problem here?

I have tried using tty: true and stdin_open: true but none of them worked.

Dockerfile

FROM golang:1.16-alpine

ADD . /go/src/user-service
WORKDIR /go/src/user-service

RUN go build -o /user-service

EXPOSE 8080

ENTRYPOINT [ "/user-service" ]

docker-compose.yml

version: '3.3'

services:

  database:
    image: mongo
    ports:
      - "27017:27017"
    healthcheck:
      test: mongo --norc --quiet --host=localhost:27017 --eval "db.getMongo()"
      interval: 30s
      timeout: 2s
      retries: 3
      start_period: 15s

  user:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      database:
        condition: service_healthy
ruby glade
#

what is your code doing

hazy stone
astral raft
#

can you try running it manually and see if that also exits

hazy stone
tight bolt
hazy stone
# tight bolt how does the connection to mongo look like
func InitDatabase(databaseUri string) *mongo.Database {
    client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(databaseUri))
    if err != nil {
        log.Println(err)
        panic(err)
    }

    err = client.Ping(context.TODO(), readpref.Primary())
    if err != nil {
        log.Println(err)
        panic(err)
    }

    database := client.Database(DatabaseName)

    return database
}
tight bolt
#

whats ur databaseuri

hazy stone
# tight bolt whats ur databaseuri

this is my main

func main() {
    router := gin.Default()

    validator := validator.New()
    database := repository.InitDatabase("mongodb://database:27017")
    userRepository := repository.NewUserRepository(database)
    userService := service.NewUserService(userRepository)
    userController := controller.NewUserController(userService, validator)

    router.GET("/users", userController.GetUsers)
    router.GET("/users/:id", userController.GetById)
    router.POST("/users", userController.Create)
    router.PATCH("/users/:id", userController.UpdateById)
    router.DELETE("/users/:id", userController.DeleteById)

    router.Run()
}```
tight bolt
#

that looks fine

#

no other logs than the exist code?

hazy stone
tight bolt
#

database is up 15 seconds BEFORE service is

#

that shouldnt be an issue

#

unless your db is fucked up

hazy stone
#

can I screen share for a second? @tight bolt

tight bolt
#

no

#

im playing league

ruby glade
#

It would not exit with 0 as you panic

warm fjord
#

Doesn’t router Run return an error?

#

Might want to check that one

hazy stone
#

I changed docker-compose to this and it worked

version: '3.8'

services:

  database:
    image: mongo
    container_name: mongo-db
    ports:
      - "27017:27017"

  user:
    container_name: user-api
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - database
warm fjord
#

My bet is still on ignored errors

marsh basin
#

non-blocking main goroutine

#

db connection fails, app dies

warm fjord
#

Also a good guess