Skip to content

Commit 585b5e3

Browse files
committed
docs: rework factory readme examples
1 parent 81b2531 commit 585b5e3

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
@@ -28,139 +28,175 @@ If no args are defined, only the default version of node will be installed. This
2828

2929
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.
3030

31-
Example: `NODE_VERSION='16.18.1'`
31+
Example: `NODE_VERSION='20.14.0'`
3232

3333
[Node Versions](https://nodejs.org/en/download/releases)
3434

3535
### YARN_VERSION
3636

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

39-
Example: `YARN_VERSION='1.22.19'`
39+
Example: `YARN_VERSION='1.22.22'`
4040

4141
[Yarn versions](https://www.npmjs.com/package/yarn)
4242

4343
### CYPRESS_VERSION
4444

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

47-
Example: `CYPRESS_VERSION='12.1.0'`
47+
Example: `CYPRESS_VERSION='13.11.0'`
4848

4949
[Cypress versions](https://www.npmjs.com/package/cypress)
5050

5151
### CHROME_VERSION
5252

5353
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.
5454

55-
Example: `CHROME_VERSION='107.0.5304.121-1'`
55+
Example: `CHROME_VERSION='125.0.6422.141-1'`
5656

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

5959
### FIREFOX_VERSION
6060

6161
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.
6262

63-
Example: `FIREFOX_VERSION='107.0'`
63+
Example: `FIREFOX_VERSION='126.0.1'`
6464

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

6767
### EDGE_VERSION
6868

6969
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.
7070

71-
Example: `EDGE_VERSION='100.0.1185.29-1'`
71+
Example: `EDGE_VERSION='125.0.2535.85-1'`
7272

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

7575
## Usage
7676

77-
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.
77+
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.
78+
79+
In the examples below, we install Cypress into the Docker image using:
80+
81+
```dockerfile
82+
RUN npm install cypress --save-dev
83+
RUN npx cypress install
84+
```
85+
86+
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.
87+
88+
### Example project
89+
90+
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:
91+
92+
```shell
93+
mkdir cy-example-test # or another directory of your choice
94+
cd cy-example-test
95+
npm init -y
96+
npm install cypress --save-dev
97+
npx cypress open
98+
```
99+
100+
In the Cypress GUI then:
101+
102+
- Select "E2E Testing"
103+
- Select "Continue"
104+
- Select "Electron" browser
105+
- Select "Create new spec"
106+
- Select "Create spec"
107+
- Close all Cypress windows
108+
109+
Finally, test that the spec `cypress/e2e/spec.cy.js` runs:
110+
111+
```shell
112+
npx cypress run
113+
```
78114

79115
### In the Dockerfile
80116

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

83-
Dockerfile
119+
Create a `Dockerfile` with the following content:
84120

85121
```dockerfile
86122
# Args are defined in the Dockerfile before the FROM command.
87-
# Using these args will cause an image to be created with node (default version is 16.18.1), chrome, firefox and edge.
88-
ARG CHROME_VERSION='107.0.5304.121-1'
89-
ARG EDGE_VERSION='100.0.1185.29-1'
90-
ARG FIREFOX_VERSION='107.0'
123+
# Using these args will cause an image to be created with
124+
# Node.js (default version from .env file), Chrome, Firefox and Edge.
125+
ARG CHROME_VERSION='125.0.6422.141-1'
126+
ARG EDGE_VERSION='125.0.2535.85-1'
127+
ARG FIREFOX_VERSION='126.0.1'
91128

92129
FROM cypress/factory
93130

94131
COPY . /opt/app
95132
WORKDIR /opt/app
96-
RUN npm install --save-dev cypress
133+
RUN npm install cypress --save-dev
134+
RUN npx cypress install
97135
```
98136

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

101139
```bash
102140
docker build . -t test
103-
docker run -it test npm run test -b chrome
141+
docker run -it --rm test npx cypress run -b chrome
104142
```
105143

106144
### At build time
107145

108146
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.
109147

110-
Dockerfile
148+
Create a `Dockerfile` with the following content:
111149

112150
```dockerfile
113151
FROM cypress/factory
114152

115153
COPY . /opt/app
116154
WORKDIR /opt/app
117-
RUN npm install --save-dev cypress
155+
RUN npm install cypress --save-dev
156+
RUN npx cypress install
118157
```
119158

120-
build commands
159+
Run the Docker commands:
121160

122161
```bash
123-
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
124-
125-
docker run test npm run test -b chrome
162+
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
163+
docker run -it --rm test npx cypress run -b chrome
126164
```
127165

128166
### In docker-compose.yml
129167

130-
Finally, args can be specified in the docker-compose.yml file.
168+
Finally, args can be specified in the `docker-compose.yml` file.
131169

132-
docker-compose.yml
170+
Create a `docker-compose.yml` file with the following content:
133171

134172
```yml
135-
version: '3'
136-
137173
services:
138174
test:
139175
build:
140176
context: .
141177
args:
142-
CHROME_VERSION: '107.0.5304.121-1'
143-
EDGE_VERSION: '100.0.1185.29-1'
144-
FIREFOX_VERSION: '107.0'
145-
command: npm run test
178+
CHROME_VERSION: '125.0.6422.141-1'
179+
EDGE_VERSION: '125.0.2535.85-1'
180+
FIREFOX_VERSION: '126.0.1'
181+
command: npx cypress run
146182
```
147183
148-
Dockerfile
184+
and a `Dockerfile` with this content:
149185

150186
```dockerfile
151187
FROM cypress/factory
152188
153189
COPY . /opt/app
154190
WORKDIR /opt/app
155-
RUN npm install --save-dev cypress
191+
RUN npm install cypress --save-dev
192+
RUN npx cypress install
156193
```
157194

158-
docker commands
195+
Run the Docker commands:
159196

160197
```bash
161198
docker compose build test
162-
163-
docker compose run test
199+
docker compose run --rm test
164200
```
165201

166202
### Reducing the size of the docker container
@@ -169,28 +205,27 @@ As mentioned above we can reduce the size of the docker image by removing browse
169205

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

172-
Dockerfile
208+
Create a `Dockerfile` with the following content:
173209

174210
```dockerfile
175-
# Args are defined in the Dockerfile before the FROM command.
176-
# Using these args will cause an image to be created with node (default version is 16.18.1), chrome, firefox and edge.
177-
ARG CHROME_VERSION='107.0.5304.121-1'
211+
ARG CHROME_VERSION='125.0.6422.141-1'
178212
179213
FROM cypress/factory
180214
181215
COPY . /opt/app
182216
WORKDIR /opt/app
183-
RUN npm install --save-dev cypress
217+
RUN npm install cypress --save-dev
218+
RUN npx cypress install
184219
```
185220

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

188223
```bash
189224
docker build . -t test
190-
docker run -it test npm run test -b chrome
225+
docker run -it --rm test npx cypress run -b chrome
191226
```
192227

193-
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.
228+
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.
194229

195230
## Version Testing
196231

0 commit comments

Comments
 (0)