Skip to content

Commit 0432ef0

Browse files
authored
docs: updates to source code doc (#1032)
ensure style guide compliance rewrites for clarity & readability add admonitions
1 parent 6aaacc8 commit 0432ef0

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

sources/platform/actors/development/actor_definition/source_code.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ sidebar_position: 4
99

1010
---
1111

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`.
1313

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.
1515

1616
## Example setup
1717

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`
1923

2024
```dockerfile
21-
FROM apify/actor-node:16
25+
FROM apify/actor-node:20
2226

2327
COPY package*.json ./
2428

@@ -37,11 +41,56 @@ COPY . ./
3741
CMD npm start --silent
3842
```
3943

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+
:::
4190
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`
4392
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:
4594
4695
```json
4796
{
@@ -62,4 +111,4 @@ Then it will install the NPM packages and copy the rest of the source code to th
62111
}
63112
```
64113
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

Comments
 (0)