Make caddy file_server start from current folder instead of the configuration folder (using caddy in docker)

1. Caddy version (caddy version):

caddy:2.0.0-alpine

2. How I run Caddy:

I run Caddy inside Docker, it’s configured in my docker-compose.yml as following:


a. System environment:

Linux OS
Docker

b. Command:

No specific command since run with Docker


c. Service/unit/compose file:

caddy: 
    image: caddy:2.0.0-alpine
    restart: always
    volumes:
        - ./Caddyfile:/etc/caddy/Caddyfile
    ports:
        - "8180:8080"
        - "443:443"

d. My complete Caddyfile or JSON config:

Broke my whole Caddyfile down to this which is the main reason of my problems

:8080

file_server browse

3. The problem I’m having:

I would like the file_server to show my current directory (where my docker-compose.yml and my Caddyfile is in) since I want to share static files (inside a static folder) with some reverse proxies I use. Instead the file_server shows me the directory of the caddy configuration (seems like it has something to do with $HOME)

4. Error messages and/or full log output:

No error message, just wrong directory

5. What I already tried:

I’ve already tried manipulating the root in the Caddyfile with following code:

:8080

root * /home/fas/docker_env

file_server browse

The mentioned folder is where I want to be (where my Caddyfile & my docker-compose are located).

I’d like to somehow have it work in the current directory so I can use

file_server /static/* browse

to serve my static files.

6. Links to relevant resources:

I don’t know if that’s a good way of doing it, but I was being able to access the /static folder by inserting it as a volume inside my docker-compose

caddy: 
    image: caddy:2.0.0-alpine
    restart: always
    volumes:
        - ./Caddyfile:/etc/caddy/Caddyfile
        - ./static:/static
    ports:
        - "8180:8080"
        - "443:443"

which makes it possible for me to access the static folder inside the file_server (since it’s now also visible in the “wrong-directory” file_server)

1 Like

Yep, that’s the right approach. Docker containers are completely isolated from the host’s filesystem unless you mount volumes.

Caddy will default the root to the current working directory. When running in Docker, that’ll be /. This is actually something I’m looking to fix now, because it doesn’t make sense for the default root to be /. So thanks for making me spend a minute to think! :stuck_out_tongue:

2 Likes

Glad to hear that! Thank you :slight_smile:

Will I have to change anything after you’ve sucessfully managed to fix the default root usage in Docker?

No, you should always use root anyways honestly.

1 Like

Maybe this will help anyone who will be encountered with this thread in future:

3 days ago there was a change for caddy:2.0.0-alpine which changed the standard working direction from root (/) to /srv (to avoid exposing / directory).

Be aware that you now need to either place the static folder inside the /srv directory, or change the root path back to / by using “root * /” inside the Caddyfile (I used it right after declaring the site address, in my case “:8080”)

Please don’t do that. Exposing /, even though it’s inside a container, is never a good idea.

4 Likes

Okay I’ll keep that in mind, thanks!

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