Comments (6)
Depends on how much "automatic" you want it to be. And what sacrifices you're willing to make.
With docker-compose you can use YAML anchors . Add this on top of your configuration (before services
):
networks:
ingress:
x-constants:
- &default_restart unless-stopped
x-snippets:
env: &with_env
environment: &env
PUID:
PGID:
TZ:
net: &with_net
networks: &net
ingress:
x-labels:
- &caddy_auto_name
caddy: &auto_name '{{ index .Labels "com.docker.compose.service" }}.${BASE_DOMAIN}'
- &simple_expose
caddy: *auto_name
caddy.reverse_proxy: 'http://{{ index .Labels "com.docker.compose.service" }}:{{ (index .Ports 0).PrivatePort }}'
- &tls_strip
caddy.reverse_proxy.transport: http
caddy.reverse_proxy.transport.tls_insecure_skip_verify:
x-templates:
default: &default
environment: *env
networks: *net
restart: *default_restart
dummy: &dummy
restart: *default_restart
environment: *env
init: true
network_mode: none
read_only: true
image: alpine
command: sleep infinity
This is part of my config, which I stripped down to your use-case.
After that you can use it like this:
services:
qr:
<<: *default
build: ./build/qr
environment:
PORT: 8080
labels: *simple_expose
prometheus:
<<: *default
image: prom/prometheus:latest
command:
- --config.file=/etc/prometheus/config.yml
- --web.external-url=https://prometheus.${BASE_DOMAIN}
- --log.format=json
volumes:
- /config/prometheus:/etc/prometheus/
- data_prometheus:/prometheus
expose:
- "9090/tcp"
labels: *simple_expose
graph:
<<: *default
image: grafana/grafana
environment:
<<: *env
GF_PLUGINS_ALLOW_LOADING_UNSIGNED_PLUGINS: jasonlashua-prtg-datasource,natel-discrete-panel,fatcloud-windrose-panel
GF_SERVER_ROOT_URL: https://graf.${BASE_DOMAIN}
GF_LOG_CONSOLE_FORMAT: json
volumes:
- data_grafana:/var/lib/grafana
- conf_grafana:/etc/grafana/provisioning
expose:
- "3000/tcp"
labels: *simple_expose
# ....
It will use the service name, $BASE_DOMAIN
from .env
file, and first exposed/forwarded port of your container as parameters for reverse_proxy
statement.
yet another approach
You can use a snippet. Define it in global configuration file:
# enable global config in caddy service
environment:
CADDY_DOCKER_CADDYFILE_PATH: /config/Caddyfile.globals
CADDY_DOCKER_PROCESS_CADDYFILE: "true"
BASE_DOMAIN: # null means "pick from the .env file"
(rproxy) {
{args[0]}.{env.BASE_DOMAIN} {
reverse_proxy {args[1:]}
}
}
services:
foo:
<<: *default
# ....
labels:
caddy: import rproxy foo "{{upstreams 8080}}"
I used this in the past.
Pros:
- you have the most explicit configuration while being shorter than multiple statement
- you can have exposed name to be different from service name
Cons: - you still have to repeat a boilerplate
- you only shorten configs by one label
This approach is better if you have to throw more labels at your containers (for example if you use pawelmalak/flame
, benphelps/homepage
or something similar.
yet another another approach
You can combine snippets and cryptic go-templates.
Disclaimer: I haven't tried this one. I use what I described in the first part, because I like autogenerated config to be more verbose
(rproxy) {
{args[0]}.{env.BASE_DOMAIN} {
reverse_proxy {args[1:]}
}
}
x-labels:
- &simple_expose
caddy: import rproxy {{ index .Labels "com.docker.compose.service" }} {{ (index .Ports 0).PrivatePort | upstreams }}
It should work like this, or maybe with some quotes thrown here or there.
from caddy-docker-proxy.
I don't understand. Can't you just add labels to do that?
from caddy-docker-proxy.
Yes, but, I don't want to add labels. I want to implicitly give EVERY container a locally proxied domain name as a default
from caddy-docker-proxy.
That's not supported (and I'm not sure it would be possible anyway).
The problem is the port number that a container is exposing it not known. So automatically proxying is not possible without some configuration.
from caddy-docker-proxy.
i think hes trying to archive something similar to OrbStack witch would be awesome as a plugin, this is how i landed here
from caddy-docker-proxy.
That's not supported (and I'm not sure it would be possible anyway).
The problem is the port number that a container is exposing it not known. So automatically proxying is not possible without some configuration.
@francislavoie
We can retrieve the exposed port numbers and use the first one, right? Of course, unexposed ports won't be found. I'd like to filter on "network" as well. In my case, I add all my proxied containers to the network "proxynetwork".
docker container ls --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}\t{{.Networks}}" -a
i think hes trying to archive something similar to OrbStack witch would be awesome as a plugin, this is how i landed here
@kiwina
I just looked at this, and this is exactly what I want.
In the end, I could make a custom "docker monitor" and have it add the labels manually, but ideally, this would be done by this service since it's already doing something similar.
from caddy-docker-proxy.
Related Issues (20)
- Feature request: Apply labels after a service is healthy HOT 2
- Add another example for a static configuration
- Add an manual written base config HOT 4
- How to map multiple handlers? HOT 2
- Docker v26 support (TLS mandatory now) HOT 2
- "Container is not in same network as caddy" even tho they are HOT 3
- Clarification of labels needed
- Enhancement: Add requests per second to /metrics HOT 1
- Caddy config not reloaded HOT 4
- Redirecting to IP? HOT 1
- Enable localhost:2019/config/ HOT 4
- rate-limit: Do we need to build our own caddy-docker-proxy image? HOT 7
- Any way to get metrics on 2019 port? HOT 4
- `Failed to get Container Caddyfile` HOT 3
- Run server NOT in docker? (but controller in docker) HOT 1
- Add --pidfile flag to docker-proxy
- wildcard not working HOT 2
- IPv6 Logging not working HOT 2
- Caddy 2.8 Final Release HOT 2
- Trouble with generic logging snippet import HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caddy-docker-proxy.