From fe16543a6da653ec800d86aaacdfe37d8043740b Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Tue, 24 Jan 2023 01:14:30 -0800 Subject: [PATCH 1/2] Dockerfile: Add playwright support for rendering PNGs Closes #594 --- ci/release/Dockerfile | 24 ++++++++++++++++++++---- ci/release/Dockerfile_entrypoint.sh | 6 ++++++ ci/release/build.sh | 1 + docs/INSTALL.md | 2 +- main.go | 10 ++++++++++ 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100755 ci/release/Dockerfile_entrypoint.sh diff --git a/ci/release/Dockerfile b/ci/release/Dockerfile index 3d44ac80c..623a0f490 100644 --- a/ci/release/Dockerfile +++ b/ci/release/Dockerfile @@ -3,19 +3,35 @@ FROM debian:latest ARG TARGETARCH -RUN apt-get update && \ - apt-get install -y ca-certificates +RUN apt-get update && apt-get install -y ca-certificates curl dumb-init sudo + +RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash -s - && \ + apt-get install -y nodejs +RUN npx playwright install-deps + +RUN adduser --gecos '' --disabled-password debian \ + && echo "debian ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/nopasswd + +RUN curl -fsSL "https://github.com/boxboat/fixuid/releases/download/v0.5/fixuid-0.5-linux-$TARGETARCH.tar.gz" | tar -C /usr/local/bin -xzf - \ + && chown root:root /usr/local/bin/fixuid \ + && chmod 4755 /usr/local/bin/fixuid \ + && mkdir -p /etc/fixuid \ + && printf "user: debian\ngroup: debian\npaths: [/home/debian]\n" > /etc/fixuid/config.yml COPY ./d2-*-linux-$TARGETARCH.tar.gz /tmp +ADD ./Dockerfile_entrypoint.sh /usr/local/bin/entrypoint.sh RUN mkdir -p /usr/local/lib/d2 \ && tar -C /usr/local/lib/d2 -xzf /tmp/d2-*-linux-"$TARGETARCH".tar.gz \ && /usr/local/lib/d2/d2-*/scripts/install.sh \ && rm -Rf /tmp/d2-*-linux-"$TARGETARCH".tar.gz -WORKDIR /root/src +USER debian:debian +RUN d2 init-playwright + +WORKDIR /home/debian/src EXPOSE 8080 ENV PORT 8080 ENV HOST 0.0.0.0 ENV BROWSER false -ENTRYPOINT ["/usr/local/bin/d2"] +ENTRYPOINT ["/usr/local/bin/entrypoint.sh"] diff --git a/ci/release/Dockerfile_entrypoint.sh b/ci/release/Dockerfile_entrypoint.sh new file mode 100755 index 000000000..63ffeeb48 --- /dev/null +++ b/ci/release/Dockerfile_entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh +set -eu + +eval "$(fixuid -q)" + +exec dumb-init /usr/local/bin/d2 "$@" diff --git a/ci/release/build.sh b/ci/release/build.sh index 19b607336..e8148a83e 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -253,6 +253,7 @@ build_docker_image() { if [ -n "${PUSH_DOCKER-}" -o -n "${RELEASE-}" ]; then flags='--push --platform linux/amd64,linux/arm64' fi + sh_c rsync --archive --human-readable ./ci/release/Dockerfile_entrypoint.sh "./ci/release/build/$VERSION" sh_c docker buildx build $flags -t "$D2_DOCKER_IMAGE:$VERSION" -t "$D2_DOCKER_IMAGE:latest" --build-arg "VERSION=$VERSION" -f ./ci/release/Dockerfile "./ci/release/build/$VERSION" } diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 5c20aa1bd..8a4f7a090 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -219,7 +219,7 @@ Example usage: ```sh echo 'x -> y' >helloworld.d2 -docker run --rm -it -u "$(id -u):$(id -g)" -v "$PWD:/root/src" \ +docker run --rm -it -u "$(id -u):$(id -g)" -v "$PWD:/home/debian/src" \ -p 127.0.0.1:8080:8080 terrastruct/d2:v0.1.2 --watch helloworld.d2 # Visit http://127.0.0.1:8080 ``` diff --git a/main.go b/main.go index 646b8c994..c7a0429d9 100644 --- a/main.go +++ b/main.go @@ -96,6 +96,8 @@ func run(ctx context.Context, ms *xmain.State) (err error) { if len(ms.Opts.Flags.Args()) > 0 { switch ms.Opts.Flags.Arg(0) { + case "init-playwright": + return initPlaywright() case "layout": return layoutCmd(ctx, ms, ps) case "fmt": @@ -322,3 +324,11 @@ func populateLayoutOpts(ctx context.Context, ms *xmain.State, ps []d2plugin.Plug return nil } + +func initPlaywright() error { + pw, err := png.InitPlaywright() + if err != nil { + return err + } + return pw.Cleanup() +} From ccabee9e7dfa22b1113119a4ae9735576cb8cfb0 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Tue, 24 Jan 2023 01:19:09 -0800 Subject: [PATCH 2/2] next.md: Add Dockerfile release note --- ci/release/changelogs/next.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index f3c0d2a77..6dbdee6f0 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,5 +1,8 @@ #### Features ๐Ÿš€ +- The [Dockerfile](./docs/INSTALL.md#docker) now supports rendering PNGs [#594](https://github.com/terrastruct/d2/issues/594) + - There was a minor breaking change as part of this where the default working directory of the Dockerfile is now `/home/debian/src` instead of `/root/src` to allow UID remapping with [`fixuid`](https://github.com/boxboat/fixuid). + #### Improvements ๐Ÿงน #### Bugfixes โ›‘๏ธ