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
After you deploy a Worker that uses a Container, your image is uploaded to
11
-
[Cloudflare's Registry](/containers/image-management) and distributed globally to Cloudflare's Network.
10
+
After you deploy an application with a Container, your image is uploaded to
11
+
[Cloudflare's Registry](/containers/platform-details/image-management) and distributed globally to Cloudflare's Network.
12
12
Cloudflare will pre-schedule instances and pre-fetch images across the globe to ensure quick start
13
-
times when scaling up the number of concurrent container instances. This allows you to call
14
-
`env.YOUR_CONTAINER.get(id)` and get a new instance quickly without worrying
15
-
about the underlying scaling.
13
+
times when scaling up the number of concurrent container instances.
16
14
17
-
When a request is made to start a new container instance, the nearest location
18
-
with a pre-fetched image is selected. Subsequent requests to the same instance,
19
-
regardless of where they originate, will be routed to this location as long as
20
-
the instance stays alive.
15
+
Unlike Workers, which are updated immediately on deploy, container instances are updated using a rolling deploy strategy.
16
+
This allows you to gracefully shutdown any running instances during a rollout. Refer to [rollouts](/containers/platform-details/rollouts/) for more details.
21
17
22
-
Starting additional container instances will use other locations with pre-fetched images,
23
-
and Cloudflare will automatically begin prepping additional machines behind the scenes
24
-
for additional scaling and quick cold starts. Because there are a finite number pre-warmed
25
-
locations, some container instances may be started in locations that are farther away from
26
-
the end-user. This is done to ensure that the container instance starts quickly. You are
27
-
only charged for actively running instances and not for any unused pre-warmed images.
18
+
## Lifecycle of a Request
28
19
29
-
Each container instance runs inside its own VM, which provides strong
30
-
isolation from other workloads running on Cloudflare's network. Containers
31
-
should be built for the `linux/amd64` architecture, and should stay within
32
-
[size limits](/containers/platform-details/#limits). Logging, metrics collection, and
33
-
networking are automatically set up on each container.
20
+
### Client to Worker
34
21
35
-
## Life of a Container Request
36
-
37
-
When a request is made to any Worker, including one with an associated Container, it is generally handled
22
+
Recall that Containers are backed by Durable Objects and Workers.
23
+
Requests are first routed through a Worker, which is generally handled
38
24
by a datacenter in a location with the best latency between itself and the requesting user.
39
25
A different datacenter may be selected to optimize overall latency, if [Smart Placement](/workers/configuration/smart-placement/)
40
26
is on, or if the nearest location is under heavy load.
41
27
42
-
When a request is made to a Container instance, it is sent through a Durable Object, which
43
-
can be defined by either using a `DurableObject` or the [`Container` class](/containers/container-package), which
44
-
extends Durable Objects with Container-specific APIs and helpers. We recommend using `Container`, see
45
-
the [`Container` class documentation](/containers/container-package) for more details.
28
+
Because all Container requests are passed through a Worker, end-users cannot make TCP or
29
+
UDP requests to a Container instance. If you have a use case that requires inbound TCP
30
+
or UDP from an end-user, please [let us know](https://forms.gle/AGSq54VvUje6kmKu8).
31
+
32
+
### Worker to Durable Object
46
33
47
-
Each Durable Object is a globally routable isolate that can execute code and store state. This allows
34
+
From the Worker, a request passes through a Durable Object instance (the [Container package](/containers/container-package) extends a Durable Object class).
35
+
Each Durable Object instance is a globally routable isolate that can execute code and store state. This allows
48
36
developers to easily address and route to specific container instances (no matter where they are placed),
49
37
define and run hooks on container status changes, execute recurring checks on the instance, and store persistent
50
38
state associated with each instance.
51
39
52
-
As mentioned above, when a container instance starts, it is launched in the nearest pre-warmed location. This means that
53
-
code in a container is usually executed in a different location than the one handling the Workers request.
40
+
### Starting a Container
41
+
42
+
When a Durable Object instance requests to start a new container instance, the **nearest location
43
+
with a pre-fetched image** is selected.
54
44
55
45
:::note
56
46
Currently, Durable Objects may be co-located with their associated Container instance, but often are not.
@@ -59,6 +49,73 @@ Cloudflare is currently working on expanding the number of locations in which a
59
49
which will allow container instances to always run in the same location as their Durable Object.
60
50
:::
61
51
62
-
Because all Container requests are passed through a Worker, end-users cannot make TCP or
63
-
UDP requests to a Container instance. If you have a use case that requires inbound TCP
64
-
or UDP from an end-user, please [let us know](https://forms.gle/AGSq54VvUje6kmKu8).
52
+
Starting additional container instances will use other locations with pre-fetched images,
53
+
and Cloudflare will automatically begin prepping additional machines behind the scenes
54
+
for additional scaling and quick cold starts. Because there are a finite number of pre-warmed
55
+
locations, some container instances may be started in locations that are farther away from
56
+
the end-user. This is done to ensure that the container instance starts quickly. You are
57
+
only charged for actively running instances and not for any unused pre-warmed images.
58
+
59
+
#### Cold starts
60
+
61
+
A cold start is when a container instance is started from a completely stopped state.
62
+
If you call `env.MY_CONTAINER.get(id)` with a completely novel ID and launch
63
+
this instance for the first time, it will result in a cold start.
64
+
This will start the container image from its entrypoint for the first time. Depending
65
+
on what this entrypoint does, it will take a variable amount of time to start.
66
+
67
+
Container cold starts can often be the 2-3 second range, but this is dependent
68
+
on image size and code execution time, among other factors.
69
+
70
+
### Requests to running Containers
71
+
72
+
When a request _starts_ a new container instance, the nearest location with a pre-fetched image is selected.
73
+
Subsequent requests to a particular instance, regardless of where they originate, will be routed to this location as long as
74
+
the instance stays alive.
75
+
76
+
However, once that container instance stops and restarts, future requests could be routed to a _different_ location.
77
+
This location will again be the nearest location to the originating request with a pre-fetched image.
78
+
79
+
### Container runtime
80
+
81
+
Each container instance runs inside its own VM, which provides strong
82
+
isolation from other workloads running on Cloudflare's network. Containers
83
+
should be built for the `linux/amd64` architecture, and should stay within
84
+
[size limits](/containers/platform-details/limits). [Logging](containers/faq/#how-do-container-logs-work), metrics collection, and
85
+
networking are automatically set up on each container, if configured by the developer.
86
+
87
+
### After a request
88
+
89
+
Cloudflare will not actively shut off a container instance after a specific amount of
90
+
time. If you do not set `sleepAfter` on your Container class, or stop the instance
91
+
manually, it will continue to run unless its host server is restarted. This
92
+
happens on an irregular cadence, but frequently enough where Cloudflare does not
93
+
guarantee that any instance will run for any set period of time.
94
+
95
+
When a container instance is going to be shut down, it is sent a `SIGTERM` signal,
96
+
and then a `SIGKILL` signal after 15 minutes. You should perform any necessary
97
+
cleanup to ensure a graceful shutdown in this time. The container instance
98
+
will be rebooted elsewhere shortly after this.
99
+
100
+
All disk is ephemeral. When a Container instance goes to sleep, the next time
101
+
it is started, it will have a fresh disk as defined by its container image.
102
+
103
+
Persistent disk is something the Cloudflare team is exploring in the future, but
104
+
is not slated for the near term.
105
+
106
+
## An example request
107
+
108
+
- A developer deploys a Container. Cloudflare automatically readies instances across its Network.
109
+
- A request is made from a client in Bariloche, Argentina. It reaches the Worker in a nearby
110
+
Cloudflare location in Neuquen, Argentina.
111
+
- This Worker request calls `getContainer(env.MY_CONTAINER, "session-1337")`. Under the hood, this brings up a Durable
112
+
Object, which then calls `this.ctx.container.start`.
113
+
- This requests the nearest free Container instance. Cloudflare recognizes that an instance is free in Buenos Aires, Argentina, and
114
+
starts it there.
115
+
- A different user needs to route to the same container. This user's request reaches
116
+
the Worker running in Cloudflare's location in San Diego, US.
117
+
- The Worker again calls `getContainer(env.MY_CONTAINER, "session-1337")`.
118
+
- If the initial container instance is still running, the request is routed to the original location
119
+
in Buenos Aires. If the initial container has gone to sleep, Cloudflare will once
120
+
again try to find the nearest "free" instance of the Container, likely
121
+
one in North America, and start an instance there.
0 commit comments