Image overrides for local ECS services
--image-override is a flag of
cdkd local start-service and
cdkd local start-alb. It builds a service's image from
a local Dockerfile, in place of the image the service names.
cdkd local run-task
takes the same flags for a single task.
You need it when the service uses an image that is already in a registry
(ContainerImage.fromEcrRepository or fromRegistry). cdkd pulls that image,
so edits to your working copy would never reach the container, and --watch
has nothing to rebuild for that service.
An image from an ECR repository that the same stack creates
(ContainerImage.fromEcrRepository(repo) on a repository in the app) needs it
too, unless --from-state or --from-cfn-stack finds the deployed
repository: without one, the image has no address to pull from. cdkd refuses
to start while such an image has no override, before any service starts.
Combine it with --from-cfn-stack or --from-state to run your working copy
against the deployed stack's real configuration:
cdkd local start-service MyStack/Orders --from-cfn-stack \
--image-override MyStack/Orders=./orders/Dockerfile
The two forms of --image-override
The flag takes a Dockerfile for one named service, or a Dockerfile alone:
| Form | Effect |
|---|---|
--image-override <service>=<dockerfile> |
Uses that Dockerfile for that service. |
--image-override <dockerfile> |
Asks which services should use it. |
The second form opens a picker over the services that have no override yet, so one Dockerfile can serve several services. Naming the same service twice in the first form is an error.
<service> names the service in any of these ways, here and in the
per-service forms of the build flags below:
- the target you passed to
start-service, such asMyStack/Orders - its
ServiceName, or its construct id (such asOrders) when the service sets noServiceName - its logical id, or
Stack:LogicalId
start-alb starts every service behind the load balancer, so name each one
by its ServiceName, construct id or logical id. If a name fits two services
that use a registry image, cdkd refuses to start; use Stack:LogicalId to
pick one. Two keys that name the same service are also an error.
Build arguments, secrets and stages
Four flags pass options to the override builds. Each takes a value for every build or a value for one service, and the value for one service wins.
| Flag | For every build | For one service |
|---|---|---|
--image-build-arg |
KEY=VAL |
<service>:KEY=VAL |
--image-build-secret |
id=src |
<service>:id=src |
--image-target |
<stage> |
<service>=<stage> |
--image-context |
<dir> |
<service>=<dir> |
--image-context sets the docker build context, for a Dockerfile that sits
below the directory it needs, as in a monorepo whose apps share code:
--image-override Api=apps/api/Dockerfile --image-context Api=.. By default
the context is the Dockerfile's directory. cdkd resolves a relative directory
against the working directory and expands ~; one that does not exist stops
the start.
--image-build-secret supplies the file that a Dockerfile line such as
RUN --mount=type=secret,id=<id> reads, which is how a build reaches a
private registry or uses an npm token. cdkd resolves a relative src against
the working directory and expands ~.
Services left without an override
At startup, cdkd warns about each service that still uses an image from a registry. In a terminal it also asks for a Dockerfile for each one. Two flags change this:
--no-interactive-overridesturns the questions off, for scripts and CI.--strict-overridesmakes cdkd refuse to start while any such service remains. The check runs before any service starts, so nothing is left running.
Related
cdkd local start-service: the command these flags belong tocdkd local start-alb: accepts the same flags for the services behind a load balancer