Skip to content

Commit

Permalink
Upstream --inside-docker-env
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismccord committed Feb 4, 2025
1 parent 77ce927 commit 2e4948a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:

env:
MIX_ENV: test
PHX_CI: true

strategy:
matrix:
Expand Down
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 @@ -140,7 +140,8 @@ defmodule Mix.Tasks.Phx.New do
install: :boolean,
prefix: :string,
mailer: :boolean,
adapter: :string
adapter: :string,
inside_docker_env: :boolean
]

@reserved_app_names ~w(server table)
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 @@ -181,6 +181,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("PHX_CI") 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 @@ -239,7 +250,8 @@ defmodule Phx.New.Generator do
web_adapter_docs: web_adapter_docs,
generators: nil_if_empty(project.generators ++ adapter_generators(adapter_config)),
namespaced?: namespaced?(project),
dev: dev
dev: dev,
inside_docker_env?: inside_docker_env?
]

%{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 @@ -819,4 +820,13 @@ defmodule Mix.Tasks.Phx.NewTest do
Mix.Tasks.Phx.New.run(["table"])
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 2e4948a

Please sign in to comment.