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

Doesn't bind ports with docker enabled on Mac OS X #2455

Closed
seanhess opened this issue Aug 8, 2016 · 12 comments
Closed

Doesn't bind ports with docker enabled on Mac OS X #2455

seanhess opened this issue Aug 8, 2016 · 12 comments

Comments

@seanhess
Copy link

seanhess commented Aug 8, 2016

Hi, I'm writing a web server, and would like to use stack with docker enabled. I am able to use stack build and stack image container to successfully create a docker image, then run it using docker run -p NNNN:NNNN. However, during development, I normally test by running:

$ stack ghci
> main

My server starts up, and connects to its port, but I can't connect to the port from my host computer.

Steps to reproduce

  1. Clone this repository: https://github.com/seanhess/haskell-docker-example
  2. Start the app in GHCI
$ stack ghci
> main
  1. Attempt to connect via your web browser: http://localhost:8010/

All configuration is in the repo, but here is the main change from the default template:

docker:
  enable: true

image:
  container:
    name: haskell-docker-example
    base: fpco/stack-run

Expected

I expected the server to respond with "Hello World" when I hit http://localhost:8010/ in my web browser.

Actual

Chrome says "The site can't be reached. Localhost refused to connect"

Stack version

$ stack --version
Version 1.1.2 x86_64 hpack-0.14.0

Method of installation

Using homebrew.

@seanhess seanhess changed the title stack ghci doesn't publish ports with docker enabled stack ghci doesn't open ports with docker enabled Aug 8, 2016
@seanhess seanhess changed the title stack ghci doesn't open ports with docker enabled stack ghci doesn't bind ports with docker enabled Aug 8, 2016
@borsboom
Copy link
Contributor

borsboom commented Aug 8, 2016

That's surprising. Stack sets Docker Engine to use --net=host by default, which means all ports in the container should be available on the host. You say you use homebrew, so that implies you're on a Mac. Are you using Docker for Mac, or Docker Machine, or another way to run Docker Engine on your system?

@seanhess
Copy link
Author

seanhess commented Aug 8, 2016

I'm using docker for mac.

Version 1.12.0-beta21 (build: 11019)
5a44e81a0513f32f5c49f7d2966570893451f32f

@seanhess
Copy link
Author

seanhess commented Aug 8, 2016

I'm switching to the stable channel to see if that fixes it. I was only on beta to get early access. I'll report back soon.

@seanhess
Copy link
Author

seanhess commented Aug 8, 2016

It didn't help. Here's my new Docker for Mac version.

Version 1.12.0 (build: 10871)
9cd1dc28d3ac81cb5177ff9728f65200304b36f6

I still can't connect with docker: enable: true, but if I comment that out, I can. Thanks for your help!

@borsboom
Copy link
Contributor

borsboom commented Aug 8, 2016

Ok, I've done some more digging, and it appears --net=host is broken on Docker for Mac. Not only does it not share the host's network, but it even breaks the -p option so published ports don't work either. Switching it back to bridge mode at least makes the -p option work. Please try this command and let me know if it helps:

stack --docker-run-args '--net=bridge -p 8010:8010' ghci

@seanhess
Copy link
Author

seanhess commented Aug 8, 2016

That command does work, thanks. Your explanation matches what I'm seeing.

So this is on docker's end? Did you find/file a bug report I can track?

On Mon, Aug 8, 2016 at 12:06 PM Emanuel Borsboom [email protected]
wrote:

Ok, I've done some more digging, and it appears --net=host is broken on
Docker for Mac. Not only does it not share the host's network, but it even
breaks the -p option so published ports don't work either. Switching it
back to bridge mode at least makes the -p option work. Please try this
command and let me know if it helps:

stack --docker-run-args '--net=bridge -p 8010:8010' ghci


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#2455 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAA_ebEk3uDmyc0x96NeKnhUHG7u2v51ks5qd2-HgaJpZM4JfQBe
.

@borsboom
Copy link
Contributor

borsboom commented Aug 8, 2016

It is on Docker's end (I get the same behaviour just using plain docker run commands). This issue appears to be related: docker/for-mac#68

@borsboom borsboom changed the title stack ghci doesn't bind ports with docker enabled stack ghci doesn't bind ports with docker enabled on Mac OS X Aug 8, 2016
@borsboom borsboom changed the title stack ghci doesn't bind ports with docker enabled on Mac OS X Doesn't bind ports with docker enabled on Mac OS X Aug 8, 2016
@mgsloan
Copy link
Contributor

mgsloan commented Aug 8, 2016

Are there any more actions for us to take from stack's end? Re-open if so.

@snap9
Copy link

snap9 commented Apr 20, 2017

@seanhess Hey, were you able to find a workaround, or do you just have to compile the app (without ghci) every time to test it?

cc @mgsloan @borsboom

@seanhess
Copy link
Author

seanhess commented Apr 20, 2017

@snap9 Not really. It doesn't look like there will be any movement on the linked docker issue either so it doubt it will be resolved soon. We stopped using stack with docker for local development, it wasn't fast enough to support hdevtools/ghc_mod either. Since stack already does a good job providing reproducible builds, we have docker turned off during development, and just use the --docker flags to dockerize it on our CI server.

@snap9
Copy link

snap9 commented Apr 20, 2017

@seanhess Hey, I think I got it to work. All I did was

stack --docker-run-args='--net=bridge -p 8000:8000' ghci

From there I can call main I can connect to localhost:8000 on my macOS Sierra. (I'm assuming of course that main runs on port 8000 by default.)

(One mysterious thing is that removing the -p 8000:8000 — or replacing it with -p 8000:8123 and trying localhost:8123 on macOS — doesn't work at all.)

@dunnl
Copy link

dunnl commented Sep 29, 2017

A note to those who find this thread by Google: You may well find that the cause of your problems has to do with the hostname your socket has bound to. In my case, using the Hakyll preview server, it turns out the default hostname passed to Warp is "127.0.0.1", which only accepts connections from the container itself (in bridge mode, I'm not sure about host mode because it doesn't seem to work on OSX). The solution is to pass "*" instead. A bit mysterious to debug, because at first glance it looks like something is wrong with Docker.

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

6 participants