Dockerfile and scripting improvements

This commit is contained in:
sn0w 2019-08-25 23:29:34 +02:00
parent b44d428493
commit b768471a7d
7 changed files with 116 additions and 37 deletions

View File

@ -8,3 +8,19 @@ DOCKER_DATADIR=./data
# Pleroma's mix environment. # Pleroma's mix environment.
# You should leave this at prod unless you know what you're doing. # You should leave this at prod unless you know what you're doing.
MIX_ENV=prod MIX_ENV=prod
# The uid/gid used by pleroma.
# You should probably set this to the same
# uid/guid that cloned the pleroma-docker repo.
# This way modding pleroma becomes a lot easier.
DOCKER_UID=1000
DOCKER_GID=1000
# The git repo where pleroma's sources are located.
# This will be used at build-time and to resolve PLEROMA_VERSION via "git ls-remote".
# The latter involves one connection per "pleroma.sh build" execution, even if a rebuild does not occur.
#
# You might want to change this if you're working on a fork,
# or if you do not trust the admins of pleroma's Gitlab instance.
#
PLEROMA_GIT_REPO=https://git.pleroma.social/pleroma/pleroma.git

8
.gitignore vendored
View File

@ -1,10 +1,10 @@
data data/
.env cache/
config.yml
custom.d/ custom.d/
!custom.d/.gitkeep !custom.d/.gitkeep
docker-compose.yml
config.exs config.exs
secret.exs
.env
# Created by https://www.gitignore.io/api/osx,linux,windows # Created by https://www.gitignore.io/api/osx,linux,windows

View File

@ -10,7 +10,8 @@ RUN \
# Set up environment # Set up environment
ENV LC_ALL=C.UTF-8 ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8 ENV LANG=C.UTF-8
ENV MIX_ENV=prod ARG MIX_ENV
ENV MIX_ENV=$MIX_ENV
# Prepare mounts # Prepare mounts
VOLUME /custom.d VOLUME /custom.d
@ -22,12 +23,14 @@ EXPOSE 4000
RUN \ RUN \
apk add --no-cache --virtual .tools \ apk add --no-cache --virtual .tools \
git curl rsync postgresql-client \ git curl rsync postgresql-client \
\ && \
&& apk add --no-cache --virtual .sdk \ apk add --no-cache --virtual .sdk \
build-base \ build-base \
\ && \
&& apk add --no-cache --virtual .runtime \ apk add --no-cache --virtual .runtime \
imagemagick erlang erlang-runtime-tools erlang-xmerl erlang-ssl erlang-eldap elixir imagemagick \
elixir erlang erlang-runtime-tools \
erlang-xmerl erlang-ssl erlang-ssh erlang-eldap
# Add entrypoint # Add entrypoint
COPY ./entrypoint.sh / COPY ./entrypoint.sh /
@ -35,21 +38,25 @@ RUN chmod a+x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"] ENTRYPOINT ["/entrypoint.sh"]
# Limit permissions # Limit permissions
ARG DOCKER_UID=1000 ARG DOCKER_UID
ARG DOCKER_GID=1000 ARG DOCKER_GID
ARG PLEROMA_UPLOADS_PATH=/uploads
RUN \ RUN \
addgroup -g ${DOCKER_GID} pleroma \ echo "#> Pleroma user will be ${DOCKER_UID}:${DOCKER_GID}" 1>&2 && \
&& adduser -S -s /bin/ash -G pleroma -u ${DOCKER_UID} pleroma \ addgroup -g ${DOCKER_GID} pleroma && \
&& mkdir -p /custom.d $PLEROMA_UPLOADS_PATH \ adduser -S -s /bin/ash -G pleroma -u ${DOCKER_UID} pleroma && \
&& chown -R pleroma:pleroma /custom.d $PLEROMA_UPLOADS_PATH mkdir -p /custom.d /uploads && \
chown -R pleroma:pleroma /custom.d /uploads
USER pleroma USER pleroma
WORKDIR /home/pleroma WORKDIR /home/pleroma
# Get pleroma sources # Get pleroma sources
RUN git clone --progress https://git.pleroma.social/pleroma/pleroma.git ./pleroma ARG PLEROMA_GIT_REPO
RUN \
echo "#> Getting pleroma sources from $PLEROMA_GIT_REPO..." 1>&2 && \
git clone --progress $PLEROMA_GIT_REPO ./pleroma
WORKDIR /home/pleroma/pleroma WORKDIR /home/pleroma/pleroma
# Bust the build cache (if needed) # Bust the build cache (if needed)
@ -63,12 +70,12 @@ ENV __CACHE_TAG $__CACHE_TAG
# Fetch changes, checkout # Fetch changes, checkout
ARG PLEROMA_VERSION ARG PLEROMA_VERSION
RUN \ RUN \
git fetch --all \ git fetch --all && \
&& git checkout $PLEROMA_VERSION \ git checkout $PLEROMA_VERSION && \
&& git pull --rebase --autostash git pull --rebase --autostash
# Precompile # Precompile
RUN \ RUN \
cp ./config/dev.exs ./config/prod.secret.exs \ cp ./config/dev.exs ./config/prod.secret.exs && \
&& BUILDTIME=1 /entrypoint.sh \ BUILDTIME=1 /entrypoint.sh && \
&& rm ./config/prod.secret.exs rm ./config/prod.secret.exs

View File

@ -11,8 +11,10 @@ config :pleroma, Pleroma.Repo,
hostname: "db", hostname: "db",
pool_size: 10 pool_size: 10
# Listening to 0.0.0.0 is required in a container # Listening to 0.0.0.0 is required in a container since the IP is not known in advance
# Do not change this # You should not change the options below this.
# Instead, go change the mapping to your host ports in "docker-compose.yml"
config :pleroma, Pleroma.Web.Endpoint, config :pleroma, Pleroma.Web.Endpoint,
http: [ http: [
ip: {0, 0, 0, 0}, ip: {0, 0, 0, 0},
@ -23,6 +25,9 @@ config :pleroma, :gopher,
ip: {0, 0, 0, 0}, ip: {0, 0, 0, 0},
port: 9999 port: 9999
config :esshd,
port: 2222
# vvv Your awesome config options go here vvv # vvv Your awesome config options go here vvv
### ###

41
docker-compose.yml Normal file
View File

@ -0,0 +1,41 @@
version: "3.7"
services:
db:
image: postgres:10-alpine
init: true
restart: unless-stopped
environment:
POSTGRES_DB: pleroma
POSTGRES_USER: pleroma
POSTGRES_PASSWORD: pleroma
volumes:
- $DOCKER_DATADIR/db:/var/lib/postgresql/data
- ./initdb.sql:/docker-entrypoint-initdb.d/pleroma.sql
server:
build: .
init: true
restart: unless-stopped
links:
- db
ports: [
# Uncomment/Change port mappings below as needed.
# The left side is your host machine, the right one is the pleroma container.
# You can prefix the left side with an ip.
# Webserver (for reverse-proxies outside of docker)
# If you use a dockerized proxy (see README), you can leave this commented
# and use a container link instead.
# "127.0.0.1:4000:4000",
# SSH support
# "2222:2222",
# Gopher support
# "9999:9999",
]
volumes:
- ./custom.d:/custom.d:ro
- ./config.exs:/home/pleroma/pleroma/config/prod.secret.exs:ro
- $DOCKER_DATADIR/uploads:/uploads

View File

@ -3,34 +3,38 @@
set -e set -e
log() {
echo -e "\n#> $@\n" 1>&2
}
if [ -n "$BUILDTIME" ]; then if [ -n "$BUILDTIME" ]; then
echo "#> Getting rebar..." log "Getting rebar..."
mix local.rebar --force mix local.rebar --force
echo "#> Getting hex..." log "Getting hex..."
mix local.hex --force mix local.hex --force
echo "#> Getting dependencies..." log "Getting dependencies..."
mix deps.get mix deps.get
echo "#> Precompiling..." log "Precompiling..."
mix compile mix compile
exit 0 exit 0
fi fi
echo "#> Applying customizations and patches.." log "Syncing changes and patches..."
rsync -av /custom.d/ /home/pleroma/pleroma/ rsync -av /custom.d/ /home/pleroma/pleroma/
echo "#> Recompiling..." log "Recompiling..."
mix compile mix compile
echo "#> Waiting until database is ready..." log "Waiting for postgres..."
while ! pg_isready -U pleroma -d postgres://db:5432/pleroma -t 1; do while ! pg_isready -U pleroma -d postgres://db:5432/pleroma -t 1; do
sleep 1s sleep 1s
done done
echo "#> Upgrading database..." log "Migrating database..."
mix ecto.migrate mix ecto.migrate
echo "#> Liftoff!" log "Liftoff o/"
exec mix phx.server exec mix phx.server

View File

@ -77,12 +77,14 @@ request_file_content() { # $1: source
fi fi
} }
builds_args=""
load_env() { load_env() {
while read -r line; do while read -r line; do
if [[ "$line" == \#* ]] || [[ -z "$line" ]]; then if [[ "$line" == \#* ]] || [[ -z "$line" ]]; then
continue; continue;
fi fi
builds_args="${builds_args} --build-arg ${line?}"
export "${line?}" export "${line?}"
done < .env done < .env
} }
@ -98,7 +100,7 @@ action__build() {
if [[ -z "$cacheTag" ]] && has_command git && has_command grep && has_command awk; then if [[ -z "$cacheTag" ]] && has_command git && has_command grep && has_command awk; then
set +o pipefail set +o pipefail
local resolvedHash local resolvedHash
resolvedHash="$(git ls-remote $GITLAB_URI/$ENDPOINT_REPO | grep "/$PLEROMA_VERSION" | awk '{ print $1 }')" resolvedHash="$(git ls-remote $PLEROMA_GIT_REPO | grep "/$PLEROMA_VERSION" | awk '{ print $1 }')"
set -o pipefail set -o pipefail
if [[ -n "$resolvedHash" ]]; then if [[ -n "$resolvedHash" ]]; then
@ -169,7 +171,11 @@ action__build() {
echo -e "#> (Re-)Building pleroma @$PLEROMA_VERSION with cache tag \`${cacheTag}\`...\n" echo -e "#> (Re-)Building pleroma @$PLEROMA_VERSION with cache tag \`${cacheTag}\`...\n"
sleep 1 sleep 1
docker_compose build --build-arg __VIA_SCRIPT=1 --build-arg __CACHE_TAG="$cacheTag" --build-arg PLEROMA_VERSION="$PLEROMA_VERSION" server docker_compose build \
$builds_args \
--build-arg __VIA_SCRIPT=1 \
--build-arg __CACHE_TAG="$cacheTag" \
server
} }
action__enter() { action__enter() {