Help deploying Caddy with Cloudflare DNS

Continuation of this post How to download file with caddy browse - #7 by matt

I now have a file server to serve files. I now want to deploy it on my own DNS (files.nikiv.dev) in my case.

I searched caddy docs and asked ChatGPT and came up with this

files.nikiv.dev

file_server browse

tls {
	dns cloudflare {env.CF_API_TOKEN}
}

@kmmacros path *.kmmacros
header @kmmacros {
	Content-Type application/octet-stream
	Content-Disposition `attachment; filename="{file}"`
}

I am trying now to create a Docker image and deploy it.

My Dockerfile I put in same folder is this

FROM caddy:2-alpine

COPY Caddyfile /etc/caddy/Caddyfile
COPY . /usr/share/caddy

EXPOSE 80
EXPOSE 443

image

I now ran docker build -t files-nikiv-dev .

And then

docker run -d -p 80:80 -p 443:443 files-nikiv-dev
8861c656d4e3cf8ad7b21bea3aadc957fc2e61cef83df31b1fbd59b85cd12757

But I don’t get what to do next.

Remember that you need to ensure your domain name files.nikiv.dev is correctly configured to point to the server where you're running your Caddy server. This would typically involve creating A (or AAAA for IPv6) record in your domain's DNS settings, pointing to the public IP address of your server.

ChatGPT said above but its confusing what that means. I have to go to cloudflare dashboard and do something? Edit more things in Caddyfile?

Thank you lots.

See the docs on Docker Hub, it explains how to build Caddy with plugins.

Please also read How to use DNS provider modules in Caddy 2

Also, I recommend using Docker Compose instead of manual docker run commands. See here: Keep Caddy Running — Caddy Documentation

2 Likes

Thank you, newish to Docker, only wrote simple things before.

I have this docker-compose.yml now

I am not sure if I left out something.

With regards to

Please also read How to use DNS provider modules in Caddy 2

I have this Caddyfile now. The 44.. is my cloudflare api key i hard code currently into the file.

files.nikiv.dev

file_server browse

tls {
	dns cloudflare 4r..
}

@kmmacros path *.kmmacros
header @kmmacros {
	Content-Type application/octet-stream
	Content-Disposition `attachment; filename="{file}"`
}

I am actually confused do I need a Dockerfile still now that I have docker-compose.yml.

And if so what should it include?

FROM caddy:2.6.4-alpine

COPY Caddyfile /etc/caddy/Caddyfile
COPY . /usr/share/caddy

EXPOSE 80
EXPOSE 443

FROM caddy:2.6.4-alpine-builder AS builder

RUN xcaddy build \
    --with github.com/caddy-dns/cloudflare

FROM caddy:2.6.4-alpine

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Above I guess?

I am rereading the docs again but it’s not clear to me still if above is correct and how I can actually deploy it.

Also if you can help adjust my caddyfile to adjust for this would be grateful

So caddy only in http mode and no docker-compose

Reason is that I wanted to deploy caddy on https://railway.app but they don’t support docker compose sadly

I guess though I can just deploy it on google cloud run instead so it’s non issue.

Ok trying this now

Didn’t find official Caddy guides on how to do full deployment step by step so trying above out.

I guess i need to do something in cloudflare dashboard after to make the DNS work too.

Ok it says google cloud run can't mount a volume. I am confused that seems like an essential thing for caddy to work.

GitHub - nikitavoloboev/shared is my current state of caddy + docker-compose.yml

someone shared this template but it does it for static site not for sharing files like I have

Yes, actually!

In your docker-compose.yml where you have image: you instead want to refer to the Dockerfile to build a custom container instead of pulling one from the registry. You’d use build: /path/to/folder.

Since I put my Dockerfile for Caddy right next to the docker-compose.yml file, in my case the context is the current folder, and in my Compose file looks like simply: build: .

And my Dockerfile looks something like:

FROM caddy:2.6.4-alpine-builder AS builder
RUN xcaddy build v2.6.4 \
  --with github.com/caddy-dns/cloudflare

FROM caddy:2.6.4-alpine
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

There’s no need to start from caddy and copy files into builder and then back to caddy again. Just start from the builder and then go to caddy and you’ll have everything there, as it should inherit from caddy:2.6.4-alpine.

Since you’re relying on {env.CF_API_TOKEN} you’ll also need to make sure you add an environment: stanza in the Compose file, something like:

    environment:
      - "CF_API_TOKEN=44.."

Or use Docker secrets.

Once you’ve got build: in your Compose file, you run docker compose build caddy and it will run the Dockerfile. Then run docker compose up caddy and watch the first run to make sure it’s configured and gets a certificate correctly before daemonising it with -d.

This part sounds a little odd, though. Is this still a current issue with the Compose file in the repository you linked?

3 Likes

I build caddy with every DNS providers here GitHub - princemaple/docker-caddy: Build caddy from source

Docker You can use my “caddy with cloudflare” image if you like.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.