You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: factory/README.md
+75-40Lines changed: 75 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,139 +42,175 @@ If no args are defined, only the default version of node will be installed. This
42
42
43
43
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.
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.
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.
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.
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
+
```
92
128
93
129
### In the Dockerfile
94
130
95
131
Args can be defined directly in the Dockerfile to specify variables that are available when the container is built.
96
132
97
-
Dockerfile
133
+
Create a `Dockerfile` with the following content:
98
134
99
135
```dockerfile
100
136
# 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'
105
142
106
143
FROM cypress/factory
107
144
108
145
COPY . /opt/app
109
146
WORKDIR /opt/app
110
-
RUN npm install --save-dev cypress
147
+
RUN npm install cypress --save-dev
148
+
RUN npx cypress install
111
149
```
112
150
113
151
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
114
152
115
153
```bash
116
154
docker build . -t test
117
-
docker run -it testnpm run test -b chrome
155
+
docker run -it --rm testnpx cypress run -b chrome
118
156
```
119
157
120
158
### At build time
121
159
122
160
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.
docker run -it --rm test npx cypress run -b chrome
140
178
```
141
179
142
180
### In docker-compose.yml
143
181
144
-
Finally, args can be specified in the docker-compose.yml file.
182
+
Finally, args can be specified in the `docker-compose.yml` file.
145
183
146
-
docker-compose.yml
184
+
Create a `docker-compose.yml` file with the following content:
147
185
148
186
```yml
149
-
version: '3'
150
-
151
187
services:
152
188
test:
153
189
build:
154
190
context: .
155
191
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
160
196
```
161
197
162
-
Dockerfile
198
+
and a `Dockerfile` with this content:
163
199
164
200
```dockerfile
165
201
FROM cypress/factory
166
202
167
203
COPY . /opt/app
168
204
WORKDIR /opt/app
169
-
RUN npm install --save-dev cypress
205
+
RUN npm install cypress --save-dev
206
+
RUN npx cypress install
170
207
```
171
208
172
-
docker commands
209
+
Run the Docker commands:
173
210
174
211
```bash
175
212
docker compose build test
176
-
177
-
docker compose run test
213
+
docker compose run --rm test
178
214
```
179
215
180
216
### 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
183
219
184
220
Since this example only uses chrome, removing edge and firefox is as simple as not including a version.
185
221
186
-
Dockerfile
222
+
Create a `Dockerfile` with the following content:
187
223
188
224
```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'
192
226
193
227
FROM cypress/factory
194
228
195
229
COPY . /opt/app
196
230
WORKDIR /opt/app
197
-
RUN npm install --save-dev cypress
231
+
RUN npm install cypress --save-dev
232
+
RUN npx cypress install
198
233
```
199
234
200
235
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
201
236
202
237
```bash
203
238
docker build . -t test
204
-
docker run -it testnpm run test -b chrome
239
+
docker run -it --rm test npx cypress run -b chrome
205
240
```
206
241
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.
0 commit comments