Skip to content

Commit 7d2078e

Browse files
authored
devops: bake browsers into Docker image (#2990)
This patch bakes browsers into docker image. Important observations: - We now re-build docker image everytime we roll browsers. - Docker image size almost doubles: from `225MB` to `496MB`. References #2926
1 parent 9a2245d commit 7d2078e

File tree

7 files changed

+122
-25
lines changed

7 files changed

+122
-25
lines changed

.github/workflows/publish_canary_docker.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ on:
77
- release-*
88
paths:
99
- docs/docker/**
10+
- browsers.json
1011

1112
jobs:
1213
publish-canary-docker:
1314
name: "publish to DockerHub"
1415
runs-on: ubuntu-18.04
1516
steps:
1617
- uses: actions/checkout@v2
18+
- uses: actions/setup-node@v1
19+
with:
20+
node-version: 10.15
21+
- run: npm ci
22+
- run: npm run build
23+
- run: ./docs/docker/build.sh --prepare-context
1724
- uses: docker/build-push-action@v1
1825
with:
1926
username: playwright

.github/workflows/publish_release.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ jobs:
2929
runs-on: ubuntu-18.04
3030
steps:
3131
- uses: actions/checkout@v2
32+
- uses: actions/setup-node@v1
33+
with:
34+
node-version: 10.15
35+
- run: npm ci
36+
- run: npm run build
37+
- run: ./docs/docker/build.sh --prepare-context
3238
- uses: docker/build-push-action@v1
3339
with:
3440
username: playwright

docs/docker/CURRENT_DOCKER_IMAGE_SIZE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
(generated with docker-image-size.sh)
2-
225M
2+
496M

docs/docker/Dockerfile.bionic

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
################################################
2-
# Compile with:
3-
# sudo docker build -t microsoft/playwright:bionic -f Dockerfile.bionic .
4-
#
5-
# Run with:
6-
# sudo docker run -d -p --rm --name playwright microsoft/playwright:bionic
7-
#
8-
#################################################
9-
101
FROM ubuntu:bionic
112

123
# 1. Install node12
@@ -72,3 +63,16 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
7263

7364
# 9. Run everything after as non-privileged user.
7465
USER pwuser
66+
67+
# === BAKE BROWSERS INTO IMAGE ===
68+
69+
# 1. Add tip-of-tree Playwright package to install its browsers.
70+
# The package should be built beforehand from tip-of-tree Playwright.
71+
COPY --chown=pwuser ./playwright.tar.gz /home/pwuser/playwright.tar.gz
72+
73+
# 2. Install playwright and then delete the installation.
74+
# Browsers will remain downloaded in `/home/pwuser/.cache/ms-playwright`.
75+
RUN mkdir /home/pwuser/tmp && cd /home/pwuser/tmp && npm init -y && \
76+
npm i ../playwright.tar.gz && \
77+
cd ../ && rm -rf tmp && rm /home/pwuser/playwright.tar.gz
78+

docs/docker/README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[Dockerfile.bionic](Dockerfile.bionic) is a playwright-ready image of playwright.
44
This image includes all the dependencies needed to run browsers in a Docker
5-
container.
5+
container, including browsers.
66

77
<!-- GEN:toc -->
88
- [Usage](#usage)
@@ -47,18 +47,30 @@ See our [Continuous Integration guides](../ci.md) for sample configs.
4747

4848
### Build the image
4949

50+
Use [`//docs/docker/build.sh`](build.sh) to build the image.
51+
5052
```
51-
$ docker build -t mcr.microsoft.com/playwright:bionic -f Dockerfile.bionic .
53+
$ ./docs/docker/build.sh
5254
```
5355

54-
### Push
55-
56-
Playwright on Docker Hub is published via the [Microsoft Container Registry](https://github.com/microsoft/containerregistry).
56+
The image will be tagged as `playwright:localbuild` and could be run as:
5757

5858
```
59-
$ docker push playwright.azurecr.io/public/playwright:bionic
59+
$ docker run --rm -it playwright:localbuild /bin/bash
6060
```
6161

62+
> **NOTE**: any commit that changes docker image should also update [`//docs/docker/CURRENT_DOCKER_IMAGE_SIZE`](CURRENT_DOCKER_IMAGE_SIZE). Please run [`//docs/docker/docker-image-size.sh`](docker-image-size.sh) locally and commit updated number.
63+
64+
### Push
65+
66+
Docker images are published automatically by Github Actions. We currently publish the following
67+
images:
68+
- `mcr.microsoft.com/playwright:dev` - tip-of-tree image version.
69+
- `mcr.microsoft.com/playwright:bionic` - last Playwright release docker image.
70+
- `mcr.microsoft.com/playwright:sha-XXXXXXX` - docker image for every commit that changed
71+
docker files or browsers, marked with a [short sha](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Short-SHA-1) (first 7 digits of the SHA commit).
72+
73+
6274
## Base images
6375

6476
`playwright:bionic` is based on Ubuntu 18.04 LTS (Bionic Beaver).

docs/docker/build.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
set -e
3+
set +x
4+
5+
if [[ ($1 == '--help') || ($1 == '-h') ]]; then
6+
echo "usage: $(basename $0) [--prepare-only]"
7+
echo
8+
echo "Build Playwright docker image and tag it as 'playwright:localbuild'."
9+
echo "Once image is built, you can run it with"
10+
echo ""
11+
echo " docker run --rm -it playwright:localbuild /bin/bash"
12+
echo ""
13+
echo "NOTE: this requires on Playwright dependencies to be installed with 'npm install'"
14+
echo " and Playwright itself being built with 'npm run build'"
15+
echo
16+
echo " --prepare-context prepare docker context and skip building."
17+
echo " This is to defer building & publishing to Docker Github Action."
18+
echo ""
19+
exit 0
20+
fi
21+
22+
PREPARE_CONTEXT_ONLY=""
23+
if [[ $1 == "--prepare-context" ]]; then
24+
PREPARE_CONTEXT_ONLY="1"
25+
fi
26+
27+
function cleanup() {
28+
if [[ -z "${PREPARE_CONTEXT_ONLY}" ]]; then
29+
rm -f "playwright.tar.gz"
30+
fi
31+
}
32+
33+
trap "cleanup; cd $(pwd -P)" EXIT
34+
cd "$(dirname "$0")"
35+
36+
# We rely on `./playwright.tar.gz` to download browsers into the docker
37+
# image.
38+
node ../../packages/build_package.js playwright ./playwright.tar.gz
39+
40+
if [[ -n "${PREPARE_CONTEXT_ONLY}" ]]; then
41+
exit 0
42+
fi
43+
44+
docker build -t "playwright:localbuild" -f Dockerfile.bionic .

docs/docker/docker-image-size.sh

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,49 @@ set +x
55
# This script computes **compressed image size with all its layers**.
66
# This solution is based on https://stackoverflow.com/a/55156181/314883
77

8-
DOCKER_IMAGE_NAME="docker-image-to-count-compressed-size"
8+
9+
if [[ ($1 == '--help') || ($1 == '-h') ]]; then
10+
echo "usage: $(basename $0) [--image-name <local image to compute size>]"
11+
echo
12+
echo "Compute docker image size defined by the 'Dockerfile.bionic'."
13+
echo ""
14+
echo "Script will build the image using 'build.sh', unless '--image-name'"
15+
echo "is specified."
16+
echo ""
17+
echo "NOTE: this requires on Playwright dependencies to be installed with 'npm install'"
18+
echo " and Playwright itself being built with 'npm run build'"
19+
echo
20+
echo " --image-name custom image name to compute size of."
21+
echo ""
22+
exit 0
23+
fi
24+
25+
CUSTOM_IMAGE_NAME=""
26+
if [[ $1 == "--image-name" ]]; then
27+
CUSTOM_IMAGE_NAME=$2
28+
fi
29+
30+
TMP_IMAGE_NAME="docker-image-to-count-compressed-size"
931
FILE_NAME="docker-image-to-count-compressed-size"
1032

1133
function cleanup() {
12-
echo "-- Removing .tar if any"
1334
rm -f "${FILE_NAME}.tar"
14-
echo "-- Removing .tar.gz if any"
1535
rm -f "${FILE_NAME}.tar.gz"
16-
echo "-- Removing docker image if any"
17-
docker rmi "${DOCKER_IMAGE_NAME}:bionic" >/dev/null
36+
docker rmi "${TMP_IMAGE_NAME}:bionic" >/dev/null
1837
}
1938

2039
trap "cleanup; cd $(pwd -P)" EXIT
2140
cd "$(dirname "$0")"
2241

23-
echo "-- Building image..."
24-
docker build -t "${DOCKER_IMAGE_NAME}:bionic" -f Dockerfile.bionic . >/dev/null
25-
echo "-- Saving .tar of the image..."
26-
docker save "${DOCKER_IMAGE_NAME}:bionic" > "${FILE_NAME}.tar"
42+
if [[ -z "${CUSTOM_IMAGE_NAME}" ]]; then
43+
echo "-- Building image..."
44+
./build.sh >/dev/null
45+
echo "-- Saving .tar of the image..."
46+
docker save "${TMP_IMAGE_NAME}:bionic" > "${FILE_NAME}.tar"
47+
else
48+
echo "-- Saving .tar of the image..."
49+
docker save "${CUSTOM_IMAGE_NAME}" > "${FILE_NAME}.tar"
50+
fi
2751
echo "-- Compressing image..."
2852
gzip "${FILE_NAME}.tar" >/dev/null
2953

0 commit comments

Comments
 (0)