Docker Provider
The Docker provider allows you to create, manage, and orchestrate Docker resources directly from your Alchemy applications. With this provider, you can pull images, run containers, create networks, and more, all using the familiar Alchemy Resource syntax.
Resources
Section titled “Resources”The Docker provider includes the following resources:
- RemoteImage - Pull and manage Docker images
- Image - Build Docker images from local Dockerfiles
- Container - Run and manage Docker containers
- Network - Create and manage Docker networks
- Volume - Create and manage persistent Docker volumes
Example
Section titled “Example”Here’s a complete example of using the Docker provider to create a web application with Redis, custom images, and persistent volumes:
import * as docker from "alchemy/docker";
// Create a Docker networkconst network = await docker.Network("app-network", { name: "my-application-network"});
// Create a persistent volume for Redis dataconst redisVolume = await docker.Volume("redis-data", { name: "redis-data", labels: [ { name: "app", value: "my-application" }, { name: "service", value: "redis" } ]});
// Pull Redis imageconst redisImage = await docker.RemoteImage("redis-image", { name: "redis", tag: "alpine"});
// Run Redis container with persistent volumeconst redis = await docker.Container("redis", { image: redisImage.imageRef, name: "redis", networks: [{ name: network.name }], volumes: [ { hostPath: redisVolume.name, containerPath: "/data" } ], start: true});
// Build a custom application image from local Dockerfileconst appImage = await docker.Image("app-image", { name: "my-web-app", tag: "latest", build: { context: "./app", buildArgs: { NODE_ENV: "production" } }});
// Create a volume for application logsconst logsVolume = await docker.Volume("logs-volume", { name: "app-logs", labels: { "com.example.environment": "production", "com.example.backup": "daily" }});
// Run the application containerconst app = await docker.Container("app", { image: appImage, // Using the custom built image name: "web-app", ports: [{ external: 3000, internal: 3000 }], networks: [{ name: network.name }], volumes: [ { hostPath: logsVolume.name, containerPath: "/app/logs" } ], environment: { REDIS_HOST: "redis", NODE_ENV: "production" }, restart: "always", start: true});
// Output the URLexport const url = `http://localhost:3000`;
Additional Resources
Section titled “Additional Resources”For more complex examples, see the Docker Example in the Alchemy repository.