Skip to content

Commit 86b5e12

Browse files
authored
docs: rework factory readme examples (#1104)
1 parent b0e0450 commit 86b5e12

File tree

1 file changed

+75
-40
lines changed

1 file changed

+75
-40
lines changed

factory/README.md

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,139 +42,175 @@ If no args are defined, only the default version of node will be installed. This
4242

4343
The version of Node to install in the docker container. If the env is unset or an empty string, the default version of Node (defined [here](./.env)) is installed. Node is required. The exact version must be used, no wildcards or shorthands are supported.
4444

45-
Example: `NODE_VERSION='16.18.1'`
45+
Example: `NODE_VERSION='20.14.0'`
4646

4747
[Node Versions](https://nodejs.org/en/download/releases)
4848

4949
### YARN_VERSION
5050

5151
The version of yarn to install (via npm). If the env is unset or an empty string, Yarn is not installed.
5252

53-
Example: `YARN_VERSION='1.22.19'`
53+
Example: `YARN_VERSION='1.22.22'`
5454

5555
[Yarn versions](https://www.npmjs.com/package/yarn)
5656

5757
### CYPRESS_VERSION
5858

5959
The version of Cypress to install (via npm). If the env is unset or an empty string, Cypress is not installed.
6060

61-
Example: `CYPRESS_VERSION='12.1.0'`
61+
Example: `CYPRESS_VERSION='13.11.0'`
6262

6363
[Cypress versions](https://www.npmjs.com/package/cypress)
6464

6565
### CHROME_VERSION
6666

6767
The version of Chrome to install. If the env is unset or an empty string, Chrome is not installed. The exact version must be used, no wildcards or shorthands are supported.
6868

69-
Example: `CHROME_VERSION='107.0.5304.121-1'`
69+
Example: `CHROME_VERSION='125.0.6422.141-1'`
7070

7171
[Chrome versions](https://www.ubuntuupdates.org/package/google_chrome/stable/main/base/google-chrome-stable)
7272

7373
### FIREFOX_VERSION
7474

7575
The version of Firefox to install. If the env is unset or an empty string, Firefox is not installed. The exact version must be used, no wildcards or shorthands are supported.
7676

77-
Example: `FIREFOX_VERSION='107.0'`
77+
Example: `FIREFOX_VERSION='126.0.1'`
7878

7979
[Firefox versions](https://download-installer.cdn.mozilla.net/pub/firefox/releases/)
8080

8181
### EDGE_VERSION
8282

8383
The version of Edge to install. If the env is unset or an empty string, Edge is not installed. The exact version must be used, no wildcards or shorthands are supported.
8484

85-
Example: `EDGE_VERSION='100.0.1185.29-1'`
85+
Example: `EDGE_VERSION='125.0.2535.85-1'`
8686

8787
[Edge versions](https://packages.microsoft.com/repos/edge/pool/main/m/microsoft-edge-stable/)
8888

8989
## Usage
9090

91-
The cypress docker factory works by relying on the [`ONBUILD`](https://docs.docker.com/engine/reference/builder/#onbuild) docker instruction to run commands at the container's build time. To make use of the docker factory users will have to create a dockerfile to declare what dependency versions are desired. Docker `args` can be specified in a number of ways, some of which are demonstrated below. For each of these examples we are building the equivalent of the `cypress/browsers` docker image. In each instance since the example is only testing the chrome version, the examples could just install chrome by itself if the other browsers were not used.
91+
The cypress docker factory works by relying on the [`ONBUILD`](https://docs.docker.com/engine/reference/builder/#onbuild) docker instruction to run commands at the container's build time. To make use of the docker factory users will have to create a dockerfile to declare what dependency versions are desired. Docker `args` can be specified in a number of ways, some of which are demonstrated below. For each of these examples we are building the equivalent of the `cypress/browsers` docker image with Cypress additionally installed. In each instance, since the example is only testing the chrome version, the examples could just install chrome by itself if the other browsers were not used.
92+
93+
In the examples below, we install Cypress into the Docker image using:
94+
95+
```dockerfile
96+
RUN npm install cypress --save-dev
97+
RUN npx cypress install
98+
```
99+
100+
The additional `npx cypress install` command ensures that the Cypress binary component is installed, even if the Docker build step is re-run. The [Docker build cache](https://docs.docker.com/build/cache/) process may otherwise incorrectly optimize the build and fail to include the Cypress binary in the image, leading to run failures.
101+
102+
### Example project
103+
104+
To test the following Docker build examples you need to have a Cypress project available. You can create a simple Cypress E2E project by following these steps:
105+
106+
```shell
107+
mkdir cy-example-test # or another directory of your choice
108+
cd cy-example-test
109+
npm init -y
110+
npm install cypress --save-dev
111+
npx cypress open
112+
```
113+
114+
In the Cypress GUI then:
115+
116+
- Select "E2E Testing"
117+
- Select "Continue"
118+
- Select "Electron" browser
119+
- Select "Create new spec"
120+
- Select "Create spec"
121+
- Close all Cypress windows
122+
123+
Finally, test that the spec `cypress/e2e/spec.cy.js` runs:
124+
125+
```shell
126+
npx cypress run
127+
```
92128

93129
### In the Dockerfile
94130

95131
Args can be defined directly in the Dockerfile to specify variables that are available when the container is built.
96132

97-
Dockerfile
133+
Create a `Dockerfile` with the following content:
98134

99135
```dockerfile
100136
# Args are defined in the Dockerfile before the FROM command.
101-
# Using these args will cause an image to be created with node (default version is 16.18.1), chrome, firefox and edge.
102-
ARG CHROME_VERSION='107.0.5304.121-1'
103-
ARG EDGE_VERSION='100.0.1185.29-1'
104-
ARG FIREFOX_VERSION='107.0'
137+
# Using these args will cause an image to be created with
138+
# Node.js (default version from .env file), Chrome, Firefox and Edge.
139+
ARG CHROME_VERSION='125.0.6422.141-1'
140+
ARG EDGE_VERSION='125.0.2535.85-1'
141+
ARG FIREFOX_VERSION='126.0.1'
105142

106143
FROM cypress/factory
107144

108145
COPY . /opt/app
109146
WORKDIR /opt/app
110-
RUN npm install --save-dev cypress
147+
RUN npm install cypress --save-dev
148+
RUN npx cypress install
111149
```
112150

113151
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
114152

115153
```bash
116154
docker build . -t test
117-
docker run -it test npm run test -b chrome
155+
docker run -it --rm test npx cypress run -b chrome
118156
```
119157

120158
### At build time
121159

122160
Args can be passed to the docker build command with the `--build-arg` flag. Note: any value set via the command line will override the ARG value provided in the Dockerfile.
123161

124-
Dockerfile
162+
Create a `Dockerfile` with the following content:
125163

126164
```dockerfile
127165
FROM cypress/factory
128166

129167
COPY . /opt/app
130168
WORKDIR /opt/app
131-
RUN npm install --save-dev cypress
169+
RUN npm install cypress --save-dev
170+
RUN npx cypress install
132171
```
133172

134-
build commands
173+
Run the Docker commands:
135174

136175
```bash
137-
docker build . --build-arg CHROME_VERSION='107.0.5304.121-1' --build-arg EDGE_VERSION='100.0.1185.29-1' --build-arg FIREFOX_VERSION='107.0' -t test
138-
139-
docker run test npm run test -b chrome
176+
docker build . --build-arg CHROME_VERSION='125.0.6422.141-1' --build-arg EDGE_VERSION='125.0.2535.85-1' --build-arg FIREFOX_VERSION='126.0.1' -t test
177+
docker run -it --rm test npx cypress run -b chrome
140178
```
141179

142180
### In docker-compose.yml
143181

144-
Finally, args can be specified in the docker-compose.yml file.
182+
Finally, args can be specified in the `docker-compose.yml` file.
145183

146-
docker-compose.yml
184+
Create a `docker-compose.yml` file with the following content:
147185

148186
```yml
149-
version: '3'
150-
151187
services:
152188
test:
153189
build:
154190
context: .
155191
args:
156-
CHROME_VERSION: '107.0.5304.121-1'
157-
EDGE_VERSION: '100.0.1185.29-1'
158-
FIREFOX_VERSION: '107.0'
159-
command: npm run test
192+
CHROME_VERSION: '125.0.6422.141-1'
193+
EDGE_VERSION: '125.0.2535.85-1'
194+
FIREFOX_VERSION: '126.0.1'
195+
command: npx cypress run
160196
```
161197
162-
Dockerfile
198+
and a `Dockerfile` with this content:
163199

164200
```dockerfile
165201
FROM cypress/factory
166202
167203
COPY . /opt/app
168204
WORKDIR /opt/app
169-
RUN npm install --save-dev cypress
205+
RUN npm install cypress --save-dev
206+
RUN npx cypress install
170207
```
171208

172-
docker commands
209+
Run the Docker commands:
173210

174211
```bash
175212
docker compose build test
176-
177-
docker compose run test
213+
docker compose run --rm test
178214
```
179215

180216
### Reducing the size of the docker container
@@ -183,28 +219,27 @@ As mentioned above we can reduce the size of the docker image by removing browse
183219

184220
Since this example only uses chrome, removing edge and firefox is as simple as not including a version.
185221

186-
Dockerfile
222+
Create a `Dockerfile` with the following content:
187223

188224
```dockerfile
189-
# Args are defined in the Dockerfile before the FROM command.
190-
# Using these args will cause an image to be created with node (default version is 16.18.1), chrome, firefox and edge.
191-
ARG CHROME_VERSION='107.0.5304.121-1'
225+
ARG CHROME_VERSION='125.0.6422.141-1'
192226
193227
FROM cypress/factory
194228
195229
COPY . /opt/app
196230
WORKDIR /opt/app
197-
RUN npm install --save-dev cypress
231+
RUN npm install cypress --save-dev
232+
RUN npx cypress install
198233
```
199234

200235
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
201236

202237
```bash
203238
docker build . -t test
204-
docker run -it test npm run test -b chrome
239+
docker run -it --rm test npx cypress run -b chrome
205240
```
206241

207-
The docker image including chrome, edge and firefox weighs in a ~1.93 GB, by removing edge and firefox the image can be reduced to ~1.06 GB.
242+
The released [cypress/base](https://hub.docker.com/r/cypress/base) image (no browsers) has a compressed size on [Docker Hub](https://hub.docker.com/u/cypress) of ~ 230 MB. The [cypress/browsers](https://hub.docker.com/r/cypress/browsers) image for `linux/amd64` has a compressed image size of ~ 840 MB. By generating a custom image with unneeded browsers removed, the image size can be correspondingly reduced.
208243

209244
## Version Testing
210245

0 commit comments

Comments
 (0)