Dockerfile 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # This is the Dockerfile for ArchiveBox, it bundles the following dependencies:
  2. # python3, ArchiveBox, curl, wget, git, chromium, youtube-dl, yt-dlp, single-file
  3. # Usage:
  4. # git submodule update --init --recursive
  5. # git pull --recurse-submodules
  6. # docker build . -t archivebox --no-cache
  7. # docker run -v "$PWD/data":/data archivebox init
  8. # docker run -v "$PWD/data":/data archivebox add 'https://example.com'
  9. # docker run -v "$PWD/data":/data -it archivebox manage createsuperuser
  10. # docker run -v "$PWD/data":/data -p 8000:8000 archivebox server
  11. # Multi-arch build:
  12. # docker buildx create --use
  13. # docker buildx build . --platform=linux/amd64,linux/arm64--push -t archivebox/archivebox:latest -t archivebox/archivebox:dev
  14. #
  15. # Read more about [developing Archivebox](https://github.com/ArchiveBox/ArchiveBox#archivebox-development).
  16. FROM python:3.11-slim-bookworm
  17. # Uses Debian 12 w/ faster-updating apt-lists added below: https://packages.debian.org/bookworm-backports/
  18. LABEL name="archivebox" \
  19. maintainer="Nick Sweeting <[email protected]>" \
  20. description="All-in-one self-hosted internet archiving solution" \
  21. homepage="https://github.com/ArchiveBox/ArchiveBox" \
  22. documentation="https://github.com/ArchiveBox/ArchiveBox/wiki/Docker" \
  23. org.opencontainers.image.title="ArchiveBox" \
  24. org.opencontainers.image.vendor="ArchiveBox" \
  25. org.opencontainers.image.description="All-in-one self-hosted internet archiving solution" \
  26. org.opencontainers.image.source="https://github.com/ArchiveBox/ArchiveBox" \
  27. com.docker.image.source.entrypoint="Dockerfile" \
  28. # TODO: release ArchiveBox as a Docker Desktop extension (requires these labels):
  29. # https://docs.docker.com/desktop/extensions-sdk/architecture/metadata/
  30. com.docker.desktop.extension.api.version=">= 1.4.7" \
  31. com.docker.desktop.extension.icon="https://archivebox.io/icon.png" \
  32. com.docker.extension.publisher-url="https://archivebox.io" \
  33. com.docker.extension.screenshots='[{"alt": "Screenshot of Admin UI", "url": "https://github.com/ArchiveBox/ArchiveBox/assets/511499/e8e0b6f8-8fdf-4b7f-8124-c10d8699bdb2"}]' \
  34. com.docker.extension.detailed-description='See here for detailed documentation: https://wiki.archivebox.io' \
  35. com.docker.extension.changelog='See here for release notes: https://github.com/ArchiveBox/ArchiveBox/releases' \
  36. com.docker.extension.categories='database,utility-tools'
  37. ARG TARGETPLATFORM
  38. ARG TARGETOS
  39. ARG TARGETARCH
  40. ARG TARGETVARIANT
  41. ######### Environment Variables #################################
  42. # Global system-level config
  43. ENV TZ=UTC \
  44. LANGUAGE=en_US:en \
  45. LC_ALL=C.UTF-8 \
  46. LANG=C.UTF-8 \
  47. DEBIAN_FRONTEND=noninteractive \
  48. APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1 \
  49. PYTHONIOENCODING=UTF-8 \
  50. PYTHONUNBUFFERED=1 \
  51. PIP_DISABLE_PIP_VERSION_CHECK=1 \
  52. npm_config_loglevel=error
  53. # Version config
  54. ENV PYTHON_VERSION=3.11 \
  55. NODE_VERSION=20
  56. # User config
  57. ENV ARCHIVEBOX_USER="archivebox" \
  58. DEFAULT_PUID=911 \
  59. DEFAULT_PGID=911
  60. # Global paths
  61. ENV CODE_DIR=/app \
  62. DATA_DIR=/data \
  63. GLOBAL_VENV=/venv \
  64. PLAYWRIGHT_BROWSERS_PATH=/browsers
  65. # Application-level paths
  66. ENV APP_VENV=/app/.venv \
  67. NODE_MODULES=/app/node_modules
  68. # Build shell config
  69. ENV PATH="$PATH:$GLOBAL_VENV/bin:$APP_VENV/bin:$NODE_MODULES/.bin"
  70. SHELL ["/bin/bash", "-o", "pipefail", "-o", "errexit", "-o", "errtrace", "-o", "nounset", "-c"]
  71. ######### System Environment ####################################
  72. # Detect ArchiveBox version number by reading package.json
  73. COPY --chown=root:root --chmod=755 package.json "$CODE_DIR/"
  74. RUN grep '"version": ' "${CODE_DIR}/package.json" | awk -F'"' '{print $4}' > /VERSION.txt
  75. # Force apt to leave downloaded binaries in /var/cache/apt (massively speeds up Docker builds)
  76. RUN echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache \
  77. && rm -f /etc/apt/apt.conf.d/docker-clean
  78. # Print debug info about build and save it to disk, for human eyes only, not used by anything else
  79. RUN (echo "[i] Docker build for ArchiveBox $(cat /VERSION.txt) starting..." \
  80. && echo "PLATFORM=${TARGETPLATFORM} ARCH=$(uname -m) ($(uname -s) ${TARGETARCH} ${TARGETVARIANT})" \
  81. && echo "BUILD_START_TIME=$(date +"%Y-%m-%d %H:%M:%S %s") TZ=${TZ} LANG=${LANG}" \
  82. && echo \
  83. && echo "GLOBAL_VENV=${GLOBAL_VENV} APP_VENV=${APP_VENV} NODE_MODULES=${NODE_MODULES}" \
  84. && echo "PYTHON=${PYTHON_VERSION} NODE=${NODE_VERSION} PATH=${PATH}" \
  85. && echo "CODE_DIR=${CODE_DIR} DATA_DIR=${DATA_DIR}" \
  86. && echo \
  87. && uname -a \
  88. && cat /etc/os-release | head -n7 \
  89. && which bash && bash --version | head -n1 \
  90. && which dpkg && dpkg --version | head -n1 \
  91. && echo -e '\n\n' && env && echo -e '\n\n' \
  92. ) | tee -a /VERSION.txt
  93. # Create non-privileged user for archivebox and chrome
  94. RUN echo "[*] Setting up $ARCHIVEBOX_USER user uid=${DEFAULT_PUID}..." \
  95. && groupadd --system $ARCHIVEBOX_USER \
  96. && useradd --system --create-home --gid $ARCHIVEBOX_USER --groups audio,video $ARCHIVEBOX_USER \
  97. && usermod -u "$DEFAULT_PUID" "$ARCHIVEBOX_USER" \
  98. && groupmod -g "$DEFAULT_PGID" "$ARCHIVEBOX_USER" \
  99. && echo -e "\nARCHIVEBOX_USER=$ARCHIVEBOX_USER PUID=$(id -u $ARCHIVEBOX_USER) PGID=$(id -g $ARCHIVEBOX_USER)\n\n" \
  100. | tee -a /VERSION.txt
  101. # DEFAULT_PUID and DEFAULT_PID are overriden by PUID and PGID in /bin/docker_entrypoint.sh at runtime
  102. # https://docs.linuxserver.io/general/understanding-puid-and-pgid
  103. # Install system apt dependencies (adding backports to access more recent apt updates)
  104. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT \
  105. echo "[+] Installing APT base system dependencies for $TARGETPLATFORM..." \
  106. && echo 'deb https://deb.debian.org/debian bookworm-backports main contrib non-free' >> /etc/apt/sources.list.d/backports.list \
  107. && mkdir -p /etc/apt/keyrings \
  108. && apt-get update -qq \
  109. && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  110. # 1. packaging dependencies
  111. apt-transport-https ca-certificates apt-utils gnupg2 curl wget \
  112. # 2. docker and init system dependencies
  113. zlib1g-dev dumb-init gosu cron unzip grep ncat \
  114. # 3. frivolous CLI helpers to make debugging failed archiving easier
  115. # nano iputils-ping dnsutils htop procps jq yq \
  116. && rm -rf /var/lib/apt/lists/*
  117. ######### Language Environments ####################################
  118. # Install Node environment
  119. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-$TARGETARCH$TARGETVARIANT \
  120. echo "[+] Installing Node $NODE_VERSION environment in $NODE_MODULES..." \
  121. && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_VERSION}.x nodistro main" >> /etc/apt/sources.list.d/nodejs.list \
  122. && curl -fsSL "https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key" | gpg --dearmor | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
  123. && apt-get update -qq \
  124. && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  125. nodejs libatomic1 python3-minimal \
  126. && rm -rf /var/lib/apt/lists/* \
  127. # Update NPM to latest version
  128. && npm i -g npm --cache /root/.npm \
  129. # Save version info
  130. && ( \
  131. which node && node --version \
  132. && which npm && npm --version \
  133. && echo -e '\n\n' \
  134. ) | tee -a /VERSION.txt
  135. # Install Python environment
  136. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/pip,sharing=locked,id=pip-$TARGETARCH$TARGETVARIANT \
  137. echo "[+] Setting up Python $PYTHON_VERSION runtime..." \
  138. # tell PDM to allow using global system python site packages
  139. # && rm /usr/lib/python3*/EXTERNALLY-MANAGED \
  140. # create global virtual environment GLOBAL_VENV to use (better than using pip install --global)
  141. # && python3 -m venv --system-site-packages --symlinks $GLOBAL_VENV \
  142. # && python3 -m venv --system-site-packages $GLOBAL_VENV \
  143. # && python3 -m venv $GLOBAL_VENV \
  144. # install global dependencies / python build dependencies in GLOBAL_VENV
  145. # && pip install --upgrade pip setuptools wheel \
  146. # Save version info
  147. && ( \
  148. which python3 && python3 --version | grep " $PYTHON_VERSION" \
  149. && which pip && pip --version \
  150. # && which pdm && pdm --version \
  151. && echo -e '\n\n' \
  152. ) | tee -a /VERSION.txt
  153. ######### Extractor Dependencies ##################################
  154. # Install apt dependencies
  155. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/pip,sharing=locked,id=pip-$TARGETARCH$TARGETVARIANT \
  156. echo "[+] Installing APT extractor dependencies globally using apt..." \
  157. && apt-get update -qq \
  158. && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  159. curl wget git yt-dlp ffmpeg ripgrep \
  160. # Packages we have also needed in the past:
  161. # youtube-dl wget2 aria2 python3-pyxattr rtmpdump libfribidi-bin mpv \
  162. && rm -rf /var/lib/apt/lists/* \
  163. # Save version info
  164. && ( \
  165. which curl && curl --version | head -n1 \
  166. && which wget && wget --version 2>&1 | head -n1 \
  167. && which yt-dlp && yt-dlp --version 2>&1 | head -n1 \
  168. && which git && git --version 2>&1 | head -n1 \
  169. && which rg && rg --version 2>&1 | head -n1 \
  170. && echo -e '\n\n' \
  171. ) | tee -a /VERSION.txt
  172. # Install chromium browser using playwright
  173. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/pip,sharing=locked,id=pip-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/ms-playwright,sharing=locked,id=browsers-$TARGETARCH$TARGETVARIANT \
  174. echo "[+] Installing Browser binary dependencies to $PLAYWRIGHT_BROWSERS_PATH..." \
  175. && apt-get update -qq \
  176. && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  177. fontconfig fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-symbola fonts-noto fonts-freefont-ttf \
  178. # chrome can run without dbus/upower technically, it complains about missing dbus but should run ok anyway
  179. # libxss1 dbus dbus-x11 upower \
  180. # && service dbus start \
  181. && if [[ "$TARGETPLATFORM" == *amd64* || "$TARGETPLATFORM" == *arm64* ]]; then \
  182. # install Chromium using playwright
  183. pip install playwright \
  184. && cp -r /root/.cache/ms-playwright "$PLAYWRIGHT_BROWSERS_PATH" \
  185. && playwright install --with-deps chromium \
  186. && export CHROME_BINARY="$(python -c 'from playwright.sync_api import sync_playwright; print(sync_playwright().start().chromium.executable_path)')"; \
  187. else \
  188. # fall back to installing Chromium via apt-get on platforms not supported by playwright (e.g. risc, ARMv7, etc.)
  189. # apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  190. # chromium \
  191. # && export CHROME_BINARY="$(which chromium)"; \
  192. echo 'armv7 no longer supported in versions after v0.7.3' \
  193. exit 1; \
  194. fi \
  195. && rm -rf /var/lib/apt/lists/* \
  196. && ln -s "$CHROME_BINARY" /usr/bin/chromium-browser \
  197. && mkdir -p "/home/${ARCHIVEBOX_USER}/.config/chromium/Crash Reports/pending/" \
  198. && chown -R $ARCHIVEBOX_USER "/home/${ARCHIVEBOX_USER}/.config" \
  199. && mkdir -p "$PLAYWRIGHT_BROWSERS_PATH" \
  200. && chown -R $ARCHIVEBOX_USER "$PLAYWRIGHT_BROWSERS_PATH" \
  201. # Save version info
  202. && ( \
  203. which chromium-browser && /usr/bin/chromium-browser --version || /usr/lib/chromium/chromium --version \
  204. && echo -e '\n\n' \
  205. ) | tee -a /VERSION.txt
  206. # Install Node dependencies
  207. WORKDIR "$CODE_DIR"
  208. COPY --chown=root:root --chmod=755 "package.json" "package-lock.json" "$CODE_DIR"/
  209. RUN --mount=type=cache,target=/root/.npm,sharing=locked,id=npm-$TARGETARCH$TARGETVARIANT \
  210. echo "[+] Installing NPM extractor dependencies from package.json into $NODE_MODULES..." \
  211. && npm ci --prefer-offline --no-audit --cache /root/.npm \
  212. && ( \
  213. which node && node --version \
  214. && which npm && npm version \
  215. && echo -e '\n\n' \
  216. ) | tee -a /VERSION.txt
  217. ######### Build Dependencies ####################################
  218. # Install ArchiveBox Python dependencies
  219. WORKDIR "$CODE_DIR"
  220. COPY --chown=root:root --chmod=755 "./pyproject.toml" "requirements.txt" "$CODE_DIR"/
  221. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/pip,sharing=locked,id=pip-$TARGETARCH$TARGETVARIANT \
  222. echo "[+] Installing PIP ArchiveBox dependencies from requirements.txt for ${TARGETPLATFORM}..." \
  223. && apt-get update -qq \
  224. && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  225. build-essential \
  226. libssl-dev libldap2-dev libsasl2-dev \
  227. python3-ldap python3-msgpack python3-mutagen python3-regex python3-pycryptodome procps \
  228. # && ln -s "$GLOBAL_VENV" "$APP_VENV" \
  229. # && pdm use --venv in-project \
  230. # && pdm run python -m ensurepip \
  231. # && pdm sync --fail-fast --no-editable --group :all --no-self \
  232. # && pdm export -o requirements.txt --without-hashes \
  233. # && source $GLOBAL_VENV/bin/activate \
  234. && pip install -r requirements.txt \
  235. && apt-get purge -y \
  236. build-essential \
  237. && apt-get autoremove -y \
  238. && rm -rf /var/lib/apt/lists/*
  239. # Install ArchiveBox Python package from source
  240. COPY --chown=root:root --chmod=755 "." "$CODE_DIR/"
  241. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked,id=apt-$TARGETARCH$TARGETVARIANT --mount=type=cache,target=/root/.cache/pip,sharing=locked,id=pip-$TARGETARCH$TARGETVARIANT \
  242. echo "[*] Installing PIP ArchiveBox package from $CODE_DIR..." \
  243. # && apt-get update -qq \
  244. # install C compiler to build deps on platforms that dont have 32-bit wheels available on pypi
  245. # && apt-get install -qq -y -t bookworm-backports --no-install-recommends \
  246. # build-essential \
  247. # INSTALL ARCHIVEBOX python package globally from CODE_DIR, with all optional dependencies
  248. && pip install -e "$CODE_DIR"[sonic,ldap] \
  249. # save docker image size and always remove compilers / build tools after building is complete
  250. # && apt-get purge -y build-essential \
  251. # && apt-get autoremove -y \
  252. && rm -rf /var/lib/apt/lists/*
  253. ####################################################
  254. # Setup ArchiveBox runtime config
  255. WORKDIR "$DATA_DIR"
  256. ENV IN_DOCKER=True \
  257. DISPLAY=novnc:0.0 \
  258. CUSTOM_TEMPLATES_DIR=/data/templates \
  259. CHROME_USER_DATA_DIR=/data/personas/Default/chromium \
  260. GOOGLE_API_KEY=no \
  261. GOOGLE_DEFAULT_CLIENT_ID=no \
  262. GOOGLE_DEFAULT_CLIENT_SECRET=no \
  263. ALLOWED_HOSTS=*
  264. ## No need to set explicitly, these values will be autodetected by archivebox in docker:
  265. # WGET_BINARY="wget" \
  266. # YOUTUBEDL_BINARY="yt-dlp" \
  267. # CHROME_BINARY="/usr/bin/chromium-browser" \
  268. # USE_SINGLEFILE=True \
  269. # SINGLEFILE_BINARY="$NODE_MODULES/.bin/single-file" \
  270. # USE_READABILITY=True \
  271. # READABILITY_BINARY="$NODE_MODULES/.bin/readability-extractor" \
  272. # USE_MERCURY=True \
  273. # MERCURY_BINARY="$NODE_MODULES/.bin/postlight-parser"
  274. # Print version for nice docker finish summary
  275. RUN (echo -e "\n\n[√] Finished Docker build succesfully. Saving build summary in: /VERSION.txt" \
  276. && echo -e "PLATFORM=${TARGETPLATFORM} ARCH=$(uname -m) ($(uname -s) ${TARGETARCH} ${TARGETVARIANT})\n" \
  277. && echo -e "BUILD_END_TIME=$(date +"%Y-%m-%d %H:%M:%S %s")\n\n" \
  278. ) | tee -a /VERSION.txt
  279. RUN "$CODE_DIR"/bin/docker_entrypoint.sh version 2>&1 | tee -a /VERSION.txt
  280. ####################################################
  281. # Open up the interfaces to the outside world
  282. WORKDIR "$DATA_DIR"
  283. VOLUME "$DATA_DIR"
  284. EXPOSE 8000
  285. HEALTHCHECK --interval=30s --timeout=20s --retries=15 \
  286. CMD curl --silent 'http://localhost:8000/health/' | grep -q 'OK'
  287. ENTRYPOINT ["dumb-init", "--", "/app/bin/docker_entrypoint.sh"]
  288. CMD ["archivebox", "server", "--quick-init", "0.0.0.0:8000"]