Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.

Commit c891aef

Browse files
committed
Add networks to container
At the moment is a private property that is used to ensure exposed apps are in the same network where the proxy is
1 parent 4687e62 commit c891aef

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

lib/makina/docker.ex

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule Makina.Docker do
1919
name(app),
2020
labels(app),
2121
volumes(app),
22+
network(app),
2223
envs(app),
2324
ports(app),
2425
image(app),
@@ -33,7 +34,7 @@ defmodule Makina.Docker do
3334
Prepares the `docker inspect` command for a give container
3435
"""
3536
def inspect(%Server{} = server, %Application{} = app) do
36-
docker(server, "inspect", [app_name(app)], fn
37+
docker(server, "inspect", ["--type=container", app_name(app)], fn
3738
{:ok, result} ->
3839
info = result[:data] |> String.replace("\n", "") |> JSON.decode!()
3940

@@ -110,6 +111,24 @@ defmodule Makina.Docker do
110111
end)
111112
end
112113

114+
defp network(%Application{domains: []} = app) do
115+
docker_special_config = app.__docker__
116+
117+
Map.get(docker_special_config, :networks, [])
118+
|> Enum.flat_map(fn n ->
119+
["--network", n]
120+
end)
121+
end
122+
123+
defp network(%Application{} = app) do
124+
docker_special_config = app.__docker__
125+
126+
(Map.get(docker_special_config, :networks, []) ++ ["makina-web-net"])
127+
|> Enum.flat_map(fn n ->
128+
["--network", n]
129+
end)
130+
end
131+
113132
defp labels(%Application{} = app) do
114133
labels = Map.get(app.__docker__, :labels, []) ++ hash_label(app) ++ proxy_labels(app)
115134

@@ -149,14 +168,14 @@ defmodule Makina.Docker do
149168
[
150169
"traefik.enable=true",
151170
"traefik.http.middlewares.#{app_name(app)}.compress=true",
152-
"traefik.http.routers.foo.rule=Host\\(#{format_domains(domains)}\\)",
171+
"traefik.http.routers.#{app_name(app)}.rule=\"#{format_domains(domains)}\"",
153172
"traefik.http.routers.#{app_name(app)}.tls.certresolver=letsencrypt",
154-
"traefik.http.services.foo.loadBalancer.server.port=#{first_exposed_port(app)}"
173+
"traefik.http.services.#{app_name(app)}.loadBalancer.server.port=#{first_exposed_port(app)}"
155174
]
156175
end
157176

158177
defp format_domains(domains) when is_list(domains) do
159-
domains |> Enum.map_join(",", fn d -> "\\`#{d}\\`" end)
178+
domains |> Enum.map_join(" || ", fn d -> "Host(\\`#{d}\\`)" end)
160179
end
161180

162181
defp first_exposed_port(%Application{exposed_ports: []}) do

lib/makina/models/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ defmodule Makina.Models.Application do
3838
defstruct __hash__: nil,
3939
__docker__: %{
4040
labels: [],
41-
command: []
41+
command: [],
42+
networks: []
4243
},
4344
__scope__: [],
4445
name: nil,

lib/makina/servers.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ defmodule Makina.Servers do
6464
Application.new(name: "makina-proxy")
6565
|> Application.set_docker_image(name: "traefik", tag: "v3.3")
6666
|> Application.put_exposed_port(internal: 80, external: 80)
67-
|> Application.put_exposed_port(internal: 8080, external: 8080)
6867
|> expose_https_ports(proxy_config)
6968
|> Application.put_volume(
7069
source: "/var/run/docker.sock",
@@ -84,7 +83,8 @@ defmodule Makina.Servers do
8483

8584
Application.set_private(app, :__docker__, %{
8685
app.__docker__
87-
| command:
86+
| networks: [@docker_web_network],
87+
command:
8888
base_command
8989
|> put_https_command_args(proxy_config)
9090
})

0 commit comments

Comments
 (0)