From 7efca4317b568c408a10b71799f9b8261ac5e8e6 Mon Sep 17 00:00:00 2001
From: Ashlynn Anderson <pea@pea.sh>
Date: Wed, 31 Jul 2019 19:35:14 -0400
Subject: [PATCH 1/5] Basic working Dockerfile

No fancy script or minit automatic migration, etc, but if you start
the docker image and go in and manually do everything, it works.
---
 Dockerfile | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Dockerfile

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 000000000..667c01b39
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,32 @@
+FROM rinpatch/elixir:1.9.0-rc.0-alpine as build
+
+COPY . .
+
+ENV MIX_ENV prod
+
+RUN apk add git gcc g++ musl-dev make &&\
+	echo "import Mix.Config" > config/prod.secret.exs &&\
+	mix local.hex --force &&\
+	mix local.rebar --force
+
+RUN mix deps.get --only prod &&\
+	mkdir release &&\
+	mix release --path release
+
+FROM alpine:latest
+
+RUN echo "http://nl.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories &&\
+	apk update &&\
+	apk add ncurses postgresql-client
+
+RUN adduser --system --shell /bin/false --home /opt/pleroma pleroma &&\
+	mkdir -p /var/lib/pleroma/uploads &&\
+	chown -R pleroma /var/lib/pleroma &&\
+	mkdir -p /var/lib/pleroma/static &&\
+	chown -R pleroma /var/lib/pleroma &&\
+	mkdir -p /etc/pleroma &&\
+	chown -R pleroma /etc/pleroma
+
+USER pleroma
+
+COPY --from=build --chown=pleroma:0 /release/ /opt/pleroma/

From 4a418698db71016447f2f246f7c5579b3dc0b08c Mon Sep 17 00:00:00 2001
From: Ashlynn Anderson <pea@pea.sh>
Date: Fri, 2 Aug 2019 22:28:48 -0400
Subject: [PATCH 2/5] Create docker.exs and docker-entrypoint + round out
 Dockerfile

At this point, the implementation is completely working and has been
tested running live and federating with other instances.
---
 Dockerfile           | 23 ++++++++++-----
 config/docker.exs    | 67 ++++++++++++++++++++++++++++++++++++++++++++
 docker-entrypoint.sh | 14 +++++++++
 3 files changed, 97 insertions(+), 7 deletions(-)
 create mode 100644 config/docker.exs
 create mode 100755 docker-entrypoint.sh

diff --git a/Dockerfile b/Dockerfile
index 667c01b39..2f438c952 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -2,7 +2,7 @@ FROM rinpatch/elixir:1.9.0-rc.0-alpine as build
 
 COPY . .
 
-ENV MIX_ENV prod
+ENV MIX_ENV=prod
 
 RUN apk add git gcc g++ musl-dev make &&\
 	echo "import Mix.Config" > config/prod.secret.exs &&\
@@ -15,18 +15,27 @@ RUN mix deps.get --only prod &&\
 
 FROM alpine:latest
 
+ARG HOME=/opt/pleroma
+ARG DATA=/var/lib/pleroma
+
 RUN echo "http://nl.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories &&\
 	apk update &&\
 	apk add ncurses postgresql-client
 
-RUN adduser --system --shell /bin/false --home /opt/pleroma pleroma &&\
-	mkdir -p /var/lib/pleroma/uploads &&\
-	chown -R pleroma /var/lib/pleroma &&\
-	mkdir -p /var/lib/pleroma/static &&\
-	chown -R pleroma /var/lib/pleroma &&\
+RUN adduser --system --shell /bin/false --home ${HOME} pleroma &&\
+	mkdir -p ${DATA}/uploads &&\
+	mkdir -p ${DATA}/static &&\
+	chown -R pleroma ${DATA} &&\
 	mkdir -p /etc/pleroma &&\
 	chown -R pleroma /etc/pleroma
 
 USER pleroma
 
-COPY --from=build --chown=pleroma:0 /release/ /opt/pleroma/
+COPY --from=build --chown=pleroma:0 /release ${HOME}
+
+COPY ./config/docker.exs /etc/pleroma/config.exs
+COPY ./docker-entrypoint.sh ${HOME}
+
+EXPOSE 4000
+
+ENTRYPOINT ["/opt/pleroma/docker-entrypoint.sh"]
diff --git a/config/docker.exs b/config/docker.exs
new file mode 100644
index 000000000..c07f0b753
--- /dev/null
+++ b/config/docker.exs
@@ -0,0 +1,67 @@
+import Config
+
+config :pleroma, Pleroma.Web.Endpoint,
+   url: [host: System.get_env("DOMAIN", "localhost"), scheme: "https", port: 443],
+   http: [ip: {0, 0, 0, 0}, port: 4000]
+
+config :pleroma, :instance,
+  name: System.get_env("INSTANCE_NAME", "Pleroma"),
+  email: System.get_env("ADMIN_EMAIL"),
+  notify_email: System.get_env("NOTIFY_EMAIL"),
+  limit: 5000,
+  registrations_open: false,
+  dynamic_configuration: true
+
+config :pleroma, Pleroma.Repo,
+  adapter: Ecto.Adapters.Postgres,
+  username: System.get_env("DB_USER", "pleroma"),
+  password: System.fetch_env!("DB_PASS"),
+  database: System.get_env("DB_NAME", "pleroma"),
+  hostname: System.get_env("DB_HOST", "db"),
+  pool_size: 10
+
+# Configure web push notifications
+config :web_push_encryption, :vapid_details,
+  subject: "mailto:#{System.get_env("NOTIFY_EMAIL")}"
+
+config :pleroma, :database, rum_enabled: false
+config :pleroma, :instance, static_dir: "/var/lib/pleroma/static"
+config :pleroma, Pleroma.Uploaders.Local, uploads: "/var/lib/pleroma/uploads"
+
+# We can't store the secrets in this file, since this is baked into the docker image
+if not File.exists?("/var/lib/pleroma/secret.exs") do
+  secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
+  signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
+  {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
+
+  secret_file = EEx.eval_string(
+    """
+    import Config
+    
+    config :pleroma, Pleroma.Web.Endpoint,
+      secret_key_base: "<%= secret %>",
+      signing_salt: "<%= signing_salt %>"
+    
+    config :web_push_encryption, :vapid_details,
+      public_key: "<%= web_push_public_key %>",
+      private_key: "<%= web_push_private_key %>"
+    """,
+    secret: secret,
+    signing_salt: signing_salt,
+    web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
+    web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
+  )
+
+  File.write("/var/lib/pleroma/secret.exs", secret_file)
+end
+
+import_config("/var/lib/pleroma/secret.exs")
+
+# For additional user config
+if File.exists?("/var/lib/pleroma/config.exs"),
+  do: import_config("/var/lib/pleroma/config.exs"),
+  else: File.write("/var/lib/pleroma/config.exs", """
+  import Config
+  
+  # For additional configuration outside of environmental variables
+  """)
diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
new file mode 100755
index 000000000..f56f8c50a
--- /dev/null
+++ b/docker-entrypoint.sh
@@ -0,0 +1,14 @@
+#!/bin/ash
+
+set -e
+
+echo "-- Waiting for database..."
+while ! pg_isready -U ${DB_USER:-pleroma} -d postgres://${DB_HOST:-db}:5432/${DB_NAME:-pleroma} -t 1; do
+    sleep 1s
+done
+
+echo "-- Running migrations..."
+$HOME/bin/pleroma_ctl migrate
+
+echo "-- Starting!"
+exec $HOME/bin/pleroma start

From c86db959cb3a3f4a4f79833747d5fa8ecff0d0c7 Mon Sep 17 00:00:00 2001
From: Ashlynn Anderson <pea@pea.sh>
Date: Fri, 2 Aug 2019 22:40:31 -0400
Subject: [PATCH 3/5] Optimize Dockerfile

Just merging RUNs to decrease the number of layers
---
 Dockerfile | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 2f438c952..268ec61dc 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,9 +7,8 @@ ENV MIX_ENV=prod
 RUN apk add git gcc g++ musl-dev make &&\
 	echo "import Mix.Config" > config/prod.secret.exs &&\
 	mix local.hex --force &&\
-	mix local.rebar --force
-
-RUN mix deps.get --only prod &&\
+	mix local.rebar --force &&\
+	mix deps.get --only prod &&\
 	mkdir release &&\
 	mix release --path release
 
@@ -20,9 +19,8 @@ ARG DATA=/var/lib/pleroma
 
 RUN echo "http://nl.alpinelinux.org/alpine/latest-stable/community" >> /etc/apk/repositories &&\
 	apk update &&\
-	apk add ncurses postgresql-client
-
-RUN adduser --system --shell /bin/false --home ${HOME} pleroma &&\
+	apk add ncurses postgresql-client &&\
+	adduser --system --shell /bin/false --home ${HOME} pleroma &&\
 	mkdir -p ${DATA}/uploads &&\
 	mkdir -p ${DATA}/static &&\
 	chown -R pleroma ${DATA} &&\

From 04327721d73733c1052d284adca12b949ce61045 Mon Sep 17 00:00:00 2001
From: Ashlynn Anderson <pea@pea.sh>
Date: Fri, 2 Aug 2019 23:33:47 -0400
Subject: [PATCH 4/5] Add .dockerignore

---
 .dockerignore | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 .dockerignore

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 000000000..c5ef89b86
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,12 @@
+.*
+*.md
+AGPL-3
+CC-BY-SA-4.0
+COPYING
+*file
+elixir_buildpack.config
+docs/
+test/
+
+# Required to get version
+!.git

From 4007717534f9cc880b808b91ba6be5801afb71a0 Mon Sep 17 00:00:00 2001
From: Ashlynn Anderson <pea@pea.sh>
Date: Sat, 3 Aug 2019 13:42:57 -0400
Subject: [PATCH 5/5] Run mix format

---
 config/docker.exs | 53 ++++++++++++++++++++++++-----------------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/config/docker.exs b/config/docker.exs
index c07f0b753..63ab4cdee 100644
--- a/config/docker.exs
+++ b/config/docker.exs
@@ -1,8 +1,8 @@
 import Config
 
 config :pleroma, Pleroma.Web.Endpoint,
-   url: [host: System.get_env("DOMAIN", "localhost"), scheme: "https", port: 443],
-   http: [ip: {0, 0, 0, 0}, port: 4000]
+  url: [host: System.get_env("DOMAIN", "localhost"), scheme: "https", port: 443],
+  http: [ip: {0, 0, 0, 0}, port: 4000]
 
 config :pleroma, :instance,
   name: System.get_env("INSTANCE_NAME", "Pleroma"),
@@ -21,8 +21,7 @@
   pool_size: 10
 
 # Configure web push notifications
-config :web_push_encryption, :vapid_details,
-  subject: "mailto:#{System.get_env("NOTIFY_EMAIL")}"
+config :web_push_encryption, :vapid_details, subject: "mailto:#{System.get_env("NOTIFY_EMAIL")}"
 
 config :pleroma, :database, rum_enabled: false
 config :pleroma, :instance, static_dir: "/var/lib/pleroma/static"
@@ -34,23 +33,24 @@
   signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
   {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
 
-  secret_file = EEx.eval_string(
-    """
-    import Config
-    
-    config :pleroma, Pleroma.Web.Endpoint,
-      secret_key_base: "<%= secret %>",
-      signing_salt: "<%= signing_salt %>"
-    
-    config :web_push_encryption, :vapid_details,
-      public_key: "<%= web_push_public_key %>",
-      private_key: "<%= web_push_private_key %>"
-    """,
-    secret: secret,
-    signing_salt: signing_salt,
-    web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
-    web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
-  )
+  secret_file =
+    EEx.eval_string(
+      """
+      import Config
+
+      config :pleroma, Pleroma.Web.Endpoint,
+        secret_key_base: "<%= secret %>",
+        signing_salt: "<%= signing_salt %>"
+
+      config :web_push_encryption, :vapid_details,
+        public_key: "<%= web_push_public_key %>",
+        private_key: "<%= web_push_private_key %>"
+      """,
+      secret: secret,
+      signing_salt: signing_salt,
+      web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
+      web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
+    )
 
   File.write("/var/lib/pleroma/secret.exs", secret_file)
 end
@@ -60,8 +60,9 @@
 # For additional user config
 if File.exists?("/var/lib/pleroma/config.exs"),
   do: import_config("/var/lib/pleroma/config.exs"),
-  else: File.write("/var/lib/pleroma/config.exs", """
-  import Config
-  
-  # For additional configuration outside of environmental variables
-  """)
+  else:
+    File.write("/var/lib/pleroma/config.exs", """
+    import Config
+
+    # For additional configuration outside of environmental variables
+    """)