fix(port mapping,docker): fixed duplicate port mapping on hosts and incompatible docker options in compose

Signed-off-by: Tuan-Dat Tran <tuan-dat.tran@tudattr.dev>
This commit is contained in:
Tuan-Dat Tran
2025-01-17 02:10:36 +01:00
parent e68d534e4f
commit 6bc591550c
3 changed files with 13 additions and 5 deletions

View File

@@ -178,7 +178,7 @@ services:
ports: ports:
- name: "http" - name: "http"
internal: 8000 internal: 8000
external: 8000 external: 8001
- name: sonarr - name: sonarr
vm: vm:
- docker-host00 - docker-host00
@@ -410,7 +410,7 @@ services:
ports: ports:
- name: "http" - name: "http"
internal: proxy_only internal: proxy_only
external: 8082 external: 8083
environment: environment:
- PUID=1000 - PUID=1000
- PGID=1000 - PGID=1000

View File

@@ -2,19 +2,24 @@ services:
{% for service in services %} {% for service in services %}
{% if inventory_hostname in service.vm %} {% if inventory_hostname in service.vm %}
{{service.name}}: {{service.name}}:
hostname: {{service.name}}
container_name: {{ service.container_name }} container_name: {{ service.container_name }}
image: {{ service.image }} image: {{ service.image }}
restart: {{ service.restart }} restart: {{ service.restart }}
{% if service.network_mode is not defined %}
hostname: {{service.name}}
networks: networks:
- net - net
{% if service.ports is iterable%} {% endif %}
{% if service.ports is iterable %}
{% if service.ports[0].internal != 'proxy_only' %}
ports: ports:
{% for port in service.ports %} {% for port in service.ports %}
{% if port.internal != 'proxy_only' %} {% if port.internal != 'proxy_only' %}
- {{port.external}}:{{port.internal}} - {{port.external}}:{{port.internal}}
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% endif %}
{% endif %}
{% if service.cap_add is defined and service.cap_add is iterable %} {% if service.cap_add is defined and service.cap_add is iterable %}
cap_add: cap_add:
{% for cap in service.cap_add %} {% for cap in service.cap_add %}

View File

@@ -5,9 +5,11 @@
{% for service in services %} {% for service in services %}
{% if service.ports is iterable %} {% if service.ports is iterable %}
{% set http_port = service.ports | selectattr('name', 'equalto', 'http') | map(attribute='external') | list %}
{% if http_port %}
{{ service.name }}.{{ domain }} { {{ service.name }}.{{ domain }} {
{% for vm in service.vm %} {% for vm in service.vm %}
reverse_proxy {{ hostvars[vm].ansible_host }}:{{ service.ports | selectattr('name', 'equalto', 'http') | map(attribute='external') | first }} reverse_proxy {{ hostvars[vm].ansible_host }}:{{ http_port[0] }}
{% endfor %} {% endfor %}
log { log {
output file /var/log/caddy/{{ service.name }}.log output file /var/log/caddy/{{ service.name }}.log
@@ -25,4 +27,5 @@
} }
} }
{% endif %} {% endif %}
{% endif %}
{% endfor %} {% endfor %}