Help with Docker config

1. Caddy version (caddy version):

caddy:alpine

2. How I run Caddy:

caddy file-server --root /usr/share/caddy/html --domain new.ordename.cl

a. System environment:

Docker: FROM caddy:alpine

b. Command:

caddy file-server --root /usr/share/caddy/html --domain new.ordename.cl

c. Service/unit/compose file:

FROM node:16 as build

ENV REACT_APP_CALENDLY_ACCOUNT=https://calendly.com/ordenameventas

WORKDIR /app

COPY package.json ./

RUN npm i

COPY . ./

RUN npm run build

FROM caddy:alpine

COPY caddy/Caddyfile /etc/Caddyfile
COPY --from=build /app/build /usr/share/caddy/html

ENTRYPOINT ["caddy", "file-server", "--root", "/usr/share/caddy/html", "--domain", "new.ordename.cl"]

d. My complete Caddyfile or JSON config:

new.ordename.cl {
	gzip
	log {
		output stdout
	}
	root /usr/share/caddy/html
	rewrite {
		regexp .*
		to {path} /
	}
}

3. The problem I’m having:

Hi guys! I’m trying to deploy a dockerized react app using Caddy. Everything seems to be working fine, the https certificate is generated, and the index of the page is accessible:

curl -v https://new.ordename.cl/

But if I try to go to any other link on the page, I get a 404 Not Found error:

curl -v https://new.ordename.cl/products

4. Error messages and/or full log output:

5. What I already tried:

I’ve tried different configurations in the Caddyfile but nothing seems to work.
What am I doing wrong? Any help would be really appreciated!

6. Links to relevant resources:

Which version, exactly? That’s just the docker tag you used to pull the image, but we need to know which version of Caddy you have pulled down.

You can find it by running caddy version inside the container.

If you use the caddy file-server command, then Caddy won’t use your Caddyfile at all.

Also, the default command in the official Caddy docker image uses /etc/caddy/Caddyfile as the path to the file. You’re copying your Caddyfile to the wrong location if you were to use the default command.

You’re using Caddy a v1 config here. You’ll need to rewrite your config with Caddy v2 syntax.

This is because you’re using caddy file-server which doesn’t do any special rewrites for you at all. So requests to /products is literally looking for a file or directory on disc at /usr/share/caddy/html/products, but it doesn’t find that.

2 Likes

Hi Francis! Thanks for such a detailed answer. I did’t know I had so many things wrong with my configuration. Answering your comments:

Which version, exactly? That’s just the docker tag you used to pull the image, but we need to know which version of Caddy you have pulled down.

You can find it by running caddy version inside the container.

I decided to pull an image with a specific version, so now I’m doing:

FROM caddy:2.4.6-alpine

If you use the caddy file-server command, then Caddy won’t use your Caddyfile at all.

I suspected this, but I couldn’t confirm it since I didn’t find many information about this command and almost no examples online. Thanks for clarifying this!

Also, the default command in the official Caddy docker image uses /etc/caddy/Caddyfile as the path to the file. You’re copying your Caddyfile to the wrong location if you were to use the default command.

Fixed!

COPY caddy/Caddyfile /etc/caddy/Caddyfile

You’re using Caddy a v1 config here. You’ll need to rewrite your config with Caddy v2 syntax.

I had no idea my config was outdated. What do you think know about it?

new.ordename.cl

encode gzip
root * /usr/share/caddy/html
try_files {path} /index.html
file_server
log

This is because you’re using caddy file-server which doesn’t do any special rewrites for you at all. So requests to /products is literally looking for a file or directory on disc at /usr/share/caddy/html/products , but it doesn’t find that.

Understood. Correct me if I’m wrong but now, with the proper Caddyfile config, I now should start caddy with “run” and it will use the config and start my server correctly?

ENTRYPOINT ["caddy", "run"]
1 Like

That’s probably correct. You might want to use a block (i.e. { }) around your site, in case you plan on serving more than one site.

Just remove this line. The default entrypoint command inherited from the caddy docker image is already correct.

2 Likes

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