我正在尝试在 Docker 上部署带有 Postgres 数据库的 Phoenix 应用程序。我还打算使用 Nginx 作为我的反向代理(但这不是主要议程)。所以我一直在使用 Phoenix Releases,但不知何故我无法运行 mix phx.server 来启动系统。这是我的 Dockerfile :
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian
# instead of Alpine to avoid DNS resolution issues in production.
#
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
# https://hub.docker.com/_/ubuntu?tab=tags
#
# This file is based on these images:
#
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20240701-slim - for the release image
# - https://pkgs.org/ - resource for finding needed packages
# - Ex: hexpm/elixir:1.17.2-erlang-27.0-debian-bullseye-20240701-slim
#
ARG ELIXIR_VERSION=1.17.2
ARG OTP_VERSION=27.0
ARG DEBIAN_VERSION=bullseye-20240701-slim
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
FROM ${BUILDER_IMAGE} as builder
# install build dependencies
RUN apt-get update -y && apt-get install -y build-essential git \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# prepare build dir
WORKDIR /app
# install hex + rebar
RUN mix local.hex --force && \
mix local.rebar --force
# set build ENV
ENV MIX_ENV="prod"
# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
RUN mkdir config
# copy compile-time config files before we compile dependencies
# to ensure any relevant config change will trigger the dependencies
# to be re-compiled.
COPY config/config.exs config/${MIX_ENV}.exs config/
RUN mix deps.compile
COPY priv priv
COPY lib lib
COPY run.sh ./
COPY assets assets
# compile assets
RUN mix assets.deploy
# Compile the release
RUN mix compile
# Changes to config/runtime.exs don't require recompiling the code
COPY config/runtime.exs config/
COPY rel rel
RUN mix release
# start a new build stage so that the final image will only contain
# the compiled release and other runtime necessities
FROM ${RUNNER_IMAGE}
RUN apt-get update -y && \
apt-get install -y libstdc++6 openssl libncurses5 locales ca-certificates \
&& apt-get clean && rm -f /var/lib/apt/lists/*_*
# Set the locale
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
WORKDIR "/app"
RUN chown nobody /app
# set runner ENV
ENV MIX_ENV="prod"
# Only copy the final release from the build stage
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/my_app ./
USER nobody
EXPOSE 4000
# If using an environment that doesn't automatically reap zombie processes, it is
# advised to add an init process such as tini via `apt-get install`
# above and adding an entrypoint. See https://github.com/krallin/tini for details
# ENTRYPOINT ["/tini", "--"]
CMD ["/app/bin/server"]
我想要一种可以使用 mix phx.server 运行服务器的方法。目前我所做的是这不起作用,因为有错误说
mix not found
我编写了这个 run.sh 文件并使其可执行:
#!/bin/sh
# Adapted from Alex Kleissner's post, Running a Phoenix 1.3 project with docker-compose
# https://medium.com/@hex337/running-a-phoenix-1-3-project-with-docker-compose-d82ab55e43cf
set -e
# Ensure the app's dependencies are installed
mix deps.get
# Prepare Dialyzer if the project has Dialyxer set up
if mix help dialyzer >/dev/null 2>&1
then
echo "\nFound Dialyxer: Setting up PLT..."
mix do deps.compile, dialyzer --plt
else
echo "\nNo Dialyxer config: Skipping setup..."
fi
# Install JS libraries
echo "\nInstalling JS..."
cd assets && npm install
cd ..
# Wait for Postgres to become available.
until psql -h db -U "postgres" -c '\q' 2>/dev/null; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
echo "\nPostgres is available: continuing with database setup..."
#Analysis style code
# Prepare Credo if the project has Credo start code analyze
if mix help credo >/dev/null 2>&1
then
echo "\nFound Credo: analyzing..."
mix credo || true
else
echo "\nNo Credo config: Skipping code analyze..."
fi
# Potentially Set up the database
mix ecto.create
mix ecto.migrate
echo "\nTesting the installation..."
# "Prove" that install was successful by running the tests
mix test
echo "\n Launching Phoenix web server..."
# Start the phoenix web server
mix phx.server
然后将其附加到 docker-compose.yml
version: "3.2"
services:
db:
image: postgres:16.0-alpine
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_EXTENSIONS: citext ,uuid-ossp
ports:
- 5432:5432
web:
build: .
volumes:
- type: bind
source: .
target: /app
ports:
- "4000:4000"
environment:
# Modify your config files (dev.exs and test.exs) so that the password and hostname can be overridden
# when environment variables are set:
# password: System.get_env("DB_PASS", "postgres"),
# hostname: System.get_env("DB_HOST", "localhost"),
- DB_PASS=
- DB_HOST=db
depends_on:
- db
command:
- ./run.sh
请指出您认为我做错的地方,以便我立即更改。
或者提供 Docker 化此应用程序的替代方法 谢谢你。
您正在使用 Phoenix 的标准 Docker 设置。 总的来说,这是一个相当合理的 Docker 设置。 通读整个页面是很有帮助的背景知识。
这里一个不明显的一点是容器的默认命令只运行生成的发布脚本,但它有几个子命令。 在实践中,您可能需要
/app/server/bin start
子命令。
mix phx.server
实际上所做的就是在启用 Phoenix Web 服务器的情况下启动您的应用程序。 例如,您可能不希望它打开 mix test
。 正如文档所述,如果您的 config.exs
包含 config Phoenix.Endpoint, server: true
,那么正常启动主应用程序也会启动服务器。
Phoenix 新应用程序样板 包含此片段(其中
app_name
和 endpoint_module
来自 mix phx.new
的参数):
# If you use `mix release`, you need to explicitly enable the server
# by passing the PHX_SERVER=true when you start it:
#
# PHX_SERVER=true bin/<%= @app_name %> start
#
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
# script that automatically sets the env var above.
if System.get_env("PHX_SERVER") do
config :<%= @app_name %>, <%= @endpoint_module %>, server: true
end
如果你的
config/runtime.exs
有这个片段,那么就足够了
PHX_SERVER=true
,以及server
脚本 start
。...
ENV PHX_SERVER=true
CMD ["/app/bin/server", "start"]
(这个用于个人项目的 Dockerfile 涉及更多一些 – 特别是,我已经拆分了 Web 服务器、数据存储和工作容器 – 但对于 Web 容器,我同样设置
PHX_SERVER=true
并运行start
命令。)