Merge pull request #713 from nhooyr/docker-png-b6d1

Dockerfile: Add playwright support for rendering PNGs
This commit is contained in:
Anmol Sethi 2023-01-24 11:50:44 -08:00 committed by GitHub
commit 4afd33c4ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 5 deletions

View file

@ -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"]

View file

@ -0,0 +1,6 @@
#!/bin/sh
set -eu
eval "$(fixuid -q)"
exec dumb-init /usr/local/bin/d2 "$@"

View file

@ -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"
}

View file

@ -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 ⛑️

View file

@ -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
```

10
main.go
View file

@ -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()
}