Skip to main content
Skip to main content

Docker Service

The Docker service runs Docker Engine in a dedicated virtual machine (VM). Use it for software that ships as a Docker image or a Docker Compose stack. A VM boots slower than a container and its resources don't scale automatically, so your own code is usually better served by a native runtime service. See Containers vs VMs for the difference.

Quick Start​

Create the service with a zerops-import.yaml:

zerops-import.yaml
services:
- hostname: app
type: docker@26.1
verticalAutoscaling:
cpu: 1
ram: 1
disk: 5
zcli project service-import zerops-import.yaml

Then deploy a zerops.yaml that pulls the image and runs it:

zerops.yaml
zerops:
- setup: app
run:
base: docker@latest
prepareCommands:
- docker image pull crccheck/hello-world:v1.0.0
start: docker run --rm --network=host crccheck/hello-world:v1.0.0
ports:
- port: 8000
httpSupport: true

The Docker recipe repository has a complete example.

Resources and Scaling​

A Docker service doesn't scale automatically, neither its resources nor its number of VMs.

TopicBehavior
ResourcesFixed values for cpu, ram and disk in verticalAutoscaling, no min-max ranges. Without them a new service gets 1 vCPU, 1 GB RAM and 5 GB disk.
Changing resourcesEvery change restarts the VM.
DiskCan be increased, never decreased.
Number of VMsThe service runs minContainers VMs. With one VM, the service is unavailable during maintenance of its server.

Building and Preparing Images​

The build phase runs in a regular container without Docker, so images can't be built or pulled there. It can still prepare files for the VM, such as a docker-compose.yaml.

Pull or build images in run.prepareCommands. Zerops caches the result and reuses it on later deploys until the commands or the addToRunPrepare files change.

Use specific image tags

Always use specific version tags (like 1.0.0) instead of :latest. Because of the cache, a new :latest image isn't pulled on later deploys.

Building from a Dockerfile​

To build your own image without a registry, copy the Dockerfile and its build context to the prepare phase with addToRunPrepare and run docker build there:

zerops.yaml
zerops:
- setup: app
build:
deployFiles: ./
addToRunPrepare: ./
run:
base: docker@latest
prepareCommands:
- docker build --tag my-app:local .
start: docker run --rm --network=host my-app:local
ports:
- port: 8000
httpSupport: true

A change to any file in addToRunPrepare invalidates the cache, so every deploy with new code builds the image again.

Private Registries​

Store the registry credentials as secret variables and log in before pulling:

zerops.yaml
run:
base: docker@latest
prepareCommands:
- echo "$REGISTRY_PASSWORD" | docker login ghcr.io --username "$REGISTRY_USER" --password-stdin
- docker image pull ghcr.io/my-org/my-app:1.0.0
start: docker run --rm --network=host ghcr.io/my-org/my-app:1.0.0

Running Containers​

Network​

Run containers with --network=host (network_mode: host in Docker Compose). The container then listens directly on the VM's ports, and you expose them with ports in zerops.yaml. Docker's own port publishing (-p) is not needed.

Container Lifecycle​

Zerops runs the start command in the foreground and restarts it whenever it exits. Its output goes to the runtime log. Don't detach with -d: the command exits right away, Zerops starts it again, and every restart creates another container.

Each docker run creates a new container, and Docker keeps exited containers on the VM disk, also across VM restarts. Always pass --rm so Docker removes the container when it exits.

Without --rm, an app that crashes in a loop leaves thousands of stopped containers behind. The Docker daemon can then run out of memory while loading them on startup, and the VM fails its readiness check. To recover, stop the service, raise the RAM and start the service again. Then run docker container prune -f and lower the RAM.

Warning

Don't use --name without --rm. The next restart fails because the name is already in use, and the app doesn't come back.

Don't add --restart, Docker rejects it together with --rm.

Environment Variables​

Variables from run.envVariables and the service's environment variables are set for the start command, not inside your containers. Pass each one with -e (or list them under environment in Docker Compose):

zerops.yaml
run:
base: docker@latest
envVariables:
DB_HOST: ${db_hostname}
DB_PORT: ${db_port}
prepareCommands:
- docker image pull my-application:1.0.0
start: docker run --rm -e DB_HOST -e DB_PORT --network=host my-application:1.0.0

Docker Compose​

Deploy the docker-compose.yaml and make it available to prepareCommands with addToRunPrepare:

zerops.yaml
zerops:
- setup: app
build:
deployFiles: ./docker-compose.yaml
addToRunPrepare: ./docker-compose.yaml
run:
base: docker@latest
envVariables:
DB_HOST: ${db_hostname}
prepareCommands:
- docker compose pull
start: docker compose up --force-recreate
ports:
- port: 8000
httpSupport: true
docker-compose.yaml
services:
web:
image: web-image:1.0.0
network_mode: host
api:
image: api-image:1.0.0
network_mode: host
environment:
- DB_HOST

To run only some services of the file, name them: docker compose pull api and docker compose up api --force-recreate.

Docker Compose keeps one container per service across up runs, so stopped containers don't accumulate. docker compose up exits only when all its containers stop, so a single crashed service stays down. Set restart: unless-stopped on the services that Docker should restart.

Deploys and Data​

A deploy creates new VMs, and the old ones keep serving traffic until the new ones are ready, see Subsequent deploys. Add a readiness check so that Zerops switches traffic only when your container accepts requests.

VMs are ephemeral like containers. Data on the VM disk, including Docker volumes, is lost when a deploy replaces the VM. Keep persistent data in a database or storage service.

Debugging​

Connect to a VM over SSH and inspect it with the Docker CLI:

docker ps --all          # running and stopped containers
docker logs <container> # output of a container that still exists
docker images # images pulled or built in prepareCommands

The runtime log also keeps the output of containers that --rm already removed.

Supported Versions​

  • 26.1