Dockerfile 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. FROM ubuntu:20.04 as build-dep
  2. # Use bash for the shell
  3. SHELL ["bash", "-c"]
  4. # Install Node v12 (LTS)
  5. ENV NODE_VER="12.20.0"
  6. RUN ARCH= && \
  7. dpkgArch="$(dpkg --print-architecture)" && \
  8. case "${dpkgArch##*-}" in \
  9. amd64) ARCH='x64';; \
  10. ppc64el) ARCH='ppc64le';; \
  11. s390x) ARCH='s390x';; \
  12. arm64) ARCH='arm64';; \
  13. armhf) ARCH='armv7l';; \
  14. i386) ARCH='x86';; \
  15. *) echo "unsupported architecture"; exit 1 ;; \
  16. esac && \
  17. echo "Etc/UTC" > /etc/localtime && \
  18. apt update && \
  19. apt -y install wget python && \
  20. cd ~ && \
  21. wget https://nodejs.org/download/release/v$NODE_VER/node-v$NODE_VER-linux-$ARCH.tar.gz && \
  22. tar xf node-v$NODE_VER-linux-$ARCH.tar.gz && \
  23. rm node-v$NODE_VER-linux-$ARCH.tar.gz && \
  24. mv node-v$NODE_VER-linux-$ARCH /opt/node
  25. # Install jemalloc
  26. ENV JE_VER="5.2.1"
  27. RUN apt update && \
  28. apt -y install make autoconf gcc g++ && \
  29. cd ~ && \
  30. wget https://github.com/jemalloc/jemalloc/archive/$JE_VER.tar.gz && \
  31. tar xf $JE_VER.tar.gz && \
  32. cd jemalloc-$JE_VER && \
  33. ./autogen.sh && \
  34. ./configure --prefix=/opt/jemalloc && \
  35. make -j$(nproc) > /dev/null && \
  36. make install_bin install_include install_lib && \
  37. cd .. && rm -rf jemalloc-$JE_VER $JE_VER.tar.gz
  38. # Install Ruby
  39. ENV RUBY_VER="2.7.2"
  40. ENV CPPFLAGS="-I/opt/jemalloc/include"
  41. ENV LDFLAGS="-L/opt/jemalloc/lib/"
  42. RUN apt update && \
  43. apt -y install build-essential \
  44. bison libyaml-dev libgdbm-dev libreadline-dev \
  45. libncurses5-dev libffi-dev zlib1g-dev libssl-dev && \
  46. cd ~ && \
  47. wget https://cache.ruby-lang.org/pub/ruby/${RUBY_VER%.*}/ruby-$RUBY_VER.tar.gz && \
  48. tar xf ruby-$RUBY_VER.tar.gz && \
  49. cd ruby-$RUBY_VER && \
  50. ./configure --prefix=/opt/ruby \
  51. --with-jemalloc \
  52. --with-shared \
  53. --disable-install-doc && \
  54. ln -s /opt/jemalloc/lib/* /usr/lib/ && \
  55. make -j$(nproc) > /dev/null && \
  56. make install && \
  57. cd .. && rm -rf ruby-$RUBY_VER.tar.gz ruby-$RUBY_VER
  58. ENV PATH="${PATH}:/opt/ruby/bin:/opt/node/bin"
  59. RUN npm install -g yarn && \
  60. gem install bundler && \
  61. apt update && \
  62. apt -y install git libicu-dev libidn11-dev \
  63. libpq-dev libprotobuf-dev protobuf-compiler
  64. COPY Gemfile* package.json yarn.lock /opt/mastodon/
  65. RUN cd /opt/mastodon && \
  66. bundle config set deployment 'true' && \
  67. bundle config set without 'development test' && \
  68. bundle install -j$(nproc) && \
  69. yarn install --pure-lockfile
  70. FROM ubuntu:20.04
  71. # Copy over all the langs needed for runtime
  72. COPY --from=build-dep /opt/node /opt/node
  73. COPY --from=build-dep /opt/ruby /opt/ruby
  74. COPY --from=build-dep /opt/jemalloc /opt/jemalloc
  75. # Add more PATHs to the PATH
  76. ENV PATH="${PATH}:/opt/ruby/bin:/opt/node/bin:/opt/mastodon/bin"
  77. # Create the mastodon user
  78. ARG UID=991
  79. ARG GID=991
  80. RUN apt update && \
  81. echo "Etc/UTC" > /etc/localtime && \
  82. ln -s /opt/jemalloc/lib/* /usr/lib/ && \
  83. apt install -y whois wget && \
  84. addgroup --gid $GID mastodon && \
  85. useradd -m -u $UID -g $GID -d /opt/mastodon mastodon && \
  86. echo "mastodon:`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 | mkpasswd -s -m sha-256`" | chpasswd
  87. # Install mastodon runtime deps
  88. RUN apt -y --no-install-recommends install \
  89. libssl1.1 libpq5 imagemagick ffmpeg \
  90. libicu66 libprotobuf17 libidn11 libyaml-0-2 \
  91. file ca-certificates tzdata libreadline8 && \
  92. apt -y install gcc && \
  93. ln -s /opt/mastodon /mastodon && \
  94. gem install bundler && \
  95. rm -rf /var/cache && \
  96. rm -rf /var/lib/apt/lists/*
  97. # Add tini
  98. ENV TINI_VERSION="0.19.0"
  99. RUN dpkgArch="$(dpkg --print-architecture)" && \
  100. ARCH=$dpkgArch && \
  101. wget https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-$ARCH \
  102. https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini-$ARCH.sha256sum && \
  103. cat tini-$ARCH.sha256sum | sha256sum -c - && \
  104. mv tini-$ARCH /tini && rm tini-$ARCH.sha256sum && \
  105. chmod +x /tini
  106. # Copy over mastodon source, and dependencies from building, and set permissions
  107. COPY --chown=mastodon:mastodon . /opt/mastodon
  108. COPY --from=build-dep --chown=mastodon:mastodon /opt/mastodon /opt/mastodon
  109. # Run mastodon services in prod mode
  110. ENV RAILS_ENV="production"
  111. ENV NODE_ENV="production"
  112. # Tell rails to serve static files
  113. ENV RAILS_SERVE_STATIC_FILES="true"
  114. ENV BIND="0.0.0.0"
  115. # Set the run user
  116. USER mastodon
  117. # Precompile assets
  118. RUN cd ~ && \
  119. OTP_SECRET=precompile_placeholder SECRET_KEY_BASE=precompile_placeholder rails assets:precompile && \
  120. yarn cache clean
  121. # Set the work dir and the container entry point
  122. WORKDIR /opt/mastodon
  123. ENTRYPOINT ["/tini", "--"]
  124. EXPOSE 3000 4000