Caddy: v2: Docker on Mac unable to reach the locally running docker container's exposed ports

Created on 23 Oct 2019  路  5Comments  路  Source: caddyserver/caddy

1. Which version of Caddy are you using (caddy -version)?

v2, commit faf67b10670a14c24ce601be703dfb65f07ffa45

2. What are you trying to do?

I am trying to run caddy 2 in a local docker container and configure the caddy 2 server from my host machine (Mac).

3. What is your Caddyfile?

{
        "apps": {
            "http": {
                "servers": {
                    "example": {
                        "listen": ["127.0.0.1:2080"],
                        "routes": [
                            {
                                "handle": [{
                                    "handler": "file_server",
                                    "browse": {}
                                }]
                            }
                        ]
                    }
                }
            }
        }
    }

4. How did you run Caddy (give the full command and describe the execution environment)?

I built the Docker container from the following Dockerfile:

FROM golang:latest

WORKDIR /app

RUN git clone -b v2 "https://github.com/caddyserver/caddy.git"

WORKDIR /app/caddy/cmd/caddy/

RUN go build

EXPOSE 2019 2080

CMD ["./caddy", "run"]

5. Please paste any relevant HTTP request(s) here.

$ curl -X POST "http://localhost:2019/load" \
    -H "Content-Type: application/json" \
    -d @- << EOF
    {
        "apps": {
            "http": {
                "servers": {
                    "example": {
                        "listen": ["127.0.0.1:2080"],
                        "routes": [
                            {
                                "handle": [{
                                    "handler": "file_server",
                                    "browse": {}
                                }]
                            }
                        ]
                    }
                }
            }
        }
    }
EOF

6. What did you expect to see?

I expected the locally running docker container to respond to my request.

7. What did you see instead (give full error messages and/or log)?

curl: (52) Empty reply from server

8. Why is this a bug, and how do you think this should be fixed?

I believe this may be a bug (but it may also intentionally be this way during beta?). I forked the repo and tested a change out here: https://github.com/mikstur/caddy/commit/221aeaa8970714d1e8e8de4355127f34ca701498

I think the caddy server should listen on 0.0.0.0 to allow for this. Is there any reason not to?

9. What are you doing to work around the problem in the meantime?

In the meantime I have forked the repo and applied a test fix there. I am using that repo in my docker build: https://github.com/mikstur/caddy/commit/221aeaa8970714d1e8e8de4355127f34ca701498.

Additionally, following the tutorial POST. Once my fix was in place, I was able to reach the locally running docker container by POSTing my config (above). However, I also needed to listen on host 0.0.0.0 in order for me to reach the configured server on port 2080.

                        "listen": ["0.0.0.0:2080"],
                        "routes": [
                            {
                                "handle": [{
                                    "handler": "file_server",
                                    "browse": {}
                                }]
                            }
                        ]

10. Please link to any related issues, pull requests, and/or discussion.

During debugging I came across this post/issue from another project, which pointed me in the right direction: https://stackoverflow.com/questions/43911793/cannot-connect-to-go-grpc-server-running-in-local-docker-container

Bonus: What do you use Caddy for? Why did you choose Caddy?

I am super excited about Caddy. Really cool project. Right now I am just experimenting and playing with it for a hobby project. I want to be able to dynamically configure servers via REST, with HTTPs provisioning.

question

Most helpful comment

@mikstur I would suggest you use a (immutable) caddyfile to configure your caddy server. Take a look at https://github.com/Ilyes512/caddy-docker. It will hopefully soon be moved to inside the caddy org after some more work :)

All 5 comments

Regarding your config, you'll need to listen on 0.0.0.0. Listening on 127.0.0.1 will only allow requests from other apps within the same container (which you don't have any, looking at your Dockerfile). If you actually want to only allow your host machine to make requests, you'll need to figure out the host machine's IP from the perspective of the Caddy container (e.g. https://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container) and listen to that IP. But that's probably overkill, easier to just use 0.0.0.0, especially if you don't have port forwarding set up to your machine in your local network.

The recommended way to run caddy in a container is to use a config file as a base. If you do so, then you can set the admin endpoint (see here: https://github.com/caddyserver/caddy/wiki/v2:-Documentation#admin), no need to make code changes.

If you plan to always be configuring Caddy via CURL from scratch, then you can start with this base json config:

{
    "admin": {
        "listen": "0.0.0.0:2019"
    }
}

@mholt I think there should be an admin_listen option in global Caddyfile config maybe?

@francislavoie wow thanks so much. I can鈥檛 believe I missed this in the documentation.

@francislavoie Yeah, more admin controls are planned for the Caddyfile. The admin endpoint will be getting more attention before the release candidate.

@mikstur I don't blame you, it's a long page. Temporary, until we stand up the new v2 docs site.

Thanks for the detailed report.

@mikstur I would suggest you use a (immutable) caddyfile to configure your caddy server. Take a look at https://github.com/Ilyes512/caddy-docker. It will hopefully soon be moved to inside the caddy org after some more work :)

@Ilyes512 this is epic, thanks so much :).

Was this page helpful?
0 / 5 - 0 ratings