Skip to content

Commit 50a0210

Browse files
authored
Merge pull request #15 from pantanal-labs/docker
Add Docker
2 parents fbcba3b + b1af545 commit 50a0210

22 files changed

+306
-59
lines changed

.dockerignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.dockerignore
2+
# there are valid reasons to keep the .git, namely so that you can get the
3+
# current commit hash
4+
#.git
5+
.log
6+
tmp
7+
8+
# Mix artifacts
9+
_build
10+
deps
11+
*.ez
12+
releases
13+
14+
# Generate on crash by the VM
15+
erl_crash.dump
16+
17+
# Static artifacts
18+
node_modules

.gitignore

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# The directory Mix will write compiled artifacts to.
2-
/_build/
3-
/.elixir_ls
4-
2+
/_build/
3+
/.elixir_ls
4+
.env
55
# If you run "mix test --cover", coverage assets end up here.
66
/cover/
77

@@ -29,7 +29,7 @@ npm-debug.log
2929
# The directory NPM downloads your dependencies sources to.
3030
/assets/node_modules/
3131

32-
# Since we are building assets from assets/,
33-
# we ignore priv/static. You may want to comment
34-
# this depending on your deployment strategy.
32+
# Since we are building assets from assets/,
33+
# we ignore priv/static. You may want to comment this depending on your deployment
34+
# strategy.
3535
/priv/static/

Caddyfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
brasilemdados.com {
2+
reverse_proxy 127.0.0.1:4000
3+
tls rogervezaro1997@gmail.com
4+
}

Dockerfile

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
# Step 1 - hex dependencies
3+
#
4+
FROM hexpm/elixir:1.13.2-erlang-24.2.1-alpine-3.15.0 AS otp-dependencies
5+
6+
WORKDIR /app
7+
8+
ENV MIX_ENV="prod"
9+
10+
# Install Alpine dependencies
11+
RUN apk add --no-cache git && \
12+
apk --no-cache add build-base
13+
14+
# Install Erlang dependencies
15+
RUN mix local.rebar --force && \
16+
mix local.hex --force
17+
18+
# Install hex dependencies
19+
# COPY mix.* ./
20+
COPY mix.exs mix.lock ./
21+
RUN mix deps.get --only prod
22+
RUN mix deps.compile
23+
24+
# Compile codebase
25+
COPY config config
26+
COPY lib lib
27+
28+
29+
# Compile assets
30+
COPY priv priv
31+
COPY assets assets
32+
33+
RUN mix assets.deploy
34+
35+
RUN mix compile
36+
# Build OTP release
37+
COPY rel rel
38+
RUN mix release
39+
40+
#
41+
# Step 2 - build a lean runtime container
42+
#
43+
FROM alpine:3.15.0
44+
45+
# Install Alpine dependencies
46+
RUN apk update --no-cache && \
47+
apk upgrade --no-cache && \
48+
apk add --no-cache bash openssl libgcc libstdc++ ncurses-libs
49+
50+
WORKDIR /app
51+
52+
RUN chown nobody /app
53+
54+
# Only copy the final release from the build stage
55+
COPY --from=otp-dependencies --chown=nobody:root /app/_build/prod/rel/brasil_em_dados ./
56+
57+
USER nobody
58+
59+
CMD /app/bin/server

config/config.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# is restricted to this project.
66

77
# General application configuration
8-
use Mix.Config
8+
import Config
99

1010
config :brasil_em_dados,
1111
ecto_repos: [BrasilEmDados.Repo]
@@ -36,7 +36,7 @@ config :esbuild,
3636
]
3737

3838
config :tailwind,
39-
version: "3.0.10",
39+
version: "3.0.12",
4040
default: [
4141
args: ~w(
4242
--config=tailwind.config.js

config/dev.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Mix.Config
1+
import Config
22

33
# Configure your database
44
config :brasil_em_dados, BrasilEmDados.Repo,

config/prod.exs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Mix.Config
1+
import Config
22

33
# For production, don't forget to configure the url host
44
# to something meaningful, Phoenix uses this information
@@ -10,7 +10,6 @@ use Mix.Config
1010
# which you should run after static files are built and
1111
# before starting your production server.
1212
config :brasil_em_dados, BrasilEmDadosWeb.Endpoint,
13-
url: [host: "example.com", port: 80],
1413
cache_static_manifest: "priv/static/cache_manifest.json"
1514

1615
# Do not print debug messages in production
@@ -48,8 +47,4 @@ config :logger, level: :info
4847
# config :brasil_em_dados, BrasilEmDadosWeb.Endpoint,
4948
# force_ssl: [hsts: true]
5049
#
51-
# Check `Plug.SSL` for all available options in `force_ssl`.
52-
53-
# Finally import the config/prod.secret.exs which loads secrets
54-
# and configuration from environment variables.
55-
import_config "prod.secret.exs"
50+
# Check `Plug.SSL` for all available options in `force_ssl`.

config/prod.secret.exs

-41
This file was deleted.

config/runtime.exs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import Config
2+
3+
# config/runtime.exs is executed for all environments, including
4+
# during releases. It is executed after compilation and before the
5+
# system starts, so it is typically used to load production configuration
6+
# and secrets from environment variables or elsewhere. Do not define
7+
# any compile-time configuration in here, as it won't be applied.
8+
# The block below contains prod specific runtime configuration.
9+
10+
# Start the phoenix server if environment is set and running in a release
11+
if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do
12+
config :brasil_em_dados, BrasilEmDadosWeb.Endpoint, server: true
13+
end
14+
15+
if config_env() == :prod do
16+
database_url =
17+
System.get_env("DATABASE_URL") ||
18+
raise """
19+
environment variable DATABASE_URL is missing.
20+
For example: ecto://USER:PASS@HOST/DATABASE
21+
"""
22+
23+
maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []
24+
25+
config :brasil_em_dados, BrasilEmDados.Repo,
26+
# ssl: true,
27+
url: database_url,
28+
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
29+
socket_options: maybe_ipv6
30+
# show_sensitive_data_on_connection_error: true # The secret key base is used to sign/encrypt cookies and other secrets.
31+
# A default value is used in config/dev.exs and config/test.exs but you
32+
# want to use a different value for prod and you most likely don't want
33+
# to check this value into version control, so we use an environment
34+
# variable instead.
35+
secret_key_base =
36+
System.get_env("SECRET_KEY_BASE") ||
37+
raise """
38+
environment variable SECRET_KEY_BASE is missing.
39+
You can generate one by calling: mix phx.gen.secret
40+
"""
41+
42+
host = System.get_env("PHX_HOST") || "brasilemdados.com"
43+
port = String.to_integer(System.get_env("PORT") || "4000")
44+
45+
config :brasil_em_dados, BrasilEmDadosWeb.Endpoint,
46+
url: [host: host, port: 443],
47+
http: [
48+
# Enable IPv6 and bind on all interfaces.
49+
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
50+
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
51+
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
52+
ip: {0, 0, 0, 0, 0, 0, 0, 0},
53+
port: port
54+
],
55+
secret_key_base: secret_key_base,
56+
check_origin: false
57+
58+
# ## Using releases
59+
#
60+
# If you are doing OTP releases, you need to instruct Phoenix
61+
# to start each relevant endpoint:
62+
#
63+
# config :brasil_em_dados, BrasilEmDadosWeb.Endpoint, server: true
64+
#
65+
# Then you can assemble a release by calling `mix release`.
66+
# See `mix help release` for more information.
67+
68+
# ## Configuring the mailer
69+
#
70+
# In production you need to configure the mailer to use a different adapter.
71+
# Also, you may need to configure the Swoosh API client of your choice if you
72+
# are not using SMTP. Here is an example of the configuration:
73+
#
74+
# config :brasil_em_dados, BrasilEmDados.Mailer,
75+
# adapter: Swoosh.Adapters.Mailgun,
76+
# api_key: System.get_env("MAILGUN_API_KEY"),
77+
# domain: System.get_env("MAILGUN_DOMAIN")
78+
#
79+
# For this example you need include a HTTP client required by Swoosh API client.
80+
# Swoosh supports Hackney and Finch out of the box:
81+
#
82+
# config :swoosh, :api_client, Swoosh.ApiClient.Hackney
83+
#
84+
# See https://hexdocs.pm/swoosh/Swoosh.html#module-installation for details.
85+
end

config/test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use Mix.Config
1+
import Config
22

33
# Only in tests, remove the complexity from the password hashing algorithm
44
config :bcrypt_elixir, :log_rounds, 1

docker-compose.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
version: '3.8'
2+
services:
3+
web:
4+
build:
5+
context: .
6+
env_file: .env
7+
ports:
8+
- "4000:4000"
9+
environment:
10+
DATABASE_URL: "${DATABASE_URL}"
11+
SECRET_KEY_BASE: "${SECRET_KEY_BASE}"
12+
PORT: "${PORT}"
13+
depends_on:
14+
- db
15+
16+
db:
17+
container_name: brasil-em-dados-postgres
18+
image: postgres:14.1-alpine
19+
env_file: .env
20+
restart: unless-stopped
21+
environment:
22+
POSTGRES_DB: "${POSTGRES_DB}"
23+
POSTGRES_USER: "${POSTGRES_USER}"
24+
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
25+
ports:
26+
- '5432:5432'
27+
volumes:
28+
- db:/var/lib/postgresql/data
29+
30+
caddy:
31+
image: caddy:2.5.0-alpine
32+
restart: unless-stopped
33+
ports:
34+
- "80:80"
35+
- "443:443"
36+
volumes:
37+
- $PWD/Caddyfile:/etc/caddy/Caddyfile
38+
- caddy_data:/data
39+
- caddy_config:/config
40+
volumes:
41+
db:
42+
caddy_data:
43+
caddy_config:

example.env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SECRET_KEY_BASE=
2+
DATABASE_URL=
3+
PORT=

lib/brasil_em_dados/release.ex

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule BrasilEmDados.Release do
2+
@moduledoc """
3+
Used for executing DB release tasks when run in production without Mix
4+
installed.
5+
"""
6+
@app :brasil_em_dados
7+
8+
def migrate do
9+
load_app()
10+
11+
for repo <- repos() do
12+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
13+
end
14+
end
15+
16+
def rollback(repo, version) do
17+
load_app()
18+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
19+
end
20+
21+
defp repos do
22+
Application.fetch_env!(@app, :ecto_repos)
23+
end
24+
25+
defp load_app do
26+
Application.load(@app)
27+
end
28+
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ defmodule BrasilEmDados.MixProject do
5151
{:plug_cowboy, "~> 2.0"},
5252
{:earmark, "~> 1.4.14"},
5353
{:esbuild, "~> 0.4.0", runtime: Mix.env() == :dev},
54-
{:tailwind, "~> 0.1.5", runtime: Mix.env() == :dev},
54+
{:tailwind, "~> 0.1.5"},
5555
{:scrivener_ecto, "~> 2.7.0"}
5656
]
5757
end

rel/env.bat.eex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@echo off
2+
rem Set the release to work across nodes.
3+
rem RELEASE_DISTRIBUTION must be "sname" (local), "name" (distributed) or "none".
4+
rem set RELEASE_DISTRIBUTION=name
5+
rem set RELEASE_NODE=<%= @release.name %>

0 commit comments

Comments
 (0)