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: sources/platform/actors/development/actor_definition/source_code.md
+57-8Lines changed: 57 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,16 +9,20 @@ sidebar_position: 4
9
9
10
10
---
11
11
12
-
The Apify Actor's source code placement is defined by its [Dockerfile](./dockerfile.md). If you have created the Actor from one of Apify's [templates](https://apify.com/templates) then it's by convention placed in the `/src` directory.
12
+
The structure and placement of an Apify Actor's source code is defined by its [`Dockerfile`](./dockerfile.md). If you create the Actor from one of Apify's [templates](https://apify.com/templates), the source code by default is placed in the `/src`.
13
13
14
-
It's completely up to you what language and technologies, including various binaries (Chrome browser, Selenium, Cypress, or any other dependency of your choice) you use in your project. The only requirement is that you have to define the Dockerfile that will build the image for your Actor, including all the dependencies and your source code.
14
+
You have the flexibility to choose any programming language, technologies, and dependencies (such as Chrome browser, Selenium, Cypress, or others) for your projects. The only requirement is to define a Dockerfile that builds the image for your Actor, including all dependencies and your source code.
15
15
16
16
## Example setup
17
17
18
-
Let's take a look at the example JavaScript Actor's source code. The following Dockerfile
18
+
To better understand how to structure your Actor's source code, let's take a look at an example for a JavaScript Actor.
19
+
20
+
### `Dockerfile`
21
+
22
+
Here's the complete `Dockerfile`
19
23
20
24
```dockerfile
21
-
FROM apify/actor-node:16
25
+
FROM apify/actor-node:20
22
26
23
27
COPY package*.json ./
24
28
@@ -37,11 +41,56 @@ COPY . ./
37
41
CMD npm start --silent
38
42
```
39
43
40
-
will build the Actor from the `apify/actor-node:16` image, copy the `package.json` and `package-lock.json` files to the image.
44
+
This `Dockerfile` does the following tasks:
45
+
46
+
1. Builds the Actor from the `apify/actor-node:20` base image.
47
+
48
+
```dockerfile
49
+
FROM apify/actor-node:20
50
+
```
51
+
52
+
2. Copies the `package.json` and `package-lock.json` files to the image.
53
+
54
+
```dockerfile
55
+
COPY package*.json ./
56
+
```
57
+
58
+
3. Installs the npm packages specified in package.json, omitting development and optional dependencies.
59
+
60
+
```dockerfile
61
+
RUN npm --quiet set progress=false \
62
+
&& npm install --omit=dev --omit=optional \
63
+
&& echo "Installed NPM packages:" \
64
+
&& (npm list --omit=dev --all || true) \
65
+
&& echo "Node.js version:" \
66
+
&& node --version \
67
+
&& echo "NPM version:" \
68
+
&& npm --version \
69
+
&& rm -r ~/.npm
70
+
```
71
+
72
+
4. Copies the rest of the source code to the image
73
+
74
+
```dockerfile
75
+
COPY . ./
76
+
```
77
+
78
+
5. Runs the `npm start` command defined in `package.json`
79
+
80
+
```dockerfile
81
+
CMD npm start --silent
82
+
```
83
+
84
+
:::note Optimized build cache
85
+
86
+
By copying the `package.json` and `package-lock.json` files and installing dependencies before the rest of the source code, you can take advantage of Docker's caching mechanism. This approach ensures that dependencies are only reinstalled when the `package.json` or `package-lock.json` files change, significantly reducing build times. Since the installation of dependencies is often the most time-consuming part of the build process, this optimization can lead to substantial performance improvements, especially for larger projects with many dependencies.
87
+
88
+
89
+
:::
41
90
42
-
> We first copy the `package.json`, `package-lock.json` , and install the dependencies before copying the rest of the source code. This way, we can take advantage of Docker's caching mechanism and only install the dependencies when the `package.json` or `package-lock.json` files change. This way, the build process is much faster.
91
+
### `package.json`
43
92
44
-
Then it will install the NPM packages and copy the rest of the source code to the image. Finally, it will run the `npm start` command, which is defined in the `package.json` file:
93
+
The `package.json` file defines the `npm start` command:
45
94
46
95
```json
47
96
{
@@ -62,4 +111,4 @@ Then it will install the NPM packages and copy the rest of the source code to th
62
111
}
63
112
```
64
113
65
-
So once the Actor starts, the `src/main.js`gets executed.
114
+
When the Actor starts, the `src/main.js` file is executed.
0 commit comments