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
@@ -28,139 +28,175 @@ If no args are defined, only the default version of node will be installed. This
28
28
29
29
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.
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
+
```
78
114
79
115
### In the Dockerfile
80
116
81
117
Args can be defined directly in the Dockerfile to specify variables that are available when the container is built.
82
118
83
-
Dockerfile
119
+
Create a `Dockerfile` with the following content:
84
120
85
121
```dockerfile
86
122
# 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'
91
128
92
129
FROM cypress/factory
93
130
94
131
COPY . /opt/app
95
132
WORKDIR /opt/app
96
-
RUN npm install --save-dev cypress
133
+
RUN npm install cypress --save-dev
134
+
RUN npx cypress install
97
135
```
98
136
99
137
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
100
138
101
139
```bash
102
140
docker build . -t test
103
-
docker run -it testnpm run test -b chrome
141
+
docker run -it --rm testnpx cypress run -b chrome
104
142
```
105
143
106
144
### At build time
107
145
108
146
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
126
164
```
127
165
128
166
### In docker-compose.yml
129
167
130
-
Finally, args can be specified in the docker-compose.yml file.
168
+
Finally, args can be specified in the `docker-compose.yml` file.
131
169
132
-
docker-compose.yml
170
+
Create a `docker-compose.yml` file with the following content:
133
171
134
172
```yml
135
-
version: '3'
136
-
137
173
services:
138
174
test:
139
175
build:
140
176
context: .
141
177
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
146
182
```
147
183
148
-
Dockerfile
184
+
and a `Dockerfile` with this content:
149
185
150
186
```dockerfile
151
187
FROM cypress/factory
152
188
153
189
COPY . /opt/app
154
190
WORKDIR /opt/app
155
-
RUN npm install --save-dev cypress
191
+
RUN npm install cypress --save-dev
192
+
RUN npx cypress install
156
193
```
157
194
158
-
docker commands
195
+
Run the Docker commands:
159
196
160
197
```bash
161
198
docker compose build test
162
-
163
-
docker compose run test
199
+
docker compose run --rm test
164
200
```
165
201
166
202
### 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
169
205
170
206
Since this example only uses chrome, removing edge and firefox is as simple as not including a version.
171
207
172
-
Dockerfile
208
+
Create a `Dockerfile` with the following content:
173
209
174
210
```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'
178
212
179
213
FROM cypress/factory
180
214
181
215
COPY . /opt/app
182
216
WORKDIR /opt/app
183
-
RUN npm install --save-dev cypress
217
+
RUN npm install cypress --save-dev
218
+
RUN npx cypress install
184
219
```
185
220
186
221
Then, in the same directory as the Dockerfile, run the following commands to build the docker container and run Cypress against the chrome browser.
187
222
188
223
```bash
189
224
docker build . -t test
190
-
docker run -it testnpm run test -b chrome
225
+
docker run -it --rm test npx cypress run -b chrome
191
226
```
192
227
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.
0 commit comments