Skip to content

Commit 2e4948a

Browse files
committed
Upstream --inside-docker-env
1 parent 77ce927 commit 2e4948a

File tree

6 files changed

+38
-8
lines changed

6 files changed

+38
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ jobs:
99

1010
env:
1111
MIX_ENV: test
12+
PHX_CI: true
1213

1314
strategy:
1415
matrix:

installer/lib/mix/tasks/phx.new.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ defmodule Mix.Tasks.Phx.New do
140140
install: :boolean,
141141
prefix: :string,
142142
mailer: :boolean,
143-
adapter: :string
143+
adapter: :string,
144+
inside_docker_env: :boolean
144145
]
145146

146147
@reserved_app_names ~w(server table)

installer/lib/phx_new/generator.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ defmodule Phx.New.Generator do
181181
phoenix_path = phoenix_path(project, dev, false)
182182
phoenix_path_umbrella_root = phoenix_path(project, dev, true)
183183

184+
# detect if we're inside a docker env, but if we're in github actions,
185+
# we want to treat it like regular env for end-user testing purposes
186+
inside_docker_env? =
187+
Keyword.get_lazy(opts, :inside_docker_env, fn ->
188+
if System.get_env("PHX_CI") do
189+
false
190+
else
191+
File.exists?("/.dockerenv")
192+
end
193+
end)
194+
184195
# We lowercase the database name because according to the
185196
# SQL spec, they are case insensitive unless quoted, which
186197
# means creating a database like FoO is the same as foo in
@@ -239,7 +250,8 @@ defmodule Phx.New.Generator do
239250
web_adapter_docs: web_adapter_docs,
240251
generators: nil_if_empty(project.generators ++ adapter_generators(adapter_config)),
241252
namespaced?: namespaced?(project),
242-
dev: dev
253+
dev: dev,
254+
inside_docker_env?: inside_docker_env?
243255
]
244256

245257
%{project | binding: binding}

installer/templates/phx_single/config/dev.exs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import Config
66
# The watchers configuration can be used to run external
77
# watchers to your application. For example, we can use it
88
# to bundle .js and .css sources.
9-
config :<%= @app_name %>, <%= @endpoint_module %>,
10-
# Binding to loopback ipv4 address prevents access from other machines.
9+
config :<%= @app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do %>
10+
# Bind to 0.0.0.0 to expose the server to the docker host machine.
11+
# This makes make the service accessible from any network interface.
12+
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
13+
http: [ip: {0, 0, 0, 0}, port: 4000],<% else %># Binding to loopback ipv4 address prevents access from other machines.
1114
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
12-
http: [ip: {127, 0, 0, 1}, port: 4000],
15+
http: [ip: {127, 0, 0, 1}, port: 4000],<% end %>
1316
check_origin: false,
1417
code_reloader: true,
1518
debug_errors: true,

installer/templates/phx_umbrella/apps/app_name_web/config/dev.exs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import Config
66
# The watchers configuration can be used to run external
77
# watchers to your application. For example, we can use it
88
# to bundle .js and .css sources.
9-
config :<%= @web_app_name %>, <%= @endpoint_module %>,
10-
# Binding to loopback ipv4 address prevents access from other machines.
9+
config :<%= @web_app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do %>
10+
# Bind to 0.0.0.0 to expose the server to the docker host machine.
11+
# This makes make the service accessible from any network interface.
12+
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
13+
http: [ip: {0, 0, 0, 0}, port: 4000],<% else %># Binding to loopback ipv4 address prevents access from other machines.
1114
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
12-
http: [ip: {127, 0, 0, 1}, port: 4000],
15+
http: [ip: {127, 0, 0, 1}, port: 4000],<% end %>
1316
check_origin: false,
1417
code_reloader: true,
1518
debug_errors: true,

installer/test/phx_new_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ defmodule Mix.Tasks.Phx.NewTest do
149149
assert_file("phx_blog/config/dev.exs", fn file ->
150150
assert file =~ "esbuild: {Esbuild,"
151151
assert file =~ "lib/phx_blog_web/(controllers|live|components)/.*(ex|heex)"
152+
assert file =~ "http: [ip: {127, 0, 0, 1}, port: 4000]"
152153
end)
153154

154155
# tailwind
@@ -819,4 +820,13 @@ defmodule Mix.Tasks.Phx.NewTest do
819820
Mix.Tasks.Phx.New.run(["table"])
820821
end
821822
end
823+
824+
test "new from inside docker machine (simulated)" do
825+
in_tmp("new without defaults", fn ->
826+
Mix.Tasks.Phx.New.run([@app_name, "--inside-docker-env"])
827+
assert_file("phx_blog/config/dev.exs", fn file ->
828+
assert file =~ "http: [ip: {0, 0, 0, 0}, port: 4000]"
829+
end)
830+
end)
831+
end
822832
end

0 commit comments

Comments
 (0)