Skip to content

Commit

Permalink
Expose phx.new dev.exs to host if we're in a docker env
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Jan 31, 2025
1 parent 1585350 commit 9f309a6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 2 additions & 1 deletion installer/lib/mix/tasks/phx.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule Mix.Tasks.Phx.New do
* `--no-html` - do not generate HTML views
* `--no-live` - comment out LiveView socket setup in your Endpoint
* `--no-live` - comment out LiveView socket setup in your Endpoint
and assets/js/app.js. Automatically disabled if --no-html is given
* `--no-mailer` - do not generate Swoosh mailer files
Expand Down Expand Up @@ -142,6 +142,7 @@ defmodule Mix.Tasks.Phx.New do
mailer: :boolean,
adapter: :string,
from_elixir_install: :boolean,
inside_docker_env: :boolean
]

@impl true
Expand Down
14 changes: 13 additions & 1 deletion installer/lib/phx_new/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ defmodule Phx.New.Generator do
phoenix_path = phoenix_path(project, dev, false)
phoenix_path_umbrella_root = phoenix_path(project, dev, true)

# detect if we're inside a docker env, but if we're in github actions,
# we want to treat it like regular env for end-user testing purposes
inside_docker_env? =
Keyword.get_lazy(opts, :inside_docker_env, fn ->
if System.get_env("GITHUB_ACTIONS") do
false
else
File.exists?("/.dockerenv")
end
end)

# We lowercase the database name because according to the
# SQL spec, they are case insensitive unless quoted, which
# means creating a database like FoO is the same as foo in
Expand Down Expand Up @@ -243,7 +254,8 @@ defmodule Phx.New.Generator do
dev: dev,
from_elixir_install: from_elixir_install,
elixir_install_otp_bin_path: from_elixir_install && elixir_install_otp_bin_path(),
elixir_install_bin_path: from_elixir_install && elixir_install_bin_path()
elixir_install_bin_path: from_elixir_install && elixir_install_bin_path(),
inside_docker_env?: inside_docker_env?
]

%Project{project | binding: binding}
Expand Down
9 changes: 6 additions & 3 deletions installer/templates/phx_single/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import Config
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
config :<%= @app_name %>, <%= @endpoint_module %>,
# Binding to loopback ipv4 address prevents access from other machines.
config :<%= @app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do %>
# Bind to 0.0.0.0 to expose the server to the docker host machine.
# This makes make the service accessible from any network interface.
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
http: [ip: {0, 0, 0, 0}, port: 4000],<% else %># Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {127, 0, 0, 1}, port: 4000],
http: [ip: {127, 0, 0, 1}, port: 4000],<% end %>
check_origin: false,
code_reloader: true,
debug_errors: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import Config
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
config :<%= @web_app_name %>, <%= @endpoint_module %>,
# Binding to loopback ipv4 address prevents access from other machines.
config :<%= @web_app_name %>, <%= @endpoint_module %>,<%= if @inside_docker_env? do %>
# Bind to 0.0.0.0 to expose the server to the docker host machine.
# This makes make the service accessible from any network interface.
# Change to `ip: {127, 0, 0, 1}` to allow access only from the server machine.
http: [ip: {0, 0, 0, 0}, port: 4000],<% else %># Binding to loopback ipv4 address prevents access from other machines.
# Change to `ip: {0, 0, 0, 0}` to allow access from other machines.
http: [ip: {127, 0, 0, 1}, port: 4000],
http: [ip: {127, 0, 0, 1}, port: 4000],<% end %>
check_origin: false,
code_reloader: true,
debug_errors: true,
Expand Down
10 changes: 10 additions & 0 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ defmodule Mix.Tasks.Phx.NewTest do
assert_file("phx_blog/config/dev.exs", fn file ->
assert file =~ "esbuild: {Esbuild,"
assert file =~ "lib/phx_blog_web/(controllers|live|components)/.*(ex|heex)"
assert file =~ "http: [ip: {127, 0, 0, 1}, port: 4000]"
end)

# tailwind
Expand Down Expand Up @@ -829,4 +830,13 @@ defmodule Mix.Tasks.Phx.NewTest do
"Creates a new Phoenix project."
end)
end

test "new from inside docker machine (simulated)" do
in_tmp("new without defaults", fn ->
Mix.Tasks.Phx.New.run([@app_name, "--inside-docker-env"])
assert_file("phx_blog/config/dev.exs", fn file ->
assert file =~ "http: [ip: {0, 0, 0, 0}, port: 4000]"
end)
end)
end
end

0 comments on commit 9f309a6

Please sign in to comment.