Skip to content

This minimalist Docker image allows you to run a Cloudflare Worker inside a Docker container, offering a simple solution for deploying and testing your worker on your infrastructure.

License

Notifications You must be signed in to change notification settings

JacobLinCool/workerd-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Workerd Docker Image

Supported Architectures: amd64, arm64

This minimalist Docker image allows you to run a Cloudflare Worker inside a Docker container, offering a simple solution for deploying and testing your worker on your infrastructure.

Quick Start Guide

Selflare

The fastest way to get your Cloudflare Worker running as a Docker container is to use Selflare, which also supports KV, D1, R2, DO, and Cache API out of the box.

npm i -g selflare  # Install Selflare
selflare compile   # Compile yours worker to Cap'n Proto
selflare docker    # Generate Dockerfile and docker-compose.yml
docker compose up  # Run the worker

You can now access your worker at http://localhost:8080!

Manual Setup

If you prefer to set up the Cap'n Proto manually, follow the steps below.

1. Prepare Your Worker

First, build your worker with Wrangler by running the following command in your project directory:

wrangler deploy --dry-run --outdir .wrangler/dist

This command compiles your worker and outputs the result script to .wrangler/dist/index.js.

2. Configure worker.capnp

Next, create a worker.capnp file in the same directory as your wrangler.toml. This file is used to configure the Workerd runtime. Below is a template you can start with:

using Workerd = import "/workerd/workerd.capnp";

const config :Workerd.Config = (
    services = [
        (name = "main", worker = .worker),
    ],
    sockets = [
        (service = "main", name = "http", address = "*:8080", http = ()),
    ]
);

const worker :Workerd.Worker = (
    modules = [
        (name = "worker", esModule = embed ".wrangler/dist/index.js"),
    ],
    compatibilityDate = "2024-02-19",
);

For detailed information on configuring worker.capnp, refer to the Workerd Repository.

3. Run the Docker Image

With your worker built and configured, you're ready to run it inside a Docker container. Execute the following command:

docker run -v $(pwd):/worker -p 8080:8080 jacoblincool/workerd

This command mounts your current directory to the /worker directory inside the container and forwards port 8080 to your local machine.

Accessing Your Worker

After starting the Docker container, your worker will be accessible at http://localhost:8080.

Use as a Base Image

You can also use this image as a base image for your own worker. Below is an example Dockerfile:

FROM jacoblincool/workerd

COPY worker.capnp worker.capnp
COPY other-file-like-your-worker.js other-file-like-your-worker.js

About

This minimalist Docker image allows you to run a Cloudflare Worker inside a Docker container, offering a simple solution for deploying and testing your worker on your infrastructure.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published