Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to run prefork mode inside docker? 🤗 #1021

Closed
renatojf opened this issue Nov 14, 2020 · 4 comments
Closed

Is it possible to run prefork mode inside docker? 🤗 #1021

renatojf opened this issue Nov 14, 2020 · 4 comments

Comments

@renatojf
Copy link

Question description
When I run fiber with prefork: false inside docker, all works fine. prefork: true causes the container to panic out.

Code snippet Optional

package main

import "github.com/gofiber/fiber/v2"

func main() {
  app := fiber.New(fiber.Config{
	Prefork: true,
  })

go func() {
	if err := app.Listen(fmt.Sprintf(":%d", viper.GetInt("server.port"))); err != nil {
		log.Panic(err)
	}
  }()

  c := make(chan os.Signal, 1)   // Create channel to signify a signal being sent
  signal.Notify(c, os.Interrupt) // When an interrupt is sent, notify the channel

  _ = <-c // This blocks the main thread until an interrupt is received
  fmt.Println("Gracefully shutting down...")
  _ = app.Shutdown()
}

**Output

$ docker run test
┌───────────────────────────────────────────────────┐  ┌───────────────────────────────────────────────────┐
 │                    Fiber v2.2.0                   │  │ Child PIDs ... 16, 17, 21, 28                     │
 │               http://127.0.0.1:3000               │  └───────────────────────────────────────────────────┘
 │                                                   │
 │ Handlers ............. 7  Threads ............. 4 │
 │ Prefork ........ Enabled  PID ................. 1 │
 └───────────────────────────────────────────────────┘

2020/11/14 21:17:53 exit status 1
panic: exit status 1

goroutine 19 [running]:
log.Panic(0xc00009dfb8, 0x1, 0x1)
        /usr/local/go/src/log/log.go:351 +0xae
main.main.func2(0xc0000cb320)
        /build/cmd/ad-delivery/main.go:55 +0x110
created by main.main
        /build/cmd/ad-delivery/main.go:53 +0x507
@welcome
Copy link

welcome bot commented Nov 14, 2020

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@hi019
Copy link
Contributor

hi019 commented Nov 19, 2020

We just did a little investigation in the Discord. Prefork uses environment variables so it will need to be executed within a shell. Docker docs:

Unlike the shell form, the exec form (CMD [...]) does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form (CMD ./app) or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

So execute your app with either CMD ./app or CMD ["sh", "-c", "/app"]. Let me know if this works.

We've also mentioned this case in the docs, gofiber/docs#144

@renatojf
Copy link
Author

Sorry I have missed the discussion in the Discord chat :(
Thanks a lot for looking into this one. I confirm it runs perfectly now!

@hi019 hi019 closed this as completed Nov 20, 2020
@slaveofcode
Copy link

Another workaround if you still have a problem running Prefork: true inside a docker container
#1036

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants