Autosave.json not persisted with volumes

1. Caddy version (caddy version):

v2.4.6

2. How I run Caddy:

Defined as a service in docker-compose.yml. see below

a. System environment:

b. Command:

docker-compose up -d

c. Service/unit/compose file:

  caddy:
    image: caddy:2-alpine
    restart: unless-stopped
    depends_on:
      - laravel-api
      - nextjs-platform
      - nextjs-shop
    ports:
      - "80:80"
      - "443:443"
      - "2019:2019"
    volumes:
      - ./docker/dev/caddy/Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config

volumes:
  caddy_data:
    external: true
  caddy_config:

d. My complete Caddyfile or JSON config:

{
    debug
    admin :2019
}

localhost:80 {
    reverse_proxy nextjs-platform:3000

    reverse_proxy /api/* {
         to laravel-api:8000
    }
}


{http.request.host}.localhost:80 {
    reverse_proxy nextjs-shop:3000
    reverse_proxy /api/* laravel-api:8000
}

# These should go to 404 page cause deployment doesnt exist
# new deployments are added over api
*.localhost:80 {
    @try_files {
        not path /_next/* /logo.svg
         file {
            try_files {path} /
         }
    }

    # Redirect to the deployment not found page
    rewrite @try_files /deployment_not_found
    reverse_proxy nextjs-platform:3000
}

3. The problem I’m having:

I run a job that when a new deployment is executed it places a put request to http://caddy:2019/config/apps/http/servers/srv0/routes/0/match/0/host/0
which works fine to add a new host and I can see the new host show up in the /config/caddy/autosave.json

However when i restart or down my docker-compose services I lose the autosave.json (Doesnt persist?)

4. Error messages and/or full log output:

None it all works except I lose my autosave.json on restart

5. What I already tried:

I’ve also tried mounting the volumes differently for example:

    volumes:
      - ./docker/dev/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./docker/dev/caddy/caddy_data:/data
      - ./docker/dev/caddy/caddy_config:/config

Which are paths in my current project directory but in that case I dont even see the autosave.json get created when I exec caddy /bin/sh and go to /config/caddy

6. Links to relevant resources:

and as usual I find the solution 10 minutes later:

The issue was I was missing the --resume flag. So my docker command now looks like this:

    command: ["caddy", "run", "--resume", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]

Working fine now

It’s not recommended to use both a Caddyfile and the config API at the same time. You should just pick one or the other, not both.

The Caddyfile adapter is one-way to JSON, you can’t go back from JSON to Caddyfile. So if you plan to use the API, you should probably maintain a JSON config instead (you can adapt your Caddyfile to JSON at first to get started).

If you make a change to your Caddyfile while --resume is used, your Caddyfile changes will never be used unless you delete the autosave file – but if you do that, you’ll lose all the changes you made via the API.

So yeah, to avoid those risks, it’s best to go all-in on JSON + API, or all-in on updating the Caddyfile with your changes and reloading Caddy afterwards.

2 Likes

Thank you @francislavoie for the heads up that makes sense.

I will definitely implement that as soon as I get further (just starting out to figure out the basics and loving it so far)

1 Like

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