Container
The Container
resource allows you to create and manage Docker containers using Alchemy.
import * as docker from "alchemy/docker";
const myContainer = await docker.Container("my-container", { image: "nginx:latest", name: "web-server", ports: [{ external: 80, internal: 80 }], start: true});
Properties
Section titled “Properties”Name | Type | Required | Description |
---|---|---|---|
image | RemoteImage | string | Yes | Docker image to use for the container |
name | string | No | Name for the container |
command | string[] | No | Command to run in the container |
environment | Record<string, string> | No | Environment variables for the container |
ports | { external: number | string, internal: number | string, protocol?: "tcp" | "udp" }[] | No | Port mappings from host to container |
volumes | { hostPath: string, containerPath: string, readOnly?: boolean }[] | No | Volume mappings from host paths to container paths |
networks | { name: string, aliases?: string[] }[] | No | Networks to connect to |
restart | "no" | "always" | "on-failure" | "unless-stopped" | No | Restart policy |
removeOnExit | boolean | No | Whether to remove the container when it exits |
start | boolean | No | Start the container after creation |
Outputs
Section titled “Outputs”Name | Type | Description |
---|---|---|
id | string | The ID of the container |
state | "created" | "running" | "paused" | "stopped" | "exited" | The current state of the container |
createdAt | number | Time when the container was created |
Example
Section titled “Example”import * as docker from "alchemy/docker";
// Create a Docker networkconst network = await docker.Network("app-network", { name: "microservices-network"});
// Pull the Redis imageconst redisImage = await docker.RemoteImage("redis-image", { name: "redis", tag: "alpine"});
// Run Redis containerconst redis = await docker.Container("redis", { image: redisImage.imageRef, name: "redis", networks: [{ name: network.name }], start: true});
// Run the application containerconst app = await docker.Container("app", { image: "my-node-app:latest", name: "web-app", ports: [{ external: 3000, internal: 3000 }], networks: [{ name: network.name }], environment: { REDIS_HOST: "redis", NODE_ENV: "production" }, volumes: [ { hostPath: "./logs", containerPath: "/app/logs" } ], restart: "always", start: true});